AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
Object.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#pragma once
18
19#include <agx/Referenced.h>
20#include <agx/Name.h>
21#include <agx/Path.h>
22#include <agx/Vector.h>
23#include <agx/HashTable.h>
24#include <agx/HashSet.h>
25
26namespace agxData
27{
28 class Type;
29}
30
31namespace agxJson
32{
33 class Value;
34}
35
36namespace agx
37{
38 class TiXmlElement;
39 class TiXmlNode;
40 class TiXmlDocument;
41
42 class Model;
43 class Device;
44 class Component;
45 class Thread;
46
47 // Cannot include agx/ThreadSynchronization.h because of include loop with Type.h and Model.h.
48 using ReentrantMutex = std::recursive_mutex;
49
53
59 {
60 public:
64 {
65 public:
66 // virtual void serialize() = 0;
67
68 protected:
69 virtual ~Event();
70
71 private:
72 };
73
74 // Generated event interface, defined in Object.agxEvent
76
77#ifndef SWIG
78 void addListener(EventListener* listener);
79 void removeListener(EventListener* listener);
80 bool hasListener(EventListener* listener);
81#endif
82
83 public:
89 static void setEnableRebind(bool flag);
90
94 static bool getEnableRebind();
95
97
98 static agx::String generateName(const agx::String& bindPath);
99
100
101#ifndef SWIG
102 // void configure(const String& path);
103 virtual void configure(TiXmlElement* /*eObject*/) {}
104 virtual void save(TiXmlElement* /* eParent */) const {}
105 virtual void snapshot(TiXmlNode* /* eParent */, const String& /* directory */) const {}
106
107 static Object *load(TiXmlElement *eObject, Device *device);
108
109 template <typename T>
110 static T *load(TiXmlElement *eObject, Device *device);
111#endif
112
113 /* Load from file */
114 static Object *load(const String& path, Device *device, const String& type = "", size_t instance = 0);
115 static Object *load(const String& path, Device *device, const String& type, const String& attribute, const String& value);
116 static void load(const String& path, Device *device, ObjectPtrVector& loadedObjects);
117
118
119 /*
120 typedef Callback1<TiXmlElement *> LoadCallback;
121 static void load(const String& path, LoadCallback& callback);
122 */
123
124 static TiXmlDocument openDocument(const String& path);
125
126
127 /* Auto-cast return value */
128 template <typename T>
129 static T *load(const String& path, Device *device, const String& type = "", size_t instance = 0);
130
131 template <typename T>
132 static T *load(const String& path, Device *device, const String& type, const String& attribute, const String& value);
133
135 public:
136 Object(const Name& name = Name(), Model *model = Object::ClassModel());
137
138 // virtual Object *clone() const;
139
143 const agx::Name& getName() const;
144
148 void setName(const agx::Name& name);
149
154
159
164
169
174 agx::Path getPath(const agx::Object *root) const;
175 agx::String fullPath(const agx::Object *root) const;
176
177
181 agx::UInt32 getId() const;
182
186 void setModel(agx::Model *model);
187
192 const agx::Model *getModel() const;
193
197 agx::Object *getContext();
198 const agx::Object *getContext() const;
199
205
209 bool hasParent(const agx::Object *node) const;
210
216
220 agx::Object *getResource(const agx::Path& path, agx::Model *model = nullptr);
221 const agx::Object *getResource(const agx::Path& path, agx::Model *model = nullptr) const;
222
226 agx::Object *getAutoScopedResource(const agx::Path& path, agx::Model *model = nullptr);
227 const agx::Object *getAutoScopedResource(const agx::Path& path, agx::Model *model = nullptr) const;
228
230 template <typename T>
231 T *getContext();
232
233 template <typename T>
234 const T *getContext() const;
235
236 template <typename T>
237 T *getResource(const agx::Path& path);
238
239 template <typename T>
240 const T *getResource(const agx::Path& path) const;
241
242 template <typename T>
243 T *getAutoScopedResource(const agx::Path& path);
244
245 template <typename T>
246 const T *getAutoScopedResource(const agx::Path& path) const;
247
248 //---------------------------------------------------------------
249
250
251 bool isUnique() const;
252
253 virtual void printSubtree() const {}
254
255#ifndef SWIG
256 virtual void buildNavigationTree(agxJson::Value& eNode) const;
257#endif
258
259 // Normally handled automatically...
260 virtual void setContext(agx::Object *context);
261 virtual void rebind() {}
262#ifndef SWIG
263 virtual agx::Object *getResourceImpl(const agx::Path& path, agx::Model *model);
264#endif
265
266 void setId(UInt32 id);
267
268 protected:
269 friend class Alias;
270 friend class Pointer;
271 friend class Component;
272 virtual ~Object();
273
274 private:
275 static bool g_shouldRebind;
276
277 private:
278 UInt32 m_id;
279 Name m_name;
280 Model *m_model;
281 Object *m_context;
282 Object *m_next;
283 EventDispatch *m_eventDispatch;
284 };
285
287
288
293 // AGXCORE_EXPORT void setRoot(Component *root);
294
298 AGXCORE_EXPORT Model* getModel(const Path& path, const Path& _namespace = Path());
299 AGXCORE_EXPORT Model* getModel(const Path& path, const Name& implementation, const Path& _namespace = Path());
300
301 template <typename T>
302 T* getModel(const Path& path, const Path& _namespace = Path());
303
304 template <typename T>
305 T* getModel(const Path& path, const Name& implementation, const Path& _namespace = Path());
306
307
308 // May contain both name and implementation using name:implementation format
309 AGXCORE_EXPORT Model* getModel(const String& path, const Path& _namespace = Path());
310 AGXCORE_EXPORT Model* getModel(const char* path, const Path& _namespace = Path());
311
312
316 AGXCORE_EXPORT Model* getModel(TiXmlElement* element);
317
318
320 AGXCORE_EXPORT void printNode(const char* path);
321
322 // Internal use only.
323
324 // The mutex returned by getTypeSystemMutex() must be held when calling the
325 // create.+Model functions.
326 AGXCORE_EXPORT void createBuiltinModel(const char* path, Model* parent, std::atomic<Model*>& result); // NOLINT
327 AGXCORE_EXPORT void createImplementedModel(const char* path, std::atomic<Model*>& result);
329
330 #define AGX_MODEL_IMPLEMENTATION(_class, _path) \
331 agx::Model *_class::ClassModel() \
332 { \
333 static std::atomic<agx::Model*> s_model = nullptr; \
334 \
335 agx::Model* tmp = s_model.load(std::memory_order_acquire); \
336 \
337 if (tmp) \
338 return tmp; \
339 \
340 ScopeLock<ReentrantMutex> lock(agx::getTypeSystemMutex()); \
341 tmp = s_model.load(std::memory_order_relaxed); \
342 if (!tmp) { \
343 agx::createImplementedModel(_path, s_model); \
344 tmp = s_model.load(std::memory_order_relaxed); \
345 } \
346 \
347 if (!tmp) \
348 LOGGER_ERROR() << agx::String::format("No model found for path \'%s\'. Make sure AGX environment is properly setup", _path) << LOGGER_ENDL(); \
349 return tmp; \
350 }
351
352
353 #define AGX_BUILTIN_MODEL(_class, _path, _parent) \
354 agx::Model *_class::ClassModel() \
355 { \
356 static std::atomic<agx::Model*> s_model = nullptr; \
357 \
358 agx::Model* tmp = s_model.load(std::memory_order_acquire); \
359 \
360 if (tmp) \
361 return tmp; \
362 \
363 ScopeLock<ReentrantMutex> lock(agx::getTypeSystemMutex()); \
364 tmp = s_model.load(std::memory_order_relaxed); \
365 if (!tmp) { \
366 agx::createBuiltinModel(_path, _parent, s_model); \
367 tmp = s_model.load(std::memory_order_relaxed); \
368 } \
369 \
370 return tmp; \
371 }
372
373
374
375 /* Implementation */
376 template <typename T>
377 T *getModel(const Path& path, const Path& _namespace) { return dynamic_cast<T *>(agx::getModel(path, _namespace)); }
378
379 template <typename T>
380 T *getModel(const Path& path, const Name& implementation, const Path& _namespace) { return dynamic_cast<T *>(agx::getModel(path, implementation, _namespace)); }
381
382 AGX_FORCE_INLINE const Name& Object::getName() const { return m_name; }
383 AGX_FORCE_INLINE UInt32 Object::getId() const { return m_id; }
385 AGX_FORCE_INLINE const Model *Object::getModel() const { return m_model; }
387 AGX_FORCE_INLINE const Object *Object::getContext() const { return m_context; }
388 AGX_FORCE_INLINE bool Object::isUnique() const { return !m_next; }
389
390
391 template <typename T>
392 T *Object::load(const String& path, Device *device, const String& type, size_t instance)
393 {
394 Object *object = Object::load(path, device, type, instance);
395 return dynamic_cast<T *>(object);
396 }
397
398 template <typename T>
399 T *Object::load(const String& path, Device *device, const String& type, const String& attribute, const String& value)
400 {
401 Object *object = Object::load(path, device, type, attribute, value);
402 return dynamic_cast<T *>(object);
403 }
404
405 template <typename T>
406 T *Object::load(TiXmlElement *eObject, Device *device)
407 {
408 Object *object = Object::load(eObject, device);
409 return dynamic_cast<T *>(object);
410 }
411
412
413 template <typename T>
415 {
416 return dynamic_cast<T *>(this->getContext());
417 }
418
419 template <typename T>
421 {
422 return dynamic_cast<const T *>(this->getContext());
423 }
424
425
426 template <typename T>
427 T *Object::getResource(const Path& path)
428 {
429 Object *object = this->getResource(path, T::ClassModel());
430 return (object != nullptr && object->is<T>()) ? object->as<T>() : nullptr;
431 }
432
433 template <typename T>
434 const T *Object::getResource(const Path& path) const
435 {
436 return const_cast<Object *>(this)->getResource<T>(path);
437 }
438
439
440 template <typename T>
442 {
443 Object *object = this->getAutoScopedResource(path, T::ClassModel());
444 return (object != nullptr && object->is<T>()) ? object->as<T>() : nullptr;
445 }
446
447 template <typename T>
448 const T *Object::getAutoScopedResource(const Path& path) const
449 {
450 return const_cast<Object *>(this)->getAutoScopedResource<T>(path);
451 }
452}
453
455
456// AGX_TYPE_BINDING(agx::Object *, "ObjectPointer")
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXCORE_EXPORT
#define AGX_DECLARE_HASHSET_TYPES(type)
Definition: agx/HashSet.h:32
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:39
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:58
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:670
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
agx::Object is a refcounted object with a name.
Definition: Object.h:59
const agx::Object * getRootContext() const
agx::Path getPath() const
virtual void snapshot(TiXmlNode *, const String &) const
Definition: Object.h:105
agx::String fullName() const
virtual void configure(TiXmlElement *)
Definition: Object.h:103
static agx::String generateName(const agx::String &bindPath)
static Object * load(const String &path, Device *device, const String &type="", size_t instance=0)
const agx::Object * getAutoScopedResource(const agx::Path &path, agx::Model *model=nullptr) const
agx::Path getPath(const agx::Object *root) const
bool isUnique() const
Definition: Object.h:388
void setModel(agx::Model *model)
Set the object model (handled automatically).
const agx::Object * getNextSibling() const
const agx::Name & getImplementationName() const
void addListener(EventListener *listener)
const agx::Name & getName() const
Definition: Object.h:382
static void setEnableRebind(bool flag)
Sets if rebinding of objects should be allowed (Right now a global setting).
Object(const Name &name=Name(), Model *model=Object::ClassModel())
virtual void setContext(agx::Object *context)
virtual void printSubtree() const
Definition: Object.h:253
virtual void save(TiXmlElement *) const
Definition: Object.h:104
agx::Object * getContext()
Definition: Object.h:386
void removeListener(EventListener *listener)
bool hasParent(const agx::Object *node) const
agx::Model * getModel()
Definition: Object.h:384
static const HashSet< Object * > & getActiveObjects()
agx::UInt32 getId() const
Definition: Object.h:383
static Object * load(const String &path, Device *device, const String &type, const String &attribute, const String &value)
void setName(const agx::Name &name)
Set the name of the object.
static bool getEnableRebind()
Should rebinding be allowed? Right now a global setting.
static void load(const String &path, Device *device, ObjectPtrVector &loadedObjects)
const agx::Object * getResource(const agx::Path &path, agx::Model *model=nullptr) const
virtual void buildNavigationTree(agxJson::Value &eNode) const
agx::String fullPath(const agx::Object *root) const
agx::Object * getAutoScopedResource(const agx::Path &path, agx::Model *model=nullptr)
virtual ~Object()
void setId(UInt32 id)
virtual void rebind()
Definition: Object.h:261
agx::Object * getRootContext()
static agx::Model * ClassModel()
virtual agx::Object * getResourceImpl(const agx::Path &path, agx::Model *model)
static Object * load(TiXmlElement *eObject, Device *device)
static TiXmlDocument openDocument(const String &path)
agx::Object * getResource(const agx::Path &path, agx::Model *model=nullptr)
agx::Object * getNextSibling()
agx::String fullPath() const
bool hasListener(EventListener *listener)
Representation of a path, a list of name components.
Definition: Path.h:33
A pointer to an agx::Object.
Definition: Pointer.h:35
Base class providing referencing counted objects.
Definition: Referenced.h:120
bool is() const
Subclass test.
Vector containing 'raw' data.
Definition: agx/Vector.h:246
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:32
AGXCORE_EXPORT agx::ReentrantMutex & getTypeSystemMutex()
AGXCORE_EXPORT void printNode(const char *path)
Debugging.
AGXCORE_EXPORT void createBuiltinModel(const char *path, Model *parent, std::atomic< Model * > &result)
AGXCORE_EXPORT Model * getModel(const Path &path, const Path &_namespace=Path())
AGXCORE_EXPORT Component * root()
std::recursive_mutex ReentrantMutex
Definition: Object.h:48
AGXCORE_EXPORT void createImplementedModel(const char *path, std::atomic< Model * > &result)
HashTable< Name, Object * > ObjectPtrTable
Definition: Object.h:286