AGX Dynamics  2.37.2.0
observer_ptr.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_OBSERVER_PTR
18 #define AGX_OBSERVER_PTR
19 
20 #include <agx/macros.h>
21 #include <agx/hash.h>
22 
23 namespace agx
24 {
25 
27 
28 
29  class Observer
30  {
31  public:
32  virtual ~Observer() {}
33  virtual void objectDeleted(void*) {}
34  };
35 
37 
38 
46 
52 
59  template<class T>
60  class observer_ptr : public Observer
61  {
62  public:
63 
65  typedef T element_type;
66 
68  observer_ptr();
69 
71  observer_ptr(T* t);
72 
74  observer_ptr(const observer_ptr& rp);
75 
77  virtual ~observer_ptr();
78 
80  observer_ptr& operator = (const observer_ptr& rp);
81 
82  observer_ptr& operator = (T* ptr);
83 
84  virtual void objectDeleted(void*);
85 
87  operator T* () const;
88 
90  template <typename U>
91  bool operator == (observer_ptr<U> const& rp) const;
92 
94  template <typename U>
95  bool operator != (observer_ptr<U> const& rp) const;
96 
98  template <typename U>
99  bool operator == (U const* p) const;
100 
102  template <typename U>
103  bool operator != (U const* p) const;
104 
106  template <typename U>
107  bool operator == (U* p) const;
108 
110  template <typename U>
111  bool operator != (U* p) const;
112 
114  bool operator < (const observer_ptr& rp) const;
115 
117  T& operator*() const;
118 
120  T* operator->() const;
121 
123  T* get() const;
124 
126  bool operator!() const;
127 
129  bool isValid() const;
130 
131  private:
132  T* m_ptr;
133  };
134 
135  template<typename T>
137  {
138 
139  }
140 
141  template<typename T>
143  {
144  if (m_ptr) m_ptr->addObserver(this);
145  }
146 
147  template<typename T>
148  AGX_FORCE_INLINE observer_ptr<T>::observer_ptr(const observer_ptr<T>& rp): Observer(), m_ptr(rp.m_ptr)
149  {
150  if (m_ptr) m_ptr->addObserver(this);
151  }
152 
153  template<typename T>
155  {
156  if (m_ptr) m_ptr->removeObserver(this);
157  m_ptr = 0;
158 
159  }
160 
161  template<typename T>
163  {
164  if (m_ptr == rp.m_ptr) return *this;
165  if (m_ptr) m_ptr->removeObserver(this);
166 
167  m_ptr = rp.m_ptr;
168  if (m_ptr) m_ptr->addObserver(this);
169  return *this;
170  }
171 
172  template<typename T>
174  {
175  if (m_ptr == ptr) return *this;
176  if (m_ptr) m_ptr->removeObserver(this);
177 
178  m_ptr = ptr;
179  if (m_ptr) m_ptr->addObserver(this);
180 
181  return *this;
182  }
183 
184  template<typename T>
186  {
187  m_ptr = 0;
188  }
189 
190 #if 0 // Disable any hashing on observer_pointer, it can only be bad
191  // Hash function
192  template<typename T>
193  struct HashFn< observer_ptr<T> >
194  {
195  AGX_FORCE_INLINE UInt32 operator()(const observer_ptr<T>& key) const
196  {
197  return agx::hash(key.get());
198  }
199 
200  typedef const T *PtrT;
201  AGX_FORCE_INLINE UInt32 operator()(const PtrT key) const
202  {
203  return agx::hash(key);
204  }
205  };
206 #endif
207 
209  template<typename T>
211  {
212  return m_ptr;
213  }
214 
215  template<typename T>
216  template <typename U>
218  {
219  return m_ptr == rp.m_ptr;
220  }
221 
222  template<typename T>
223  template <typename U>
225  {
226  return m_ptr != rp.m_ptr;
227  }
228 
229  template<typename T>
230  template <typename U>
232  {
233  return m_ptr == p;
234  }
235 
236  template<typename T>
237  template <typename U>
239  {
240  return m_ptr != p;
241  }
242 
243  template<typename T>
244  template <typename U>
246  {
247  return m_ptr == p;
248  }
249 
250  template<typename T>
251  template <typename U>
253  {
254  return m_ptr != p;
255  }
256 
257  template<typename T>
258  AGX_FORCE_INLINE bool observer_ptr<T>::operator < (const observer_ptr& rp) const
259  {
260  return (m_ptr < rp.m_ptr);
261  }
262 
263  template<typename T>
265  {
266  return *m_ptr;
267  }
268 
269  template<typename T>
271  {
272  return m_ptr;
273  }
274 
275  template<typename T>
277  {
278  return m_ptr;
279  }
280 
281  template<typename T>
283  {
284  return m_ptr == 0; // not required
285  }
286 
287  template<typename T>
289  {
290  return m_ptr != 0;
291  }
292 
293 
294 
295 }
296 
297 #endif
AGXCORE_EXPORT UInt32 hash(const T &key)
Definition: HashFunction.h:65
T * get() const
Definition: observer_ptr.h:276
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:60
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:194
#define AGX_FORCE_INLINE
Definition: Object.h:59
observer_ptr()
Constructor.
Definition: observer_ptr.h:136
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:200
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
T element_type
The type of the referenced class.
Definition: observer_ptr.h:65
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)