1 #ifndef SimTK_SimTKCOMMON_CLONE_PTR_H_ 2 #define SimTK_SimTKCOMMON_CLONE_PTR_H_ 125 assert((p != src.p) || !p);
126 reset(cloneOrNull(src.p));
142 assert((p != static_cast<const T*>(src.p)) || !p);
144 reset(cloneOrNull(src.p));
154 assert((p != src.p) || !p);
155 reset(src.p); src.p =
nullptr;
169 assert((p != static_cast<const T*>(src.p)) || !p);
170 reset(src.p); src.p =
nullptr;
178 {
reset(cloneOrNull(&x));
return *
this; }
184 {
reset(x);
return *
this; }
202 const T*
get()
const noexcept {
return p;}
208 T*
upd() noexcept {
return p;}
215 "An attempt was made to dereference a null pointer.");
223 "An attempt was made to dereference a null pointer.");
272 std::swap(p, other.p);
278 bool empty() const noexcept {
return !p;}
282 explicit operator bool() const noexcept {
return !
empty();}
296 const T*
getPtr() const noexcept {
return get();}
311 static T* cloneOrNull(
const T* src) {
312 return src ? src->clone() :
nullptr;
331 template <
class T>
inline void 339 template <
class charT,
class traits,
class T>
340 inline std::basic_ostream<charT,traits>&
341 operator<<(std::basic_ostream<charT,traits>& os,
343 { os << p.
get();
return os; }
350 template <
class T,
class U>
353 {
return lhs.
get() == rhs.
get(); }
359 {
return lhs.
empty(); }
365 {
return rhs.
empty(); }
373 template <
class T,
class U>
374 inline bool operator<(const ClonePtr<T>& lhs,
376 {
return lhs.
get() < rhs.get(); }
383 inline bool operator<(const ClonePtr<T>& lhs, std::nullptr_t)
391 inline bool operator<(std::nullptr_t, const ClonePtr<T>& rhs)
392 {
return !rhs.empty(); }
399 template <
class T,
class U>
402 {
return !(lhs==rhs); }
407 {
return !(lhs==
nullptr); }
412 {
return !(
nullptr==rhs); }
416 template <
class T,
class U>
419 {
return rhs < lhs; }
424 {
return nullptr < lhs; }
430 {
return rhs <
nullptr; }
435 template <
class T,
class U>
438 {
return !(lhs < rhs); }
443 {
return !(lhs <
nullptr); }
449 {
return !(
nullptr < rhs); }
455 template <
class T,
class U>
456 inline bool operator<=(const ClonePtr<T>& lhs,
458 {
return !(rhs < lhs); }
463 inline bool operator<=(const ClonePtr<T>& lhs, std::nullptr_t)
464 {
return !(
nullptr < lhs); }
469 inline bool operator<=(std::nullptr_t, const ClonePtr<T>& rhs)
470 {
return !(rhs <
nullptr); }
474 #endif // SimTK_SimTKCOMMON_CLONE_PTR_H_ T * upd() noexcept
Return a writable pointer to the contained object if any, or nullptr.
Definition: ClonePtr.h:208
void reset() noexcept
Make this container empty if it isn't already, destructing the contained object if there is one...
Definition: ClonePtr.h:251
bool operator>(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr greater-than test defined as nullptr < lhs.
Definition: ClonePtr.h:423
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
void reset(T *x) noexcept
Replace the contents of this container with the supplied heap-allocated object, taking over ownership...
Definition: ClonePtr.h:260
ClonePtr(ClonePtr &&src) noexcept
Move constructor is very fast and leaves the source empty.
Definition: ClonePtr.h:105
ClonePtr(const ClonePtr< U > &src)
Deep copy construction from a compatible ClonePtr.
Definition: ClonePtr.h:100
~ClonePtr() noexcept
Destructor deletes the contained object.
Definition: ClonePtr.h:191
bool operator!=(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr inequality test defined as !(lhs==nullptr).
Definition: ClonePtr.h:406
ClonePtr & operator=(const ClonePtr &src)
Copy assignment replaces the currently-held object by a copy of the object held in the source contain...
Definition: ClonePtr.h:123
bool operator>=(std::nullptr_t, const ClonePtr< T > &rhs)
nullptr greater-or-equal test defined as !(nullptr < rhs).
Definition: ClonePtr.h:448
ClonePtr & operator=(ClonePtr &&src) noexcept
Move assignment replaces the currently-held object by the source object, leaving the source empty...
Definition: ClonePtr.h:152
ClonePtr(ClonePtr< U > &&src) noexcept
Move construction from a compatible ClonePtr.
Definition: ClonePtr.h:112
void swap(ClonePtr< T > &p1, ClonePtr< T > &p2) noexcept
This is an overload of the STL std::swap() algorithm which uses the cheap built-in swap() member of t...
Definition: ClonePtr.h:332
bool operator>(std::nullptr_t, const ClonePtr< T > &rhs)
nullptr greater-than test defined as rhs < nullptr.
Definition: ClonePtr.h:429
#define DEPRECATED_14(MSG)
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:289
T * release() noexcept
Remove the contained object from management by this container and transfer ownership to the caller...
Definition: ClonePtr.h:287
T & updRef()
Return a writable reference to the contained object.
Definition: ClonePtr.h:221
ClonePtr(const T &x)
Given a read-only reference to an object, create a new heap-allocated copy of that object via its clo...
Definition: ClonePtr.h:88
bool operator==(std::nullptr_t, const ClonePtr< T > &rhs)
Comparison against nullptr; same as rhs.empty().
Definition: ClonePtr.h:364
ClonePtr & operator=(const T &x)
This form of assignment replaces the currently-held object by a heap-allocated copy of the source obj...
Definition: ClonePtr.h:177
T element_type
Type of the contained object.
Definition: ClonePtr.h:57
void clear() noexcept
(Deprecated) Use reset() instead.
Definition: ClonePtr.h:303
bool operator!=(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer inequality test defined as !(lhs==rhs).
Definition: ClonePtr.h:400
ClonePtr & operator=(ClonePtr< U > &&src) noexcept
Move assignment from a compatible ClonePtr replaces the currently-held object by the source object...
Definition: ClonePtr.h:165
bool operator!=(std::nullptr_t, const ClonePtr< T > &rhs)
nullptr inequality test defined as !(nullptr==rhs).
Definition: ClonePtr.h:411
Smart pointer with deep copy semantics.
Definition: ClonePtr.h:55
T & operator*()
Return a writable reference to the contained object.
Definition: ClonePtr.h:241
bool operator>=(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer greater-or-equal test defined as !(lhs < rhs).
Definition: ClonePtr.h:436
ClonePtr(const ClonePtr &src)
Copy constructor is deep; the new ClonePtr object contains a new copy of the object in the source...
Definition: ClonePtr.h:93
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
void swap(ClonePtr &other) noexcept
Swap the contents of this ClonePtr with another one, with ownership changing hands but no copying per...
Definition: ClonePtr.h:271
ClonePtr(T *x) noexcept
Given a pointer to a writable heap-allocated object, take over ownership of that object.
Definition: ClonePtr.h:76
Mandatory first inclusion for any Simbody source or header file.
ClonePtr & operator=(T *x) noexcept
This form of assignment replaces the currently-held object by the given source object and takes over ...
Definition: ClonePtr.h:183
ClonePtr(std::nullptr_t) noexcept
Constructor from nullptr is the same as the default constructor.
Definition: ClonePtr.h:72
bool operator==(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Compare for equality the managed pointers contained in two compatible ClonePtr containers.
Definition: ClonePtr.h:351
const T & getRef() const
Return a const reference to the contained object.
Definition: ClonePtr.h:213
T * operator->()
Dereference a writable pointer to the contained object.
Definition: ClonePtr.h:233
bool empty() const noexcept
Return true if this container is empty, which is the state the container is in immediately after defa...
Definition: ClonePtr.h:278
ClonePtr() noexcept
Default constructor stores a nullptr.
Definition: ClonePtr.h:67
bool operator>(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer greater-than test defined as rhs < lhs.
Definition: ClonePtr.h:417
const T * operator->() const
Dereference a const pointer to the contained object.
Definition: ClonePtr.h:229
const T * getPtr() const noexcept
(Deprecated) Same as get().
Definition: ClonePtr.h:296
bool operator==(const ClonePtr< T > &lhs, std::nullptr_t)
Comparison against nullptr; same as lhs.empty().
Definition: ClonePtr.h:358
T * pointer
Type of a pointer to the contained object.
Definition: ClonePtr.h:58
T * updPtr() noexcept
(Deprecated) Same as upd().
Definition: ClonePtr.h:300
const T & operator*() const
This "dereference" operator returns a const reference to the contained object.
Definition: ClonePtr.h:237
T & reference
Type of a reference to the contained object.
Definition: ClonePtr.h:59
bool operator>=(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr greater-or-equal test defined as !(lhs < nullptr).
Definition: ClonePtr.h:442
ClonePtr & operator=(const ClonePtr< U > &src)
Copy assignment from a compatible ClonePtr.
Definition: ClonePtr.h:138
ClonePtr(const T *x)
Given a pointer to a read-only object, create a new heap-allocated copy of that object via its clone(...
Definition: ClonePtr.h:82
const T * get() const noexcept
Return a const pointer to the contained object if any, or nullptr.
Definition: ClonePtr.h:202