AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
Pointer.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
25namespace agx
26{
29
34 class AGXCORE_EXPORT Pointer : public agxData::Value, public Object::EventListener
35 {
36 public:
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
97 const T *operator-> () const;
98
101
102 operator T* ();
103
104 protected:
105 virtual ~PointerT();
106 };
107
108 #if 0
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_ */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGX_EXPIRED_SCOPE_ALLOCATION
Definition: Referenced.h:101
#define AGXCORE_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
Abstract representation of a value.
Definition: Value.h:49
const void * ptr() const
Definition: Value.h:328
void bind(const agx::Path &path)
Set bind path which will auto-bind when resolved.
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:58
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:59
Representation of a path, a list of name components.
Definition: Path.h:33
virtual ~PointerT()
Definition: Pointer.h:171
PointerT(const Name &name, const String &autoBindPath="")
Definition: Pointer.h:163
PointerT< T > & operator=(const PointerT< T > &pointer)
Definition: Pointer.h:195
T * operator->()
Definition: Pointer.h:189
const T * get() const
Definition: Pointer.h:180
PointerT(const Name &name, T *ptr, const String &autoBindPath="")
Definition: Pointer.h:167
T * get()
Definition: Pointer.h:177
A pointer to an agx::Object.
Definition: Pointer.h:35
Pointer(const agx::Name &name, const agx::String &autoBindPath="")
static Pointer * load(TiXmlElement *ePointer, Device *device)
Object * get()
Definition: Pointer.h:150
virtual bool bind(agx::Object *object) override
Bind the pointer to an object.
virtual Object * getResourceImpl(const Path &path, agx::Model *model) override
virtual void buildNavigationTree(agxJson::Value &eNode) const override
virtual void configure(TiXmlElement *ePointer) override
virtual void destroyCallback(agx::Object *object) override
static agx::Model * ClassModel()
Pointer(const agx::Name &name, agx::Object *target, const agx::String &autoBindPath="")
virtual ~Pointer()
void unbind()
Unbind the pointer.
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.