Simbody 3.7
Loading...
Searching...
No Matches
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
31namespace SimTK {
32
60template <class T> class ReferencePtr {
61public:
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
194private:
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
211template <class T> inline void
212swap(ReferencePtr<T>& p1, ReferencePtr<T>& p2) noexcept {
213 p1.swap(p2);
214}
215
219template <class charT, class traits, class T>
220inline std::basic_ostream<charT,traits>&
221operator<<(std::basic_ostream<charT,traits>& os,
222 const ReferencePtr<T>& p)
223{ os << p.get(); return os; }
224
230template <class T, class U>
231inline bool operator==(const ReferencePtr<T>& lhs,
232 const ReferencePtr<U>& rhs)
233{ return lhs.get() == rhs.get(); }
234
237template <class T>
238inline bool operator==(const ReferencePtr<T>& lhs, std::nullptr_t)
239{ return lhs.empty(); }
240
243template <class T>
244inline bool operator==(std::nullptr_t, const ReferencePtr<T>& rhs)
245{ return rhs.empty(); }
246
253template <class T, class U>
254inline bool operator<(const ReferencePtr<T>& lhs,
255 const ReferencePtr<U>& rhs)
256{ return lhs.get() < rhs.get(); }
257
262template <class T>
263inline bool operator<(const ReferencePtr<T>& lhs, std::nullptr_t)
264{ return false; }
265
270template <class T>
271inline 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
279template <class T, class U>
280inline bool operator!=(const ReferencePtr<T>& lhs,
281 const ReferencePtr<U>& rhs)
282{ return !(lhs==rhs); }
283
286template <class T>
287inline bool operator!=(const ReferencePtr<T>& lhs, std::nullptr_t)
288{ return !(lhs==nullptr); }
289
292template <class T>
293inline bool operator!=(std::nullptr_t, const ReferencePtr<T>& rhs)
294{ return !(nullptr==rhs); }
295
298template <class T, class U>
299inline bool operator>(const ReferencePtr<T>& lhs,
300 const ReferencePtr<U>& rhs)
301{ return rhs < lhs; }
304template <class T>
305inline bool operator>(const ReferencePtr<T>& lhs, std::nullptr_t)
306{ return nullptr < lhs; }
307
310template <class T>
311inline bool operator>(std::nullptr_t, const ReferencePtr<T>& rhs)
312{ return rhs < nullptr; }
313
314
317template <class T, class U>
318inline bool operator>=(const ReferencePtr<T>& lhs,
319 const ReferencePtr<U>& rhs)
320{ return !(lhs < rhs); }
323template <class T>
324inline bool operator>=(const ReferencePtr<T>& lhs, std::nullptr_t)
325{ return !(lhs < nullptr); }
326
329template <class T>
330inline bool operator>=(std::nullptr_t, const ReferencePtr<T>& rhs)
331{ return !(nullptr < rhs); }
332
333
337template <class T, class U>
338inline bool operator<=(const ReferencePtr<T>& lhs,
339 const ReferencePtr<U>& rhs)
340{ return !(rhs < lhs); }
344template <class T>
345inline bool operator<=(const ReferencePtr<T>& lhs, std::nullptr_t)
346{ return !(nullptr < lhs); }
350template <class T>
351inline bool operator<=(std::nullptr_t, const ReferencePtr<T>& rhs)
352{ return !(rhs < nullptr); }
353
354
357template <class T>
358DEPRECATED_14("use refptr != nullptr instead")
359inline 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_
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition ExceptionMacros.h:324
Mandatory first inclusion for any Simbody source or header file.
#define DEPRECATED_14(MSG)
Definition SimTKcommon/include/SimTKcommon/internal/common.h:289
This is a smart pointer that implements "cross reference" semantics where a pointer data member of so...
Definition ReferencePtr.h:60
ReferencePtr & operator=(T *tp) noexcept
This form of assignment replaces the current pointer with the given one; no destruction occurs.
Definition ReferencePtr.h:122
void reset(T *tp=nullptr) noexcept
Replace the stored pointer with a different one; no destruction occurs.
Definition ReferencePtr.h:160
bool operator>=(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer greater-or-equal test defined as !(lhs < rhs).
Definition ReferencePtr.h:318
bool operator<(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Less-than operator for two compatible ReferencePtr containers, comparing the pointers,...
Definition ReferencePtr.h:254
bool operator>(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer greater-than test defined as rhs < lhs.
Definition ReferencePtr.h:299
bool operator<(const ReferencePtr< T > &lhs, std::nullptr_t)
Less-than comparison against a nullptr.
Definition ReferencePtr.h:263
bool operator<(std::nullptr_t, const ReferencePtr< T > &rhs)
Less-than comparison of a nullptr against this container.
Definition ReferencePtr.h:271
ReferencePtr(T *tp) noexcept
Construct from a given pointer stores the pointer.
Definition ReferencePtr.h:77
ReferencePtr(T &t) noexcept
Construct from a reference stores the address of the supplied object.
Definition ReferencePtr.h:81
T & getRef() const
Return a reference to the target object.
Definition ReferencePtr.h:139
bool operator==(const ReferencePtr< T > &lhs, std::nullptr_t)
Comparison against nullptr; same as lhs.empty().
Definition ReferencePtr.h:238
bool operator!=(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr inequality test defined as !(lhs==nullptr).
Definition ReferencePtr.h:287
bool operator<=(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr less-or-equal test defined as !(nullptr < lhs) (note reversed arguments).
Definition ReferencePtr.h:345
void clear() noexcept
(Deprecated) Use reset() instead.
Definition ReferencePtr.h:185
bool operator!=(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer inequality test defined as !(lhs==rhs).
Definition ReferencePtr.h:280
ReferencePtr(std::nullptr_t) noexcept
Constructor from nullptr is the same as the default constructor.
Definition ReferencePtr.h:74
bool operator>=(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr greater-or-equal test defined as !(lhs < nullptr).
Definition ReferencePtr.h:324
T * get() const noexcept
Return the contained pointer, or null if the container is empty.
Definition ReferencePtr.h:135
T & operator*() const
The "dereference" operator returns a reference to the target object.
Definition ReferencePtr.h:151
bool operator!=(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr inequality test defined as !(nullptr==rhs).
Definition ReferencePtr.h:293
bool operator>(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr greater-than test defined as rhs < nullptr.
Definition ReferencePtr.h:311
bool operator>=(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr greater-or-equal test defined as !(nullptr < rhs).
Definition ReferencePtr.h:330
~ReferencePtr() noexcept
Destructor does nothing.
Definition ReferencePtr.h:128
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
void swap(ReferencePtr &other) noexcept
Swap the contents of this ReferencePtr with another one.
Definition ReferencePtr.h:164
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 &t) noexcept
This form of assignment replaces the currently-referenced object by a reference to the source object;...
Definition ReferencePtr.h:117
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 & reference
Type of a reference to the contained object.
Definition ReferencePtr.h:64
bool operator>(const ReferencePtr< T > &lhs, std::nullptr_t)
nullptr greater-than test defined as nullptr < lhs.
Definition ReferencePtr.h:305
ReferencePtr(ReferencePtr &&src) noexcept
Move constructor copies the pointer from the source and leaves the source empty.
Definition ReferencePtr.h:89
bool operator<=(const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs)
Pointer less-or-equal test defined as !(rhs < lhs) (note reversed arguments).
Definition ReferencePtr.h:338
ReferencePtr(const ReferencePtr &) noexcept
Copy constructor unconditionally sets the pointer to null; see class comments for why.
Definition ReferencePtr.h:85
T element_type
Type of the contained object.
Definition ReferencePtr.h:62
T * operator->() const
Return the contained pointer.
Definition ReferencePtr.h:147
T * release() noexcept
Extract the pointer from this container, leaving the container empty.
Definition ReferencePtr.h:177
ReferencePtr() noexcept
Default constructor creates an empty object.
Definition ReferencePtr.h:69
bool empty() const noexcept
Return true if this container is empty.
Definition ReferencePtr.h:169
bool operator<=(std::nullptr_t, const ReferencePtr< T > &rhs)
nullptr less-or-equal test defined as !(rhs < nullptr) (note reversed arguments).
Definition ReferencePtr.h:351
bool operator==(std::nullptr_t, const ReferencePtr< T > &rhs)
Comparison against nullptr; same as rhs.empty().
Definition ReferencePtr.h:244
T * pointer
Type of a pointer to the contained object.
Definition ReferencePtr.h:63
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition Assembler.h:37
std::ostream & operator<<(std::ostream &o, const ContactForce &f)
Definition CompliantContactSubsystem.h:387