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();}
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>
376 {
return lhs.
get() < rhs.
get(); }
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>
458 {
return !(rhs < lhs); }
464 {
return !(
nullptr < lhs); }
470 {
return !(rhs <
nullptr); }
#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
Smart pointer with deep copy semantics.
Definition: ClonePtr.h:55
T * updPtr() noexcept
(Deprecated) Same as upd().
Definition: ClonePtr.h:300
bool operator<=(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer less-or-equal test defined as !(rhs < lhs) (note reversed arguments).
Definition: ClonePtr.h:456
bool operator>(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer greater-than test defined as rhs < lhs.
Definition: ClonePtr.h:417
T element_type
Type of the contained object.
Definition: ClonePtr.h:57
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
const T * get() const noexcept
Return a const pointer to the contained object if any, or nullptr.
Definition: ClonePtr.h:202
T & reference
Type of a reference to the contained object.
Definition: ClonePtr.h:59
ClonePtr(const ClonePtr< U > &src)
Deep copy construction from a compatible ClonePtr.
Definition: ClonePtr.h:100
ClonePtr(T *x) noexcept
Given a pointer to a writable heap-allocated object, take over ownership of that object.
Definition: ClonePtr.h:76
const T & getRef() const
Return a const reference to the contained object.
Definition: ClonePtr.h:213
ClonePtr() noexcept
Default constructor stores a nullptr.
Definition: ClonePtr.h:67
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
bool operator<=(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr less-or-equal test defined as !(nullptr < lhs) (note reversed arguments).
Definition: ClonePtr.h:463
bool operator<(std::nullptr_t, const ClonePtr< T > &rhs)
Less-than comparison of a nullptr against this container.
Definition: ClonePtr.h:391
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
bool operator>=(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer greater-or-equal test defined as !(lhs < rhs).
Definition: ClonePtr.h:436
const T * getPtr() const noexcept
(Deprecated) Same as get().
Definition: ClonePtr.h:296
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
void clear() noexcept
(Deprecated) Use reset() instead.
Definition: ClonePtr.h:303
bool operator!=(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr inequality test defined as !(lhs==nullptr).
Definition: ClonePtr.h:406
ClonePtr(ClonePtr &&src) noexcept
Move constructor is very fast and leaves the source empty.
Definition: ClonePtr.h:105
T & updRef()
Return a writable reference to the contained object.
Definition: ClonePtr.h:221
bool operator==(std::nullptr_t, const ClonePtr< T > &rhs)
Comparison against nullptr; same as rhs.empty().
Definition: ClonePtr.h:364
const T & operator*() const
This "dereference" operator returns a const reference to the contained object.
Definition: ClonePtr.h:237
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
~ClonePtr() noexcept
Destructor deletes the contained object.
Definition: ClonePtr.h:191
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(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
bool operator>=(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr greater-or-equal test defined as !(lhs < nullptr).
Definition: ClonePtr.h:442
bool operator>(std::nullptr_t, const ClonePtr< T > &rhs)
nullptr greater-than test defined as rhs < nullptr.
Definition: ClonePtr.h:429
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(ClonePtr< U > &&src) noexcept
Move construction from a compatible ClonePtr.
Definition: ClonePtr.h:112
bool operator<(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Less-than operator for two compatible ClonePtr containers, comparing the pointers,...
Definition: ClonePtr.h:374
T * release() noexcept
Remove the contained object from management by this container and transfer ownership to the caller.
Definition: ClonePtr.h:287
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)
Comparison against nullptr; same as lhs.empty().
Definition: ClonePtr.h:358
bool operator!=(const ClonePtr< T > &lhs, const ClonePtr< U > &rhs)
Pointer inequality test defined as !(lhs==rhs).
Definition: ClonePtr.h:400
void reset(T *x) noexcept
Replace the contents of this container with the supplied heap-allocated object, taking over ownership...
Definition: ClonePtr.h:260
T * operator->()
Dereference a writable pointer to the contained object.
Definition: ClonePtr.h:233
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
bool operator!=(std::nullptr_t, const ClonePtr< T > &rhs)
nullptr inequality test defined as !(nullptr==rhs).
Definition: ClonePtr.h:411
T & operator*()
Return a writable reference to the contained object.
Definition: ClonePtr.h:241
const T * operator->() const
Dereference a const pointer to the contained object.
Definition: ClonePtr.h:229
ClonePtr(std::nullptr_t) noexcept
Constructor from nullptr is the same as the default constructor.
Definition: ClonePtr.h:72
ClonePtr & operator=(const ClonePtr< U > &src)
Copy assignment from a compatible ClonePtr.
Definition: ClonePtr.h:138
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
bool operator>(const ClonePtr< T > &lhs, std::nullptr_t)
nullptr greater-than test defined as nullptr < lhs.
Definition: ClonePtr.h:423
T * upd() noexcept
Return a writable pointer to the contained object if any, or nullptr.
Definition: ClonePtr.h:208
bool operator<=(std::nullptr_t, const ClonePtr< T > &rhs)
nullptr less-or-equal test defined as !(rhs < nullptr) (note reversed arguments).
Definition: ClonePtr.h:469
bool operator<(const ClonePtr< T > &lhs, std::nullptr_t)
Less-than comparison against a nullptr.
Definition: ClonePtr.h:383
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
T * pointer
Type of a pointer to the contained object.
Definition: ClonePtr.h:58
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