AGX Dynamics 2.41.3.0
Loading...
Searching...
No Matches
Type.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 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
32namespace agx
33{
34 class TiXmlElement;
35 class Device;
36 class Object;
37}
38
39namespace 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>
82
83
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);
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>
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 {
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 {
265 }
266
267
268 template <typename T>
270 {
272 }
273
274}
275
276#include <agx/IntegerTemplates.h>
277#include <agx/RealTemplates.h>
279
281AGX_TYPE_BINDING(agx::Name, "Name")
282AGX_TYPE_BINDING(agx::Object *, "ObjectPointer")
283AGX_TYPE_BINDING(agx::AtomicValue, "AtomicValue")
284AGX_TYPE_BINDING(agx::SpinMutex, "SpinMutex")
285
286AGX_TEMPLATED_TYPE_BINDING(agx::Vector, "Vector")
287AGX_TEMPLATED_TYPE_BINDING(agx::HashSet, "HashSet")
288
289// AGX_TYPE_BINDING(void *, "Pointer")
290
291namespace 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 {
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:254
#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:382
#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
Format * getFormat()
Definition: Type.h:269
void printAllTypesAndFormats()
Print all types and formats to std::cout.
AGXCORE_EXPORT Format * getGenericStructFormat(size_t numBytes)
AGXCORE_EXPORT Type * getOrCreateType(const agx::String &name)
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
agx::HashTable< agx::Name, FormatRef > FormatTable
Definition: Type.h:46
agx::HashVector< agx::Name, TypeRef > TypeTable
Definition: Type.h:45
Type * getType()
Definition: Type.h:262
const TypeTable & getTypes()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint64_t UInt
Definition: Integer.h:27
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