AGX Dynamics  2.35.0.1
Object.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 AGX_OBJECT_H
18 #define AGX_OBJECT_H
19 
20 #include <agx/Referenced.h>
21 #include <agx/Name.h>
22 #include <agx/Path.h>
23 #include <agx/Vector.h>
24 #include <agx/HashTable.h>
25 #include <agx/HashSet.h>
26 
27 namespace agxData
28 {
29  class Type;
30 }
31 
32 namespace agxJson
33 {
34  class Value;
35 }
36 
37 namespace agx
38 {
39  class TiXmlElement;
40  class TiXmlNode;
41  class TiXmlDocument;
42 
43  class Model;
44  class Device;
45  class Component;
46  class Thread;
47 
48  // Cannot include agx/ThreadSynchronization.h because of include loop with Type.h and Model.h.
49  using ReentrantMutex = std::recursive_mutex;
50 
54 
60  {
61  public:
65  {
66  public:
67  // virtual void serialize() = 0;
68 
69  protected:
70  virtual ~Event();
71 
72  private:
73  };
74 
75  // Generated event interface, defined in Object.agxEvent
77 
78 #ifndef SWIG
79  void addListener(EventListener* listener);
80  void removeListener(EventListener* listener);
81  bool hasListener(EventListener* listener);
82 #endif
83 
84  public:
90  static void setEnableRebind(bool flag);
91 
95  static bool getEnableRebind();
96 
98 
99  static agx::String generateName(const agx::String& bindPath);
100 
101 
102 #ifndef SWIG
103  // void configure(const String& path);
104  virtual void configure(TiXmlElement* /*eObject*/) {}
105  virtual void save(TiXmlElement* /* eParent */) const {}
106  virtual void snapshot(TiXmlNode* /* eParent */, const String& /* directory */) const {}
107 
108  static Object *load(TiXmlElement *eObject, Device *device);
109 
110  template <typename T>
111  static T *load(TiXmlElement *eObject, Device *device);
112 #endif
113 
114  /* Load from file */
115  static Object *load(const String& path, Device *device, const String& type = "", size_t instance = 0);
116  static Object *load(const String& path, Device *device, const String& type, const String& attribute, const String& value);
117  static void load(const String& path, Device *device, ObjectPtrVector& loadedObjects);
118 
119 
120  /*
121  typedef Callback1<TiXmlElement *> LoadCallback;
122  static void load(const String& path, LoadCallback& callback);
123  */
124 
125  static TiXmlDocument openDocument(const String& path);
126 
127 
128  /* Auto-cast return value */
129  template <typename T>
130  static T *load(const String& path, Device *device, const String& type = "", size_t instance = 0);
131 
132  template <typename T>
133  static T *load(const String& path, Device *device, const String& type, const String& attribute, const String& value);
134 
136  public:
137  Object(const Name& name = Name(), Model *model = Object::ClassModel());
138 
139  // virtual Object *clone() const;
140 
144  const agx::Name& getName() const;
145 
149  void setName(const agx::Name& name);
150 
155 
160 
165 
170 
177 
178 
182  agx::UInt32 getId() const;
183 
187  void setModel(agx::Model *model);
188 
192  agx::Model *getModel();
193  const agx::Model *getModel() const;
194 
198  agx::Object *getContext();
199  const agx::Object *getContext() const;
200 
205  const agx::Object *getRootContext() const;
206 
210  bool hasParent(const agx::Object *node) const;
211 
216  const agx::Object *getNextSibling() const;
217 
221  agx::Object *getResource(const agx::Path& path, agx::Model *model = nullptr);
222  const agx::Object *getResource(const agx::Path& path, agx::Model *model = nullptr) const;
223 
227  agx::Object *getAutoScopedResource(const agx::Path& path, agx::Model *model = nullptr);
228  const agx::Object *getAutoScopedResource(const agx::Path& path, agx::Model *model = nullptr) const;
229 
231  template <typename T>
232  T *getContext();
233 
234  template <typename T>
235  const T *getContext() const;
236 
237  template <typename T>
238  T *getResource(const agx::Path& path);
239 
240  template <typename T>
241  const T *getResource(const agx::Path& path) const;
242 
243  template <typename T>
244  T *getAutoScopedResource(const agx::Path& path);
245 
246  template <typename T>
247  const T *getAutoScopedResource(const agx::Path& path) const;
248 
249  //---------------------------------------------------------------
250 
251 
252  bool isUnique() const;
253 
254  virtual void printSubtree() const {}
255 
256 #ifndef SWIG
257  virtual void buildNavigationTree(agxJson::Value& eNode) const;
258 #endif
259 
260  // Normally handled automatically...
261  virtual void setContext(agx::Object *context);
262  virtual void rebind() {}
263 #ifndef SWIG
264  virtual agx::Object *getResourceImpl(const agx::Path& path, agx::Model *model);
265 #endif
266 
267  void setId(UInt32 id);
268 
269  protected:
270  friend class Alias;
271  friend class Pointer;
272  friend class Component;
273  virtual ~Object();
274 
275  private:
276  static bool g_shouldRebind;
277 
278  private:
279  UInt32 m_id;
280  Name m_name;
281  Model *m_model;
282  Object *m_context;
283  Object *m_next;
284  EventDispatch *m_eventDispatch;
285  };
286 
288 
289 
294  // AGXCORE_EXPORT void setRoot(Component *root);
295 
299  AGXCORE_EXPORT Model* getModel(const Path& path, const Path& _namespace = Path());
300  AGXCORE_EXPORT Model* getModel(const Path& path, const Name& implementation, const Path& _namespace = Path());
301 
302  template <typename T>
303  T* getModel(const Path& path, const Path& _namespace = Path());
304 
305  template <typename T>
306  T* getModel(const Path& path, const Name& implementation, const Path& _namespace = Path());
307 
308 
309  // May contain both name and implementation using name:implementation format
310  AGXCORE_EXPORT Model* getModel(const String& path, const Path& _namespace = Path());
311  AGXCORE_EXPORT Model* getModel(const char* path, const Path& _namespace = Path());
312 
313 
317  AGXCORE_EXPORT Model* getModel(TiXmlElement* element);
318 
319 
321  AGXCORE_EXPORT void printNode(const char* path);
322 
323  // Internal use only.
324 
325  // The mutex returned by getTypeSystemMutex() must be held when calling the
326  // create.+Model functions. An alternative to always taking the mutex, in case
327  // that turns out be too costly, is to use the Double-Checked Locking Pattern.
328  // See https://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/
329  AGXCORE_EXPORT void createBuiltinModel(const char* path, Model* parent, Model*& result); // NOLINT
330  AGXCORE_EXPORT void createImplementedModel(const char* path, Model*& result);
332 
333  #define AGX_MODEL_IMPLEMENTATION(_class, _path) \
334  agx::Model *_class::ClassModel() \
335  { \
336  ScopeLock<ReentrantMutex> lock(agx::getTypeSystemMutex()); \
337  static agx::Model *s_model = nullptr; \
338  \
339  if (!s_model) \
340  agx::createImplementedModel(_path, s_model); \
341  \
342  if (!s_model) \
343  LOGGER_ERROR() << agx::String::format("No model found for path \'%s\'. Make sure AGX environment is properly setup", _path) << LOGGER_ENDL(); \
344  return s_model; \
345  }
346 
347 
348  #define AGX_BUILTIN_MODEL(_class, _path, _parent) \
349  agx::Model *_class::ClassModel() \
350  { \
351  ScopeLock<ReentrantMutex> lock(agx::getTypeSystemMutex()); \
352  static agx::Model *s_model = nullptr; \
353  \
354  if (!s_model) \
355  agx::createBuiltinModel(_path, _parent, s_model); \
356  \
357  return s_model; \
358  }
359 
360 
361 
362  /* Implementation */
363  template <typename T>
364  T *getModel(const Path& path, const Path& _namespace) { return dynamic_cast<T *>(agx::getModel(path, _namespace)); }
365 
366  template <typename T>
367  T *getModel(const Path& path, const Name& implementation, const Path& _namespace) { return dynamic_cast<T *>(agx::getModel(path, implementation, _namespace)); }
368 
369  AGX_FORCE_INLINE const Name& Object::getName() const { return m_name; }
370  AGX_FORCE_INLINE UInt32 Object::getId() const { return m_id; }
371  AGX_FORCE_INLINE Model *Object::getModel() { return m_model; }
372  AGX_FORCE_INLINE const Model *Object::getModel() const { return m_model; }
373  AGX_FORCE_INLINE Object *Object::getContext() { return m_context; }
374  AGX_FORCE_INLINE const Object *Object::getContext() const { return m_context; }
375  AGX_FORCE_INLINE bool Object::isUnique() const { return !m_next; }
376 
377 
378  template <typename T>
379  T *Object::load(const String& path, Device *device, const String& type, size_t instance)
380  {
381  Object *object = Object::load(path, device, type, instance);
382  return dynamic_cast<T *>(object);
383  }
384 
385  template <typename T>
386  T *Object::load(const String& path, Device *device, const String& type, const String& attribute, const String& value)
387  {
388  Object *object = Object::load(path, device, type, attribute, value);
389  return dynamic_cast<T *>(object);
390  }
391 
392  template <typename T>
393  T *Object::load(TiXmlElement *eObject, Device *device)
394  {
395  Object *object = Object::load(eObject, device);
396  return dynamic_cast<T *>(object);
397  }
398 
399 
400  template <typename T>
402  {
403  return dynamic_cast<T *>(this->getContext());
404  }
405 
406  template <typename T>
408  {
409  return dynamic_cast<const T *>(this->getContext());
410  }
411 
412 
413  template <typename T>
414  T *Object::getResource(const Path& path)
415  {
416  Object *object = this->getResource(path, T::ClassModel());
417  return (object != nullptr && object->is<T>()) ? object->as<T>() : nullptr;
418  }
419 
420  template <typename T>
421  const T *Object::getResource(const Path& path) const
422  {
423  return const_cast<Object *>(this)->getResource<T>(path);
424  }
425 
426 
427  template <typename T>
429  {
430  Object *object = this->getAutoScopedResource(path, T::ClassModel());
431  return (object != nullptr && object->is<T>()) ? object->as<T>() : nullptr;
432  }
433 
434  template <typename T>
435  const T *Object::getAutoScopedResource(const Path& path) const
436  {
437  return const_cast<Object *>(this)->getAutoScopedResource<T>(path);
438  }
439 }
440 
442 
443 // AGX_TYPE_BINDING(agx::Object *, "ObjectPointer")
444 
445 #endif /* _AGX_OBJECT_H_ */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXCORE_EXPORT
#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:666
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:60
agx::Path getPath() const
virtual void snapshot(TiXmlNode *, const String &) const
Definition: Object.h:106
static const HashSet< Object * > & getActiveObjects()
agx::String fullName() const
static Object * load(const String &path, Device *device, const String &type, const String &attribute, const String &value)
virtual void configure(TiXmlElement *)
Definition: Object.h:104
static agx::String generateName(const agx::String &bindPath)
virtual agx::Object * getResourceImpl(const agx::Path &path, agx::Model *model)
agx::Path getPath(const agx::Object *root) const
bool isUnique() const
Definition: Object.h:375
void setModel(agx::Model *model)
Set the object model (handled automatically).
void addListener(EventListener *listener)
const agx::Name & getName() const
Definition: Object.h:369
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:254
virtual void save(TiXmlElement *) const
Definition: Object.h:105
agx::Object * getContext()
Definition: Object.h:373
void removeListener(EventListener *listener)
bool hasParent(const agx::Object *node) const
agx::Model * getModel()
Definition: Object.h:371
agx::Object * getResource(const agx::Path &path, agx::Model *model=nullptr)
const agx::Object * getResource(const agx::Path &path, agx::Model *model=nullptr) const
agx::UInt32 getId() const
Definition: Object.h:370
const agx::Object * getRootContext() const
void setName(const agx::Name &name)
Set the name of the object.
static agx::Model * ClassModel()
static bool getEnableRebind()
Should rebinding be allowed? Right now a global setting.
static void load(const String &path, Device *device, ObjectPtrVector &loadedObjects)
virtual void buildNavigationTree(agxJson::Value &eNode) const
agx::String fullPath(const agx::Object *root) const
agx::Object * getNextSibling()
static Object * load(const String &path, Device *device, const String &type="", size_t instance=0)
virtual ~Object()
agx::Object * getRootContext()
void setId(UInt32 id)
virtual void rebind()
Definition: Object.h:262
const agx::Name & getImplementationName() const
const agx::Object * getAutoScopedResource(const agx::Path &path, agx::Model *model=nullptr) const
const agx::Object * getNextSibling() const
static TiXmlDocument openDocument(const String &path)
static Object * load(TiXmlElement *eObject, Device *device)
agx::String fullPath() const
agx::Object * getAutoScopedResource(const agx::Path &path, agx::Model *model=nullptr)
bool hasListener(EventListener *listener)
Representation of a path, a list of name components.
Definition: Path.h:34
A pointer to an agx::Object.
Definition: Pointer.h:35
Base class providing referencing counted objects.
Definition: Referenced.h:114
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.
AGXCORE_EXPORT void createBuiltinModel(const char *path, Model *parent, Model *&result)
uint32_t UInt32
Definition: Integer.h:40
AGXCORE_EXPORT Component * root()
AGXCORE_EXPORT void printNode(const char *path)
Debugging.
AGX_DECLARE_HASHSET_TYPES(Object)
std::recursive_mutex ReentrantMutex
Definition: Object.h:49
AGXCORE_EXPORT void createImplementedModel(const char *path, Model *&result)
AGXCORE_EXPORT agx::ReentrantMutex & getTypeSystemMutex()
HashTable< Name, Object * > ObjectPtrTable
Definition: Object.h:287
AGXCORE_EXPORT Model * getModel(const Path &path, const Path &_namespace=Path())
Definition: Object.h:364