Simbody  3.7
ReferencePtr.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_REFERENCE_PTR_H_
2 #define SimTK_SimTKCOMMON_REFERENCE_PTR_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2012-15 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
28 #include <cassert>
29 #include <utility>
30 
31 namespace SimTK {
32 
60 template <class T> class ReferencePtr {
61 public:
62  typedef T element_type;
63  typedef T* pointer;
64  typedef T& reference;
65 
69  ReferencePtr() noexcept : p(nullptr) {}
70 
74  ReferencePtr(std::nullptr_t) noexcept : ReferencePtr() {}
75 
77  explicit ReferencePtr(T* tp) noexcept : p(tp) {}
78 
81  explicit ReferencePtr(T& t) noexcept : ReferencePtr(&t) {}
82 
85  ReferencePtr(const ReferencePtr&) noexcept : p(nullptr) {}
86 
89  ReferencePtr(ReferencePtr&& src) noexcept : p(src.release()) {}
90 
94  DEPRECATED_14("use ReferencePtr(nullptr) instead")
95  ReferencePtr(int mustBeZero) noexcept : ReferencePtr()
96  { assert(mustBeZero==0); }
104  ReferencePtr& operator=(const ReferencePtr& src) noexcept
105  { if (&src != this) reset(); return *this; }
106 
110  if (&src != this)
111  p = src.release();
112  return *this;
113  }
114 
117  ReferencePtr& operator=(T& t) noexcept
118  { reset(&t); return *this; }
119 
122  ReferencePtr& operator=(T* tp) noexcept
123  { reset(tp); return *this; }
124 
128  ~ReferencePtr() noexcept {reset();} // just being tidy
135  T* get() const noexcept {return p;}
136 
139  T& getRef() const {
140  SimTK_ERRCHK(p!=nullptr, "ReferencePtr::getRef()",
141  "An attempt was made to dereference a null pointer.");
142  return *p;
143  }
144 
147  T* operator->() const {return &getRef();}
148 
151  T& operator*() const {return getRef();}
152 
160  void reset(T* tp=nullptr) noexcept {p=tp;}
161 
164  void swap(ReferencePtr& other) noexcept {
165  std::swap(p, other.p);
166  }
167 
169  bool empty() const noexcept {return !p;}
170 
173  explicit operator bool() const noexcept {return !empty();}
174 
177  T* release() noexcept {
178  T* x=p;
179  p=nullptr;
180  return x;
181  }
182 
184  DEPRECATED_14("use reset() instead")
185  void clear() noexcept {reset();}
186 
189  DEPRECATED_14("use get() rather than implicit conversion to T*")
190  operator T*() const noexcept {return p;}
191 
194 private:
195  T* p; // can be nullptr
196 };
197 
198 
199 
200 //==============================================================================
201 // SimTK namespace-scope functions
202 //==============================================================================
203 // These namespace-scope functions will be resolved by the compiler using
204 // "Koenig lookup" which examines the arguments' namespaces first.
205 // See Herb Sutter's discussion here: http://www.gotw.ca/publications/mill08.htm.
206 
211 template <class T> inline void
213  p1.swap(p2);
214 }
215 
219 template <class charT, class traits, class T>
220 inline std::basic_ostream<charT,traits>&
221 operator<<(std::basic_ostream<charT,traits>& os,
222  const ReferencePtr<T>& p)
223 { os << p.get(); return os; }
224 
230 template <class T, class U>
231 inline bool operator==(const ReferencePtr<T>& lhs,
232  const ReferencePtr<U>& rhs)
233 { return lhs.get() == rhs.get(); }
234 
237 template <class T>
238 inline bool operator==(const ReferencePtr<T>& lhs, std::nullptr_t)
239 { return lhs.empty(); }
240 
243 template <class T>
244 inline bool operator==(std::nullptr_t, const ReferencePtr<T>& rhs)
245 { return rhs.empty(); }
246 
253 template <class T, class U>
254 inline bool operator<(const ReferencePtr<T>& lhs,
255  const ReferencePtr<U>& rhs)
256 { return lhs.get() < rhs.get(); }
257 
262 template <class T>
263 inline bool operator<(const ReferencePtr<T>& lhs, std::nullptr_t)
264 { return false; }
265 
270 template <class T>
271 inline bool operator<(std::nullptr_t, const ReferencePtr<T>& rhs)
272 { return !rhs.empty(); }
273 
274 
275 // These functions are derived from operator== and operator<.
276 
279 template <class T, class U>
280 inline bool operator!=(const ReferencePtr<T>& lhs,
281  const ReferencePtr<U>& rhs)
282 { return !(lhs==rhs); }
283 
286 template <class T>
287 inline bool operator!=(const ReferencePtr<T>& lhs, std::nullptr_t)
288 { return !(lhs==nullptr); }
289 
292 template <class T>
293 inline bool operator!=(std::nullptr_t, const ReferencePtr<T>& rhs)
294 { return !(nullptr==rhs); }
295 
298 template <class T, class U>
299 inline bool operator>(const ReferencePtr<T>& lhs,
300  const ReferencePtr<U>& rhs)
301 { return rhs < lhs; }
304 template <class T>
305 inline bool operator>(const ReferencePtr<T>& lhs, std::nullptr_t)
306 { return nullptr < lhs; }
307 
310 template <class T>
311 inline bool operator>(std::nullptr_t, const ReferencePtr<T>& rhs)
312 { return rhs < nullptr; }
313 
314 
317 template <class T, class U>
318 inline bool operator>=(const ReferencePtr<T>& lhs,
319  const ReferencePtr<U>& rhs)
320 { return !(lhs < rhs); }
323 template <class T>
324 inline bool operator>=(const ReferencePtr<T>& lhs, std::nullptr_t)
325 { return !(lhs < nullptr); }
326 
329 template <class T>
330 inline bool operator>=(std::nullptr_t, const ReferencePtr<T>& rhs)
331 { return !(nullptr < rhs); }
332 
333 
337 template <class T, class U>
338 inline bool operator<=(const ReferencePtr<T>& lhs,
339  const ReferencePtr<U>& rhs)
340 { return !(rhs < lhs); }
344 template <class T>
345 inline bool operator<=(const ReferencePtr<T>& lhs, std::nullptr_t)
346 { return !(nullptr < lhs); }
350 template <class T>
351 inline bool operator<=(std::nullptr_t, const ReferencePtr<T>& rhs)
352 { return !(rhs < nullptr); }
353 
354 
357 template <class T>
358 DEPRECATED_14("use refptr != nullptr instead")
359 inline bool operator!=(const ReferencePtr<T>& lhs, int mustBeZero)
360 { assert(mustBeZero==0); return !(lhs==nullptr); }
361 
362 } // namespace SimTK
363 
364 #endif // SimTK_SimTKCOMMON_REFERENCE_PTR_H_
bool operator>=(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr greater-or-equal test defined as !(lhs < nullptr).
Definition: ReferencePtr.h:324
T & getRef() const
Return a reference to the target object.
Definition: ReferencePtr.h:139
T * get() const noexcept
Return the contained pointer, or null if the container is empty.
Definition: ReferencePtr.h:135
ReferencePtr & operator=(const ReferencePtr &src) noexcept
Copy assignment sets the pointer to nullptr (except for a self-assign); see class comments for why...
Definition: ReferencePtr.h:104
ReferencePtr() noexcept
Default constructor creates an empty object.
Definition: ReferencePtr.h:69
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
ReferencePtr & operator=(ReferencePtr &&src) noexcept
Move assignment copies the pointer from the source and leaves the source empty.
Definition: ReferencePtr.h:109
ReferencePtr & operator=(T *tp) noexcept
This form of assignment replaces the current pointer with the given one; no destruction occurs...
Definition: ReferencePtr.h:122
bool operator==(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Compare for equality the managed pointers contained in two compatible ReferencePtr containers...
Definition: ReferencePtr.h:231
T element_type
Type of the contained object.
Definition: ReferencePtr.h:62
~ReferencePtr() noexcept
Destructor does nothing.
Definition: ReferencePtr.h:128
void swap(ReferencePtr< T > &p1, ReferencePtr< T > &p2) noexcept
This is an overload of the STL std::swap() algorithm which uses the cheap built-in swap() member of t...
Definition: ReferencePtr.h:212
void swap(ReferencePtr &other) noexcept
Swap the contents of this ReferencePtr with another one.
Definition: ReferencePtr.h:164
bool operator>(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr greater-than test defined as nullptr < lhs.
Definition: ReferencePtr.h:305
ReferencePtr & operator=(T &t) noexcept
This form of assignment replaces the currently-referenced object by a reference to the source object;...
Definition: ReferencePtr.h:117
bool operator>(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr greater-than test defined as rhs < nullptr.
Definition: ReferencePtr.h:311
#define DEPRECATED_14(MSG)
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:289
bool operator!=(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr inequality test defined as !(nullptr==rhs).
Definition: ReferencePtr.h:293
bool empty() const noexcept
Return true if this container is empty.
Definition: ReferencePtr.h:169
ReferencePtr(const ReferencePtr &) noexcept
Copy constructor unconditionally sets the pointer to null; see class comments for why...
Definition: ReferencePtr.h:85
ReferencePtr(T *tp) noexcept
Construct from a given pointer stores the pointer.
Definition: ReferencePtr.h:77
bool operator!=(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr inequality test defined as !(lhs==nullptr).
Definition: ReferencePtr.h:287
T & operator*() const
The "dereference" operator returns a reference to the target object.
Definition: ReferencePtr.h:151
bool operator==(const ReferencePtr< T > &lhs, std::nullptr_t)
Comparison against nullptr; same as lhs.empty().
Definition: ReferencePtr.h:238
ReferencePtr(T &t) noexcept
Construct from a reference stores the address of the supplied object.
Definition: ReferencePtr.h:81
T & reference
Type of a reference to the contained object.
Definition: ReferencePtr.h:64
ReferencePtr(ReferencePtr &&src) noexcept
Move constructor copies the pointer from the source and leaves the source empty.
Definition: ReferencePtr.h:89
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
This is a smart pointer that implements "cross reference" semantics where a pointer data member of so...
Definition: ReferencePtr.h:60
Mandatory first inclusion for any Simbody source or header file.
T * pointer
Type of a pointer to the contained object.
Definition: ReferencePtr.h:63
ReferencePtr(std::nullptr_t) noexcept
Constructor from nullptr is the same as the default constructor.
Definition: ReferencePtr.h:74
bool operator>=(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr greater-or-equal test defined as !(nullptr < rhs).
Definition: ReferencePtr.h:330
void clear() noexcept
(Deprecated) Use reset() instead.
Definition: ReferencePtr.h:185
T * release() noexcept
Extract the pointer from this container, leaving the container empty.
Definition: ReferencePtr.h:177
bool operator!=(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer inequality test defined as !(lhs==rhs).
Definition: ReferencePtr.h:280
T * operator->() const
Return the contained pointer.
Definition: ReferencePtr.h:147
bool operator>(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer greater-than test defined as rhs < lhs.
Definition: ReferencePtr.h:299
bool operator==(std::nullptr_t, const ReferencePtr< T > &rhs)
Comparison against nullptr; same as rhs.empty().
Definition: ReferencePtr.h:244
bool operator>=(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer greater-or-equal test defined as !(lhs < rhs).
Definition: ReferencePtr.h:318
void reset(T *tp=nullptr) noexcept
Replace the stored pointer with a different one; no destruction occurs.
Definition: ReferencePtr.h:160