AGX Dynamics  2.37.3.2
Object.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 #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 
26 namespace agxData
27 {
28  class Type;
29 }
30 
31 namespace agxJson
32 {
33  class Value;
34 }
35 
36 namespace 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 
96  static agx::Model *ClassModel();
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 
134  static const HashSet<Object *>& getActiveObjects();
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 
153  const agx::Name& getImplementationName() const;
154 
158  agx::String fullName() const;
159 
163  agx::Path getPath() const;
164 
168  agx::String fullPath() const;
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 
191  agx::Model *getModel();
192  const agx::Model *getModel() const;
193 
197  agx::Object *getContext();
198  const agx::Object *getContext() const;
199 
203  agx::Object *getRootContext();
204  const agx::Object *getRootContext() const;
205 
209  bool hasParent(const agx::Object *node) const;
210 
214  agx::Object *getNextSibling();
215  const agx::Object *getNextSibling() const;
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. An alternative to always taking the mutex, in case
326  // that turns out be too costly, is to use the Double-Checked Locking Pattern.
327  // See https://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/
328  AGXCORE_EXPORT void createBuiltinModel(const char* path, Model* parent, Model*& result); // NOLINT
329  AGXCORE_EXPORT void createImplementedModel(const char* path, Model*& result);
331 
332  #define AGX_MODEL_IMPLEMENTATION(_class, _path) \
333  agx::Model *_class::ClassModel() \
334  { \
335  ScopeLock<ReentrantMutex> lock(agx::getTypeSystemMutex()); \
336  static agx::Model *s_model = nullptr; \
337  \
338  if (!s_model) \
339  agx::createImplementedModel(_path, s_model); \
340  \
341  if (!s_model) \
342  LOGGER_ERROR() << agx::String::format("No model found for path \'%s\'. Make sure AGX environment is properly setup", _path) << LOGGER_ENDL(); \
343  return s_model; \
344  }
345 
346 
347  #define AGX_BUILTIN_MODEL(_class, _path, _parent) \
348  agx::Model *_class::ClassModel() \
349  { \
350  ScopeLock<ReentrantMutex> lock(agx::getTypeSystemMutex()); \
351  static agx::Model *s_model = nullptr; \
352  \
353  if (!s_model) \
354  agx::createBuiltinModel(_path, _parent, s_model); \
355  \
356  return s_model; \
357  }
358 
359 
360 
361  /* Implementation */
362  template <typename T>
363  T *getModel(const Path& path, const Path& _namespace) { return dynamic_cast<T *>(agx::getModel(path, _namespace)); }
364 
365  template <typename T>
366  T *getModel(const Path& path, const Name& implementation, const Path& _namespace) { return dynamic_cast<T *>(agx::getModel(path, implementation, _namespace)); }
367 
368  AGX_FORCE_INLINE const Name& Object::getName() const { return m_name; }
369  AGX_FORCE_INLINE UInt32 Object::getId() const { return m_id; }
370  AGX_FORCE_INLINE Model *Object::getModel() { return m_model; }
371  AGX_FORCE_INLINE const Model *Object::getModel() const { return m_model; }
372  AGX_FORCE_INLINE Object *Object::getContext() { return m_context; }
373  AGX_FORCE_INLINE const Object *Object::getContext() const { return m_context; }
374  AGX_FORCE_INLINE bool Object::isUnique() const { return !m_next; }
375 
376 
377  template <typename T>
378  T *Object::load(const String& path, Device *device, const String& type, size_t instance)
379  {
380  Object *object = Object::load(path, device, type, instance);
381  return dynamic_cast<T *>(object);
382  }
383 
384  template <typename T>
385  T *Object::load(const String& path, Device *device, const String& type, const String& attribute, const String& value)
386  {
387  Object *object = Object::load(path, device, type, attribute, value);
388  return dynamic_cast<T *>(object);
389  }
390 
391  template <typename T>
392  T *Object::load(TiXmlElement *eObject, Device *device)
393  {
394  Object *object = Object::load(eObject, device);
395  return dynamic_cast<T *>(object);
396  }
397 
398 
399  template <typename T>
400  AGX_FORCE_INLINE T *Object::getContext()
401  {
402  return dynamic_cast<T *>(this->getContext());
403  }
404 
405  template <typename T>
406  AGX_FORCE_INLINE const T *Object::getContext() const
407  {
408  return dynamic_cast<const T *>(this->getContext());
409  }
410 
411 
412  template <typename T>
413  T *Object::getResource(const Path& path)
414  {
415  Object *object = this->getResource(path, T::ClassModel());
416  return (object != nullptr && object->is<T>()) ? object->as<T>() : nullptr;
417  }
418 
419  template <typename T>
420  const T *Object::getResource(const Path& path) const
421  {
422  return const_cast<Object *>(this)->getResource<T>(path);
423  }
424 
425 
426  template <typename T>
427  T *Object::getAutoScopedResource(const Path& path)
428  {
429  Object *object = this->getAutoScopedResource(path, T::ClassModel());
430  return (object != nullptr && object->is<T>()) ? object->as<T>() : nullptr;
431  }
432 
433  template <typename T>
434  const T *Object::getAutoScopedResource(const Path& path) const
435  {
436  return const_cast<Object *>(this)->getAutoScopedResource<T>(path);
437  }
438 }
439 
441 
442 // AGX_TYPE_BINDING(agx::Object *, "ObjectPointer")
Representation of a path, a list of name components.
Definition: Path.h:32
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
#define AGX_DECLARE_HASHSET_TYPES(type)
Definition: HashSet.h:32
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
AGXCORE_EXPORT void printNode(const char *path)
Debugging.
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
AGXCORE_EXPORT Model * getModel(const Path &path, const Path &_namespace=Path())
Definition: Object.h:363
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
Base class providing referencing counted objects.
Definition: Referenced.h:119
AGXCORE_EXPORT Model * getModel(TiXmlElement *element)
virtual void printSubtree() const
Definition: Object.h:253
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
AGXCORE_EXPORT void createImplementedModel(const char *path, Model *&result)
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
bool is() const
Subclass test.
Contains classes for low level data storage for AGX.
Definition: Container.h:23
#define AGX_FORCE_INLINE
AGXCORE_EXPORT agx::ReentrantMutex & getTypeSystemMutex()
virtual void configure(TiXmlElement *)
Definition: Object.h:103
AGXCORE_EXPORT void createBuiltinModel(const char *path, Model *parent, Model *&result)
std::recursive_mutex ReentrantMutex
Definition: Object.h:48
virtual void rebind()
Definition: Object.h:261
HashTable< Name, Object * > ObjectPtrTable
Definition: Object.h:286
virtual void save(TiXmlElement *) const
Definition: Object.h:104
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
AGXCORE_EXPORT Component * root()
Inheritance with partial specialization due to bug with ref_ptr containers.
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
A pointer to an agx::Object.
Definition: Pointer.h:34
uint32_t UInt32
Definition: Integer.h:40
Definition: Object.h:31
virtual void snapshot(TiXmlNode *, const String &) const
Definition: Object.h:105
#define AGXCORE_EXPORT
agx::Object is a refcounted object with a name.
Definition: Object.h:58