AGX Dynamics  2.37.2.0
Component.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 #ifndef AGX_COMPONENT_H
18 #define AGX_COMPONENT_H
19 
20 #include <iostream>
21 #include <agx/Object.h>
22 #include <typeinfo>
23 #include <agx/Event.h>
24 
25 namespace agx
26 {
27  class ComponentVisitor;
28  class Device;
29 
30 
33 
39  {
40  public:
41  static agx::Model* ClassModel();
42 
43  static Component* load(const String& path, const Path& _namespace = Path());
44  static Component* load(const String& path, Device* device, const Path& _namespace = Path());
45  static Component* load(const Path& path, const Name& implementation, Device* device, const Path& _namespace = Path());
46 
47 #ifndef SWIG
48  static Component* load(TiXmlElement* eComponent, Device* device);
49  virtual void configure(TiXmlElement* eComponent) override;
50  virtual void snapshot(TiXmlNode* eParent, const String& directory) const override;
51 #endif
52 
53  public:
54 
62 
63  ObjectEvent addObjectEvent;
64  ObjectEvent removeObjectEvent;
65 
66  public:
67  Component(const agx::Name& name = agx::Name(), agx::Model *model = agx::Component::ClassModel(), agx::Device *device = nullptr);
68 
74  virtual void addObject(agx::Object *object, bool assignContext = true);
75 
79  virtual void removeObject(agx::Object *object);
80 
84  void removeObject(const agx::Name& name, size_t index = 0);
85 
89  void removeAllObjects();
90 
94  agx::Object *getObject(const agx::Name& name, size_t index = 0);
95  const agx::Object *getObject(const agx::Name& name, size_t index = 0) const;
96 
97  agx::Object *getObject(const agx::Name& name, agx::Model *model, size_t index = 0);
98  const agx::Object *getObject(const agx::Name& name, agx::Model *model, size_t index = 0) const;
99 
100  template <typename T>
101  T *getObject(const agx::Name& name, size_t index = 0);
102 
103  template <typename T>
104  const T *getObject(const agx::Name& name, size_t index = 0) const;
105 
109  size_t getNumObjects(const agx::Name& name) const;
110 
114  const agx::ObjectRefVector& getObjects() const;
115 
119  template <typename T>
120  void getObjects(agx::ObjectPtrVector& result, bool recursive = false) const;
121 
122  void getObjects(agx::ObjectPtrVector& result, const agx::Model *model, bool recursive = false) const;
123 
124 
128  agx::Device *getDevice();
129  const agx::Device *getDevice() const;
130 
131  template <typename T> T *getDevice();
132  template <typename T> const T *getDevice() const;
133 
134 
135  virtual void rebind() override;
136 
137 #ifndef SWIG
138  virtual String autoComplete(const String& partialName, StringVector& matchingNames) const;
139 #endif
140 
142  void traverse( const TraverseCallback& callback );
143 
144  void traverse( ComponentVisitor* );
145 
146  virtual void printSubtree() const override;
147  void printSubtree(std::ostream& stream, int depth=0) const;
148 
149  void configure(Model *model);
150 #ifndef SWIG
151  virtual Object *getResourceImpl(const Path& path, agx::Model *model) override;
152 
153  virtual void buildNavigationTree(agxJson::Value& eNode) const override;
154 #endif
155 
156  protected:
157  virtual ~Component();
158  void setDevice(Device* device);
159  String expandAutoCompletionMatch(const String& query, const StringVector& matchingNames) const;
160 
161  private:
162  Object *findObject(const Name& name, Model *model, size_t index) const;
163  void triggerAddEvents(Object *child);
164  void triggerRemoveEvents(Object *child);
165 
166  public:
167  // Same as load, resolves XmlLoaderRegistrator ambiguity
168  static Component *_load(TiXmlElement *eComponent, Device *device);
169 
170  private:
171  ref_ptr<Referenced> m_device; // Referenced instead of Device to avoid include loop under Windows
172  ObjectRefVector m_childObjects;
173  ObjectPtrTable m_objectTable;
174  UInt m_rebindState;
175  };
176 
177 
178 
179 
180  /* Implementation */
181  #if 0
183  {
184  Object *parent = Object::getContext();
185  agxAssert(!parent || dynamic_cast<Component *>(parent));
186  return static_cast<Component *>(parent);
187  }
188 
189  AGX_FORCE_INLINE const Component *Component::getContext() const { return const_cast<Component *>(this)->getContext(); }
190  #endif
191 
192  AGX_FORCE_INLINE const ObjectRefVector& Component::getObjects() const { return m_childObjects; }
193 
194  AGX_FORCE_INLINE Object *Component::getObject(const Name& name, size_t index) { return this->findObject(name, nullptr, index); }
195  AGX_FORCE_INLINE const Object *Component::getObject(const Name& name, size_t index) const { return this->findObject(name, nullptr, index); }
196 
197  AGX_FORCE_INLINE Object *Component::getObject(const Name& name, Model *model, size_t index) { return this->findObject(name, model, index); }
198  AGX_FORCE_INLINE const Object *Component::getObject(const Name& name, Model *model, size_t index) const { return this->findObject(name, model, index); }
199 
200 
201  template <typename T>
202  AGX_FORCE_INLINE T *Component::getObject(const Name& name, size_t index)
203  {
204  Object *object = this->findObject(name, T::ClassModel(), index);
205  T *result = dynamic_cast<T *>(object);
206 
207  #ifdef AGX_DEBUG
208  if (object && !result && this->getNumObjects(name) > 1)
209  std::cout << this->getPath() << ": Unable to cast child object \'" << name << "\' (" << object << ") to type \'" << typeid(T).name() << "\', there are however other children with the same name so an unintended name clash could be the problem." << std::endl;
210  #endif
211 
212  return result;
213  }
214 
215  template <typename T>
216  AGX_FORCE_INLINE const T *Component::getObject(const Name& name, size_t index) const
217  {
218  return const_cast<Component *>(this)->getObject<T>(name, index);
219  }
220 
221 
222  AGX_FORCE_INLINE Device *Component::getDevice() { return reinterpret_cast<Device *>(m_device.get()); }
223  AGX_FORCE_INLINE const Device *Component::getDevice() const { return const_cast<Component *>(this)->getDevice(); }
224 
225  template <typename T>
227  {
228  agxAssert(dynamic_cast<T *>(m_device.get()));
229  return static_cast<T *>(m_device.get());
230  }
231 
232  template <typename T>
234  {
235  agxAssert(dynamic_cast<const T *>(m_device.get()));
236  return static_cast<const T *>(m_device.get());
237  }
238 
239 
240  template <typename T>
241  void Component::getObjects(ObjectPtrVector& result, bool recursive) const
242  {
243  Component::getObjects(result, T::ClassModel(), recursive);
244  }
245 
246 
247 
248 
249 }
250 
251 
252 #endif /* AGX_COMPONENT_H */
Representation of a path, a list of name components.
Definition: Path.h:32
const agx::ObjectRefVector & getObjects() const
Definition: Component.h:192
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
uint32_t UInt
Definition: Integer.h:34
ObjectEvent addObjectEvent
Definition: Component.h:63
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::Object * getObject(const agx::Name &name, size_t index=0)
Definition: Component.h:194
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
agx::Object * getContext()
Definition: Object.h:372
#define agxAssert(expr)
Definition: debug.h:143
static agx::Model * ClassModel()
agx::Device * getDevice()
Definition: Component.h:222
Event2< Component *, Object * > ObjectEvent
Event when adding removing child objects.
Definition: Component.h:61
Callback1< Object * > TraverseCallback
Definition: Component.h:141
ObjectEvent removeObjectEvent
Definition: Component.h:64
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
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
Base class for ComponentVisitors.
#define AGXCORE_EXPORT
agx::Object is a refcounted object with a name.
Definition: Object.h:58