AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
InternalData.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. 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:
50
51 public:
59 template<typename DataT, typename ObjT, typename... Args>
60 static DataT* getOrCreate( ObjT obj, Source source, Args&&... args );
61
69 template<typename DataT, typename ObjT>
70 static DataT* get( const ObjT& obj, Source source );
71
78 template<typename ObjT>
79 static void set( ObjT obj, agx::Referenced* data, Source source );
80
84 template<typename ObjT>
85 static agx::Bool hasData( const ObjT& obj, Source source );
86
87 protected:
91 virtual ~InternalData() {}
92
93 private:
97 template<typename ObjT>
98 static InternalData* getOrCreateInternal( ObjT obj );
99
103 template<typename ObjT>
104 static InternalData* getInternal( const ObjT& obj );
105
106 private:
107 agx::ref_ptr<agx::Referenced> m_data[ NUM_SOURCES ];
108 };
109
110 template<typename DataT, typename ObjT, typename... Args>
111 DataT* InternalData::getOrCreate( ObjT obj, InternalData::Source source, Args&&... args )
112 {
113 agxAssert( obj != nullptr );
114
115 InternalData* iData = getOrCreateInternal( obj );
116 if ( iData->m_data[ source ] == nullptr )
117 iData->m_data[ source ] = new DataT( std::forward<Args>( args )... );
118
119 return iData->m_data[ source ]->as<DataT>();
120 }
121
122 template<typename DataT, typename ObjT>
123 DataT* InternalData::get( const ObjT& obj, InternalData::Source source )
124 {
125 InternalData* iData = getInternal( obj );
126 return iData != nullptr && iData->m_data[ source ] != nullptr ? iData->m_data[ source ]->as<DataT>() : nullptr;
127 }
128
129 template<typename ObjT>
130 void InternalData::set( ObjT obj, Referenced* data, InternalData::Source source )
131 {
132 if ( obj == nullptr )
133 return;
134
135 InternalData* iData = getInternal( obj );
136 if ( iData == nullptr && data == nullptr )
137 return;
138
139 iData = getOrCreateInternal( obj );
140 iData->m_data[ source ] = data;
141 }
142
143 template<typename ObjT>
145 {
146 const InternalData* iData = getInternal( obj );
147 return iData != nullptr && iData->m_data[ source ] != nullptr;
148 }
149
150 template<typename ObjT>
151 InternalData* InternalData::getOrCreateInternal( ObjT obj )
152 {
153 agx::Referenced* data = obj->getInternalData();
154 if ( data == nullptr ) {
155 data = new InternalData();
156 obj->setInternalData( data );
157 }
158
159 return data->as<InternalData>();
160 }
161
162 template<typename ObjT>
163 InternalData* InternalData::getInternal( const ObjT& obj )
164 {
165 return obj != nullptr && obj->getInternalData() != nullptr ? obj->getInternalData()->template as<InternalData>() : nullptr;
166 }
167}
168
169#endif
#define AGXPHYSICS_EXPORT
Internal data for any object, ObjT, with methods: agx::Referenced* ObjT::getInternalData() const; voi...
static DataT * getOrCreate(ObjT obj, Source source, Args &&... args)
Get already created data or create new instance of the internal data.
static agx::Bool hasData(const ObjT &obj, Source source)
static DataT * get(const ObjT &obj, Source source)
Get already created data of the internal data.
static void set(ObjT obj, agx::Referenced *data, Source source)
Assign data to source.
Source
Source of internal data.
@ LINKED_STRUCTURE
Tracked vehicle related data.
@ RIGID_BODY_EMITTER
Rigid Body data.
@ WIRE
Maps geometries and rigid bodies back to wires and wire nodes.
@ MERGED_BODY
Merged states, neighbors etc.
@ ENERGY
Energy related data.
@ MERGE_SPLIT
AMOR related data.
@ DEFORMABLE1D
Deformable1d related data.
virtual ~InternalData()
Reference counted object, protected destructor.
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