AGX Dynamics  2.35.0.1
Type.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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 AGXDATA_TYPE2_H
18 #define AGXDATA_TYPE2_H
19 
20 #include <typeinfo>
21 #include <agxData/agxData.h>
22 #include <agxData/Format.h>
23 #include <agx/Vector.h>
24 #include <agx/HashTable.h>
25 #include <agx/HashVector.h>
26 #include <agx/Integer.h>
27 #include <agx/Referenced.h>
28 #include <agx/Object.h>
29 #include <agx/AtomicValue.h>
30 #include <agx/SpinMutex.h>
31 
32 namespace agx
33 {
34  class TiXmlElement;
35  class Device;
36  class Object;
37 }
38 
39 namespace agxData
40 {
41  #define AGX_TYPE_NAME_MAX_LENGTH 1023
42 
44 
47 
53 
54 
58  AGXCORE_EXPORT Format* getFormat(const agx::String& typeFormatName);
59  AGXCORE_EXPORT Format* getFormat(agx::TiXmlElement* eTypeFormat, agx::Device* device = nullptr);
60 
65 
70 
74  template <typename T>
75  Type *getType();
76 
80  template <typename T>
81  Format *getFormat();
82 
83 
87  const TypeTable& getTypes();
88 
93 
98  {
99  public:
101 
102  public:
103  Type(const agx::Name& name);
104 
108  agx::UInt getId() const;
109 
113  const FormatTable& getFormats() const;
114 
118  Format *getFormat(const agx::Name& name);
119  const Format *getFormat(const agx::Name& name) const;
120 
124  template <typename T>
125  Format *getFormat();
126 
127  template <typename T>
128  const Format *getFormat() const;
129 
133  Format *getDefaultFormat();
134  const Format *getDefaultFormat() const;
135 
136  void addFormat(Format *format, bool isDefault = false);
137  void setDefaultFormat(Format *format);
138  protected:
139  virtual ~Type();
140 
141  private:
142  friend class Format;
143  // void registerFormat(const Format *format, bool isDefault) const;
144 
145  private:
146  agx::UInt m_id;
147  FormatRef m_defaultFormat;
148  FormatTable m_formats;
149  };
150 
151 
155  template <typename T>
156  struct TypeBinding
157  {
158  static Type *getType()
159  {
160  Type *type = agxData::getOrCreateType(typeid(T).name());
161  if (!type->getDefaultFormat())
162  type->addFormat(new FormatT<T>("default"), true);
163 
164  return type;
165  }
166  };
167 
168  template <typename T>
170  {
171  static Format *getFormat()
172  {
173  Type *type = getType<T>();
174  agxAssertN(type->getFormat<T>(), "No format \'%s\' in type \'%s\'", typeid(T).name(), type->getName().c_str());
175  return type->getFormat<T>();
176  }
177  };
178 
179  #define AGX_TYPE_BINDING(_Type, _Name) \
180  namespace agxData \
181  { \
182  template <> \
183  struct TypeBinding<_Type> \
184  { \
185  static Type *getType() \
186  { \
187  return agxData::getOrCreateType(_Name); \
188  } \
189  }; \
190  }
191 
192  #define AGX_TEMPLATED_TYPE_BINDING(_Type, _Name) \
193  namespace agxData \
194  { \
195  template <typename T> \
196  struct TypeBinding< _Type<T> > \
197  { \
198  static Type *getType() \
199  { \
200  Format *format = agxData::getFormat<T>(); \
201  const agx::String& elementName = format->getName() == "default" ? format->getType()->getName().str() : format->fullName(); \
202  agx::String typeName = _Name "<" + elementName + ">"; \
203  Type *type = agxData::getOrCreateType(typeName); \
204  \
205  if (!type->getDefaultFormat()) \
206  type->addFormat(new FormatT< _Type<T> >("default"), true); \
207  \
208  return type; \
209  } \
210  }; \
211  \
212  template <typename T> \
213  struct FormatBinding< _Type<T> > \
214  { \
215  static Format *getFormat() \
216  { \
217  return getType< _Type<T> >()->getDefaultFormat(); \
218  } \
219  }; \
220  }
221 
222 
223 
224  /* Implementation */
225  inline agx::UInt Type::getId() const { return m_id; }
226  inline const FormatTable& Type::getFormats() const { return m_formats; }
227  inline Format *Type::getDefaultFormat() { return m_defaultFormat; }
228  inline const Format *Type::getDefaultFormat() const { return m_defaultFormat; }
229 
230  inline Format *Type::getFormat(const agx::Name& name)
231  {
232  FormatTable::iterator it = m_formats.find(name);
233  return it == m_formats.end() ? nullptr : it->second;
234  }
235 
236  inline const Format *Type::getFormat(const agx::Name& name) const
237  {
238  return const_cast<Type *>(this)->getFormat(name);
239  }
240 
241 
242 
243  template <typename T>
245  {
246  for (FormatTable::iterator it = m_formats.begin(); it != m_formats.end(); ++it)
247  if (it->second->getImplementationName() == typeid(T).name())
248  return it->second;
249 
250  return nullptr;
251  }
252 
253  template <typename T>
254  const Format *Type::getFormat() const
255  {
256  return const_cast<Type *>(this)->getFormat<T>();
257  }
258 
259 
260 
261  template <typename T>
263  {
264  return TypeBinding<T>::getType();
265  }
266 
267 
268  template <typename T>
270  {
272  }
273 
274 }
275 
276 #include <agx/IntegerTemplates.h>
277 #include <agx/RealTemplates.h>
278 #include <agx/IndexRangeTemplates.h>
279 
280 AGX_TYPE_BINDING(agx::String, "String")
281 AGX_TYPE_BINDING(agx::Name, "Name")
282 AGX_TYPE_BINDING(agx::Object *, "ObjectPointer")
284 AGX_TYPE_BINDING(agx::SpinMutex, "SpinMutex")
285 
286 AGX_TEMPLATED_TYPE_BINDING(agx::Vector, "Vector")
287 AGX_TEMPLATED_TYPE_BINDING(agx::HashSet, "HashSet")
288 
289 // AGX_TYPE_BINDING(void *, "Pointer")
290 
291 namespace agxData
292 {
293  template <typename T>
294  struct TypeBinding<T*>
295  {
296  static Type *getType()
297  {
298  return agxData::getOrCreateType("Pointer");
299  }
300  };
301 
302  template <typename T>
303  struct FormatBinding<T*>
304  {
305  static Format *getFormat()
306  {
307  return getType<T*>()->getDefaultFormat();
308  }
309  };
310 
311  template <> AGX_FORCE_INLINE Format *getFormat<void>() { return Format::Void(); }
312  template <> AGX_FORCE_INLINE Format *getFormat<agx::String>() { return agxData::getType<agx::String>()->getDefaultFormat(); }
313 }
314 
315 #endif /* _AGXDATA_TYPE2_H_ */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
#define AGX_TEMPLATED_TYPE_BINDING(_Type, _Name)
Definition: Type.h:192
#define AGXCORE_EXPORT
Templated format, connects the type abstractions to the programming language.
Definition: Format.h:331
A format is an implementation of a agxData::Type.
Definition: Format.h:64
Abstracted type.
Definition: Type.h:98
Format * getFormat()
Definition: Type.h:244
Type(const agx::Name &name)
void addFormat(Format *format, bool isDefault=false)
Format * getFormat(const agx::Name &name)
Definition: Type.h:230
const FormatTable & getFormats() const
Definition: Type.h:226
agx::UInt getId() const
Definition: Type.h:225
virtual ~Type()
void setDefaultFormat(Format *format)
Format * getDefaultFormat()
Definition: Type.h:227
static agx::Model * ClassModel()
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:58
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:41
iterator begin()
Iterator to first element in hash table.
iterator end()
Iterator marking end of hash table.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
Inheritance with partial specialization due to bug with ref_ptr containers.
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:41
Representation of a name string.
Definition: Name.h:33
const char * c_str() const
Definition: Name.h:189
const agx::Name & getName() const
Definition: Object.h:369
#define agxAssertN(expr, format,...)
Definition: debug.h:145
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
void printAllTypesAndFormats()
Print all types and formats to std::cout.
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
AGXCORE_EXPORT Type * getOrCreateType(const agx::String &name)
const TypeTable & getTypes()
AGXCORE_EXPORT Type * getType(const agx::String &name)
agx::HashTable< agx::Name, FormatRef > FormatTable
Definition: Type.h:46
agx::HashVector< agx::Name, TypeRef > TypeTable
Definition: Type.h:45
AGXCORE_EXPORT Format * getGenericStructFormat(size_t numBytes)
Format * getFormat< void >()
Definition: Type.h:311
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
AtomicValueT< agx::Int32 > AtomicValue
agx::AtomicValue is an alias for agx::AtomicValueT<agx::Int32>
Definition: AtomicValue.h:103
uint32_t UInt
Definition: Integer.h:34
static Format * getFormat()
Definition: Type.h:305
static Format * getFormat()
Definition: Type.h:171
static Type * getType()
Definition: Type.h:296
Type binding, templated type -> abstract type.
Definition: Type.h:157
static Type * getType()
Definition: Type.h:158