AGX Dynamics  2.37.3.2
InternalData.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_INTERNALDATA_H
18 #define AGX_INTERNALDATA_H
19 
20 #include <agx/Referenced.h>
21 #include <agx/agxPhysics_export.h>
22 
23 namespace agx
24 {
33  {
34  public:
39  enum Source
40  {
41  WIRE,
48  NUM_SOURCES
49  };
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
Merged states, neighbors etc.
Definition: InternalData.h:42
bool Bool
Definition: Integer.h:48
Deformable1d related data.
Definition: InternalData.h:44
Maps geometries and rigid bodies back to wires and wire nodes.
Definition: InternalData.h:41
#define AGXPHYSICS_EXPORT
T * as()
Subclass casting.
Source
Source of internal data.
Definition: InternalData.h:39
Base class providing referencing counted objects.
Definition: Referenced.h:119
static agx::Bool hasData(const ObjT &obj, Source source)
Definition: InternalData.h:144
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
static DataT * getOrCreate(ObjT obj, Source source, Args &&... args)
Get already created data or create new instance of the internal data.
Definition: InternalData.h:111
#define agxAssert(expr)
Definition: debug.h:143
Internal data for any object, ObjT, with methods: agx::Referenced* ObjT::getInternalData() const; voi...
Definition: InternalData.h:32
Tracked vehicle related data.
Definition: InternalData.h:46
virtual ~InternalData()
Reference counted object, protected destructor.
Definition: InternalData.h:91
static DataT * get(const ObjT &obj, Source source)
Get already created data of the internal data.
Definition: InternalData.h:123
static void set(ObjT obj, agx::Referenced *data, Source source)
Assign data to source.
Definition: InternalData.h:130
Energy related data.
Definition: InternalData.h:47