AGXDynamics  2.35.0.2
Pointer.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_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_ */
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agxData::Value
Abstract representation of a value.
Definition: Value.h:42
agxData::Value::bind
void bind(const agx::Path &path)
Set bind path which will auto-bind when resolved.
agx::PointerT::operator->
T * operator->()
Definition: Pointer.h:189
agx::Path
Representation of a path, a list of name components.
Definition: Path.h:33
agx::PointerT::get
T * get()
Definition: Pointer.h:177
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::Model
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
agx::Pointer
A pointer to an agx::Object.
Definition: Pointer.h:34
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agx::PointerT::PointerT
PointerT(const Name &name, const String &autoBindPath="")
Definition: Pointer.h:163
agx::Name
Representation of a name string.
Definition: Name.h:32
Object.h
agx::Object
agx::Object is a refcounted object with a name.
Definition: Object.h:59
agx::PointerT::operator=
PointerT< T > & operator=(const PointerT< T > &pointer)
Definition: Pointer.h:195
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agx::PointerT
Definition: Pointer.h:87
agx::Device
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:57
agx::AgXString< std::string >
agxData::Value::ptr
const void * ptr() const
Definition: Value.h:321
agx::PointerT::~PointerT
virtual ~PointerT()
Definition: Pointer.h:171
agx::String
AgXString< std::string > String
Definition: String.h:423
agx::Pointer::get
Object * get()
Definition: Pointer.h:150
AGX_EXPIRED_SCOPE_ALLOCATION
#define AGX_EXPIRED_SCOPE_ALLOCATION
Definition: Referenced.h:95
Value.h
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58