AGX Dynamics 2.40.0.0
Loading...
Searching...
No Matches
InternalData.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_INTERNALDATA_H
18#define AGX_INTERNALDATA_H
19
20#include <agx/Referenced.h>
22
23namespace agx
24{
33 {
34 public:
39 enum Source
40 {
49 NUM_SOURCES
50 };
51
52 public:
60 template<typename DataT, typename ObjT, typename... Args>
61 static DataT* getOrCreate( ObjT obj, Source source, Args&&... args );
62
70 template<typename DataT, typename ObjT>
71 static DataT* get( const ObjT& obj, Source source );
72
79 template<typename ObjT>
80 static void set( ObjT obj, agx::Referenced* data, Source source );
81
85 template<typename ObjT>
86 static agx::Bool hasData( const ObjT& obj, Source source );
87
88 protected:
92 virtual ~InternalData() {}
93
94 private:
98 template<typename ObjT>
99 static InternalData* getOrCreateInternal( ObjT obj );
100
104 template<typename ObjT>
105 static InternalData* getInternal( const ObjT& obj );
106
107 private:
108 agx::ref_ptr<agx::Referenced> m_data[ NUM_SOURCES ];
109 };
110
111 template<typename DataT, typename ObjT, typename... Args>
112 DataT* InternalData::getOrCreate( ObjT obj, InternalData::Source source, Args&&... args )
113 {
114 agxAssert( obj != nullptr );
115
116 InternalData* iData = getOrCreateInternal( obj );
117 if ( iData->m_data[ source ] == nullptr )
118 iData->m_data[ source ] = new DataT( std::forward<Args>( args )... );
119
120 return iData->m_data[ source ]->as<DataT>();
121 }
122
123 template<typename DataT, typename ObjT>
124 DataT* InternalData::get( const ObjT& obj, InternalData::Source source )
125 {
126 InternalData* iData = getInternal( obj );
127 return iData != nullptr && iData->m_data[ source ] != nullptr ? iData->m_data[ source ]->as<DataT>() : nullptr;
128 }
129
130 template<typename ObjT>
131 void InternalData::set( ObjT obj, Referenced* data, InternalData::Source source )
132 {
133 if ( obj == nullptr )
134 return;
135
136 InternalData* iData = getInternal( obj );
137 if ( iData == nullptr && data == nullptr )
138 return;
139
140 iData = getOrCreateInternal( obj );
141 iData->m_data[ source ] = data;
142 }
143
144 template<typename ObjT>
146 {
147 const InternalData* iData = getInternal( obj );
148 return iData != nullptr && iData->m_data[ source ] != nullptr;
149 }
150
151 template<typename ObjT>
152 InternalData* InternalData::getOrCreateInternal( ObjT obj )
153 {
154 agx::Referenced* data = obj->getInternalData();
155 if ( data == nullptr ) {
156 data = new InternalData();
157 obj->setInternalData( data );
158 }
159
160 return data->as<InternalData>();
161 }
162
163 template<typename ObjT>
164 InternalData* InternalData::getInternal( const ObjT& obj )
165 {
166 return obj != nullptr && obj->getInternalData() != nullptr ? obj->getInternalData()->template as<InternalData>() : nullptr;
167 }
168}
169
170#endif
#define AGXPHYSICS_EXPORT
Internal data for any object, ObjT, with methods: agx::Referenced* ObjT::getInternalData() const; voi...
Definition: InternalData.h:33
static DataT * getOrCreate(ObjT obj, Source source, Args &&... args)
Get already created data or create new instance of the internal data.
Definition: InternalData.h:112
static agx::Bool hasData(const ObjT &obj, Source source)
Definition: InternalData.h:145
static DataT * get(const ObjT &obj, Source source)
Get already created data of the internal data.
Definition: InternalData.h:124
static void set(ObjT obj, agx::Referenced *data, Source source)
Assign data to source.
Definition: InternalData.h:131
Source
Source of internal data.
Definition: InternalData.h:40
@ SENSOR_ENVIRONMENT
Sensor environment data.
Definition: InternalData.h:48
@ LINKED_STRUCTURE
Tracked vehicle related data.
Definition: InternalData.h:46
@ RIGID_BODY_EMITTER
Rigid Body data.
Definition: InternalData.h:45
@ WIRE
Maps geometries and rigid bodies back to wires and wire nodes.
Definition: InternalData.h:41
@ MERGED_BODY
Merged states, neighbors etc.
Definition: InternalData.h:42
@ ENERGY
Energy related data.
Definition: InternalData.h:47
@ MERGE_SPLIT
AMOR related data.
Definition: InternalData.h:43
@ DEFORMABLE1D
Deformable1d related data.
Definition: InternalData.h:44
virtual ~InternalData()
Reference counted object, protected destructor.
Definition: InternalData.h:92
Base class providing referencing counted objects.
Definition: Referenced.h:120
T * as()
Subclass casting.
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
#define agxAssert(expr)
Definition: debug.h:143
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
bool Bool
Definition: Integer.h:40