AGX Dynamics  2.37.3.2
Pointer.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_POINTER_H
18 #define AGX_POINTER_H
19 
20 
21 
22 #include <agx/Object.h>
23 #include <agxData/Value.h>
24 
25 namespace agx
26 {
29 
34  class AGXCORE_EXPORT Pointer : public agxData::Value, public Object::EventListener
35  {
36  public:
37  static agx::Model *ClassModel();
38  static Pointer *load(TiXmlElement *ePointer, Device *device);
39  virtual void configure(TiXmlElement *ePointer) override;
40 
41  public:
42  Pointer(const agx::Name& name, const agx::String& autoBindPath = "");
43  Pointer(const agx::Name& name, agx::Object* target, const agx::String& autoBindPath = "");
44 
46 
50  virtual bool bind(agx::Object *object) override;
51 
55  void unbind();
56 
60  Object *get();
61  const Object *get() const;
62 
63  template <typename T>
64  T *get();
65 
66  template <typename T>
67  const T *get() const;
68 
69  virtual void buildNavigationTree(agxJson::Value& eNode) const override;
70 
71  protected:
72  virtual Object *getResourceImpl(const Path& path, agx::Model *model) override;
73  virtual ~Pointer();
74 
75  // From Object::EventListener
76  virtual void destroyCallback(agx::Object* object) override;
77 
78  private:
79  bool m_hasReference;
80  };
81 
82 
83 
84  //---------------------------------------------------------------
85 
86  template <typename T>
87  class PointerT : public Pointer
88  {
89  public:
90  PointerT(const Name& name, const String& autoBindPath = "");
91  PointerT(const Name& name, T *ptr, const String& autoBindPath = "");
92 
93  T *get();
94  const T *get() const;
95 
96  T *operator-> ();
97  const T *operator-> () const;
98 
99  PointerT<T>& operator= (const PointerT<T>& pointer);
100  PointerT<T>& operator= (T *pointer);
101 
102  operator T* ();
103 
104  protected:
105  virtual ~PointerT();
106  };
107 
108  #if 0
109 
112  template <typename T>
113  class Ptr : public PointerT<T>
114  {
115  public:
116  Ptr(const Name& name, const String& autoBindPath = "") : PointerT<T>(name, autoBindPath)
117  {
118  this->reference();
119  }
120 
121  Ptr(const Name& name, T *ptr, const String& autoBindPath = "") : PointerT<T>(name, ptr, autoBindPath)
122  {
123  this->reference();
124  }
125 
126  virtual ~Ptr()
127  {
128  this->forceUnbindChildren();
129  // agxAssert1(this->getReferenceCount() == 1, "Can not have active reference to member allocated value!");
130  this->unreference_nodelete();
131  if (this->getReferenceCount() > 0)
132  {
133  // std::cerr << "Warning: " << this->getReferenceCount() << " active references to member allocated value \'" << this->getName() << "\' (" << this << ") in component \'" << (this->getContext() ? this->getContext()->getName() : String("nullptr")) << "\' in explicit destructor!"<< std::endl;
134  this->setReferenceCount(AGX_EXPIRED_SCOPE_ALLOCATION);
135  }
136 
137  if (dynamic_cast<Component *>(this->getContext()))
138  static_cast<Component *>(this->getContext())->removeObject(this);
139  }
140 
141  Ptr<T>& operator= (const Ptr<T>& pointer) { return this->operator=(pointer.get()); }
142  Ptr<T>& operator= (T *pointer) { this->set((Object *)pointer); return *this; }
143  };
144  #endif
145 
146 
147 
148  /* Implementation */
149 
150  AGX_FORCE_INLINE Object *Pointer::get() { return agxData::Value::get<Object *>(); }
151  AGX_FORCE_INLINE const Object *Pointer::get() const { return agxData::Value::get<Object *>(); }
152 
153  template <typename T>
154  AGX_FORCE_INLINE T *Pointer::get() { return dynamic_cast<T *>(agxData::Value::get<Object *>()); }
155 
156  template <typename T>
157  AGX_FORCE_INLINE const T *Pointer::get() const { return dynamic_cast<const T *>(agxData::Value::get<Object *>()); }
158 
159 
160  //-----------------------------------------------------------------------------------------------------
161 
162  template <typename T>
163  PointerT<T>::PointerT(const Name& name, const String& autoBindPath) : Pointer(name, autoBindPath)
164  {}
165 
166  template <typename T>
167  PointerT<T>::PointerT(const Name& name, T *ptr, const String& autoBindPath) : Pointer(name, ptr, autoBindPath)
168  {}
169 
170  template <typename T>
172  {
173  }
174 
175 
176  template <typename T>
177  AGX_FORCE_INLINE T *PointerT<T>::get() { return m_value.get<T *>(); }
178 
179  template <typename T>
180  AGX_FORCE_INLINE const T *PointerT<T>::get() const { return m_value.get<T *>(); }
181 
182  template <typename T>
184  {
185  return this->get();
186  }
187 
188  template <typename T>
189  AGX_FORCE_INLINE T *PointerT<T>::operator-> () { return this->get(); }
190 
191  template <typename T>
192  AGX_FORCE_INLINE const T *PointerT<T>::operator-> () const { return this->get(); }
193 
194  template <typename T>
196  {
197  this->set((Object *)pointer.get());
198  return *this;
199  }
200 
201  template <typename T>
203  {
204  this->set((Object *)pointer);
205  return *this;
206  }
207 
208 
209  //-----------------------------------------------------------------------------------------------------
210 
211 }
212 
213 #endif /* _AGX_POINTER_H_ */
Representation of a path, a list of name components.
Definition: Path.h:32
Object * get()
Definition: Pointer.h:150
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
Abstract representation of a value.
Definition: Value.h:48
#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
PointerT(const Name &name, const String &autoBindPath="")
Definition: Pointer.h:163
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void bind(const agx::Path &path)
Set bind path which will auto-bind when resolved.
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
#define AGX_EXPIRED_SCOPE_ALLOCATION
Definition: Referenced.h:101
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
#define AGXCORE_EXPORT
agx::Object is a refcounted object with a name.
Definition: Object.h:58
T * get()
Definition: Pointer.h:177