AGX Dynamics 2.41.2.0
Loading...
Searching...
No Matches
ref_ptr.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_REF_PTR_H
18#define AGX_REF_PTR_H
19
20#include <agx/macros.h>
21#include <agx/hash.h>
22#include <agx/HashFunction.h>
23
24namespace agx
25{
26
28 template<class T>
29 class ref_ptr
30 {
31 public:
32
33#if !(defined(AGXJAVA) || defined(AGXPYTHON))
34
36 typedef T element_type;
37
40
42 ref_ptr(T* ptr);
43
45 ref_ptr(const ref_ptr& rp);
46
49
52
55
57 operator T* () const;
58
60 template <typename U>
61 bool operator == (ref_ptr<U> const& rp) const;
62
64 template <typename U>
65 bool operator != (ref_ptr<U> const& rp) const;
66
68 template <typename U>
69 bool operator == (U const* p) const;
70
71
73 template <typename U>
74 bool operator != (U const* p) const;
75
77 template <typename U>
78 bool operator == (U* p) const;
79
81 template <typename U>
82 bool operator != (U* p) const;
83
85 bool operator < (const ref_ptr& rp) const;
86
88 T& operator*() const;
89
91 T* operator->() const;
92
94 T* get() const;
95
97 bool operator!() const;
98
100 bool isValid() const;
101
104
107
111 void swap(ref_ptr& rp);
112
113#endif //AGXJAVA || AGXPYTHON
114
115 private:
116 T* m_ptr;
117 };
118
119 template <typename T, typename U>
120 AGX_FORCE_INLINE bool operator == (T const* p, ref_ptr<U> const& rp)
121 {
122 return (rp == p);
123 }
124
125 template <typename T, typename U>
126 AGX_FORCE_INLINE bool operator != (T const* p, ref_ptr<U> const& rp)
127 {
128 return !(rp == p);
129 }
130
131 template <typename T, typename U>
133 {
134 return (rp == p);
135 }
136
137 template <typename T, typename U>
139 {
140 return !(rp == p);
141 }
142
143
144 template <typename T>
146
147 template <typename T>
149 {
150 if (m_ptr) m_ptr->reference(this);
151 }
152
153 template <typename T>
155 {
156 if (m_ptr) m_ptr->reference(this);
157 }
158
159 template <typename T>
161 {
162 if (m_ptr) m_ptr->unreference(this);
163 m_ptr = 0;
164 }
165
166 template <typename T>
168 {
169 if (m_ptr == rp.m_ptr) return *this;
170 T* tmp_ptr = m_ptr;
171 m_ptr = rp.m_ptr;
172 if (m_ptr) m_ptr->reference(this);
173 // unref second to prevent any deletion of any object which might
174 // be referenced by the other object. i.e rp is child of the
175 // original m_ptr.
176 if (tmp_ptr) tmp_ptr->unreference(this);
177 return *this;
178 }
179
180 template <typename T>
182 {
183 if (m_ptr == ptr) return *this;
184 T* tmp_ptr = m_ptr;
185 m_ptr = ptr;
186 if (m_ptr) m_ptr->reference(this);
187 // unref second to prevent any deletion of any object which might
188 // be referenced by the other object. i.e rp is child of the
189 // original m_ptr.
190 if (tmp_ptr) tmp_ptr->unreference(this);
191 return *this;
192 }
193
195 template <typename T>
197 {
198 return m_ptr;
199 }
200
201 template <typename T> template <typename U>
203 {
204 return m_ptr == rp.get();
205 }
206
207 template <typename T> template <typename U>
209 {
210 return m_ptr != rp.get();
211 }
212
213 template <typename T> template <typename U>
215 {
216 return m_ptr == p;
217 }
218
219 template <typename T> template <typename U>
221 {
222 return m_ptr != p;
223 }
224
225 template <typename T> template <typename U>
227 {
228 return m_ptr == p;
229 }
230
231 template <typename T> template <typename U>
233 {
234 return m_ptr != p;
235 }
236
237 template <typename T>
239 {
240 return (m_ptr < rp.m_ptr);
241 }
242
243 template <typename T>
245 {
246 return *m_ptr;
247 }
248
249 template <typename T>
251 {
252 return m_ptr;
253 }
254
255 template <typename T>
257 {
258 return m_ptr;
259 }
260
261 template <typename T>
263 {
264 return m_ptr == 0; // not required
265 }
266
267 template <typename T>
269 {
270 return m_ptr != 0;
271 }
272
273 template <typename T>
275 {
276 T* tmp = m_ptr;
277 if (m_ptr) m_ptr->unreference_nodelete();
278 m_ptr = 0;
279 return tmp;
280 }
281
282 template <typename T>
284 {
285 m_ptr = nullptr;
286 }
287
288 template <typename T>
290 {
291 T* tmp = m_ptr;
292 m_ptr = rp.m_ptr;
293 rp.m_ptr = tmp;
294 }
295
296 // Hash function
297 template<typename T>
298 struct HashFn< ref_ptr<T> >
299 {
301 {
302 return agx::hash(key.get());
303 }
304
305 typedef const T *PtrT;
307 {
308 return agx::hash(key);
309 }
310 };
311
312
313 template<class Type>
315 const Type* operator()( agx::ref_ptr<Type> a ) const {
316 return a.get();
317 }
318 };
319
320
321 template<class T> AGX_FORCE_INLINE
323 {
324 return rp.get();
325 }
326
327 template<class T, class Y> AGX_FORCE_INLINE
329 {
330 return static_cast<T*>(rp.get());
331 }
332
333 template<class T, class Y> AGX_FORCE_INLINE
335 {
336 return dynamic_cast<T*>(rp.get());
337 }
338
339 template<class T, class Y> AGX_FORCE_INLINE
341 {
342 return const_cast<T*>(rp.get());
343 }
344
345
346
347}
348
349namespace std
350{
351 template<class T> AGX_FORCE_INLINE
353 {
354 rp1.swap(rp2);
355 }
356}
357
358
359#endif
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
~ref_ptr()
Destructor, will decrement reference count with one.
Definition: ref_ptr.h:160
ref_ptr(const ref_ptr &rp)
Copy constructor. Will increment reference count with one.
Definition: ref_ptr.h:154
bool operator!=(ref_ptr< U > const &rp) const
Definition: ref_ptr.h:208
ref_ptr()
Constructor.
Definition: ref_ptr.h:145
T element_type
The type of the referenced class.
Definition: ref_ptr.h:36
T * get() const
Definition: ref_ptr.h:256
ref_ptr & operator=(const ref_ptr &rp)
Assignment operator, will increment reference count.
Definition: ref_ptr.h:167
T * release()
Release the reference (without decrementing) to the referenced object and return a native pointer.
Definition: ref_ptr.h:274
bool isValid() const
Definition: ref_ptr.h:268
bool operator==(ref_ptr< U > const &rp) const
Definition: ref_ptr.h:202
bool operator<(const ref_ptr &rp) const
Definition: ref_ptr.h:238
void forceClear()
Force the pointer to the referenced object to be null. Without deleting or decrement counters....
Definition: ref_ptr.h:283
bool operator!() const
Definition: ref_ptr.h:262
void swap(ref_ptr &rp)
Swap rb with this reference without changing reference count.
Definition: ref_ptr.h:289
ref_ptr(T *ptr)
Will take ownership and reference ptr.
Definition: ref_ptr.h:148
T * operator->() const
automatic cast to native pointer and use the dereferencing operator
Definition: ref_ptr.h:250
T & operator*() const
Definition: ref_ptr.h:244
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
UInt32 hash(const T &key)
Definition: HashFunction.h:65
uint32_t UInt32
Definition: Integer.h:32
ref_ptr< T > dynamic_pointer_cast(const ref_ptr< Y > &rp)
Definition: ref_ptr.h:334
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:222
T * get_pointer(const ref_ptr< T > &rp)
Definition: ref_ptr.h:322
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:213
ref_ptr< T > const_pointer_cast(const ref_ptr< Y > &rp)
Definition: ref_ptr.h:340
ref_ptr< T > static_pointer_cast(const ref_ptr< Y > &rp)
Definition: ref_ptr.h:328
STL namespace.
void swap(agx::Name &lhs, agx::Name &rhs)
Definition: Name.h:323
UInt32 operator()(const PtrT key) const
Definition: ref_ptr.h:306
UInt32 operator()(const ref_ptr< T > &key) const
Definition: ref_ptr.h:300
const Type * operator()(agx::ref_ptr< Type > a) const
Definition: ref_ptr.h:315