1 #ifndef SimTK_SimTKCOMMON_CLONE_ON_WRITE_PTR_H_ 2 #define SimTK_SimTKCOMMON_CLONE_ON_WRITE_PTR_H_ 82 {
if (x) {p=x; count=
new long(1);} }
109 { shareWith<U>(src); }
115 { moveFrom(std::move(src)); }
121 { moveFrom<U>(std::move(src)); }
135 {
reset(); shareWith(src); }
143 if (static_cast<T*>(src.p) != p)
144 {
reset(); shareWith<U>(src); }
156 {
reset(); moveFrom(std::move(src)); }
165 reset(); moveFrom<U>(std::move(src));
175 {
reset(cloneOrNull(&x));
return *
this; }
182 {
reset(x);
return *
this; }
200 const T*
get()
const noexcept {
return p;}
215 "An attempt was made to dereference a null pointer.");
224 "An attempt was made to dereference a null pointer.");
262 if (decr()==0) {
delete p;
delete count;}
274 if (x) {p=x; count=
new long(1);}
283 std::swap(p, other.p);
284 std::swap(count, other.count);
291 long use_count() const noexcept {
return count ? *count : 0;}
301 bool empty() const noexcept {
return !p;}
305 explicit operator bool() const noexcept {
return !
empty();}
316 T* save = p;
delete count; init();
331 { decr(); p=p->clone(); count=
new long(1); }
339 static T* cloneOrNull(
const T* src) {
340 return src ? src->clone() :
nullptr;
347 if (!src.empty()) {p=src.p; count=src.count; incr();}
352 template <
class U>
void moveFrom(CloneOnWritePtr<U>&& src) noexcept {
354 p=src.p; count=src.count; src.init();
358 long incr() const noexcept {assert(count && *count>=0);
return ++(*count);}
359 long decr() const noexcept {assert(count && *count>=1);
return --(*count);}
361 void init() noexcept {p=
nullptr; count=
nullptr;}
380 template <
class T>
inline void 388 template <
class charT,
class traits,
class T>
389 inline std::basic_ostream<charT,traits>&
390 operator<<(std::basic_ostream<charT,traits>& os,
392 { os << p.
get();
return os; }
399 template <
class T,
class U>
402 {
return lhs.
get() == rhs.
get(); }
408 {
return lhs.
empty(); }
414 {
return rhs.
empty(); }
422 template <
class T,
class U>
423 inline bool operator<(const CloneOnWritePtr<T>& lhs,
425 {
return lhs.
get() < rhs.get(); }
432 inline bool operator<(const CloneOnWritePtr<T>& lhs, std::nullptr_t)
440 inline bool operator<(std::nullptr_t, const CloneOnWritePtr<T>& rhs)
441 {
return !rhs.empty(); }
448 template <
class T,
class U>
451 {
return !(lhs==rhs); }
456 {
return !(lhs==
nullptr); }
461 {
return !(
nullptr==rhs); }
465 template <
class T,
class U>
468 {
return rhs < lhs; }
473 {
return nullptr < lhs; }
479 {
return rhs <
nullptr; }
484 template <
class T,
class U>
487 {
return !(lhs < rhs); }
492 {
return !(lhs <
nullptr); }
498 {
return !(
nullptr < rhs); }
504 template <
class T,
class U>
505 inline bool operator<=(const CloneOnWritePtr<T>& lhs,
507 {
return !(rhs < lhs); }
512 inline bool operator<=(const CloneOnWritePtr<T>& lhs, std::nullptr_t)
513 {
return !(
nullptr < lhs); }
518 inline bool operator<=(std::nullptr_t, const CloneOnWritePtr<T>& rhs)
519 {
return !(rhs <
nullptr); }
523 #endif // SimTK_SimTKCOMMON_CLONE_ON_WRITE_PTR_H_ T * operator->()
Clone if necessary, then dereference a writable pointer to the contained object.
Definition: CloneOnWritePtr.h:238
CloneOnWritePtr & operator=(const CloneOnWritePtr< U > &src) noexcept
Copy assignment from a compatible CloneOnWritePtr.
Definition: CloneOnWritePtr.h:142
Smart pointer with deep copy semantics but with the copying delayed until an attempt is made to write...
Definition: CloneOnWritePtr.h:59
T & updRef()
Clone if necessary to ensure the contained object is not shared, then return a writable reference to ...
Definition: CloneOnWritePtr.h:222
CloneOnWritePtr() noexcept
Default constructor stores a nullptr and sets use count to zero.
Definition: CloneOnWritePtr.h:71
CloneOnWritePtr(T *x)
Given a pointer to a writable heap-allocated object, take over ownership of that object.
Definition: CloneOnWritePtr.h:81
CloneOnWritePtr(const T *x)
Given a pointer to a read-only object, create a new heap-allocated copy of that object via its clone(...
Definition: CloneOnWritePtr.h:89
T * release()
(Advanced) Remove the contained object from management by this container and transfer ownership to th...
Definition: CloneOnWritePtr.h:314
const T & operator*() const
This "dereference" operator returns a const reference to the contained object.
Definition: CloneOnWritePtr.h:246
CloneOnWritePtr(const CloneOnWritePtr< U > &src) noexcept
Copy construction from a compatible CloneOnWritePtr.
Definition: CloneOnWritePtr.h:108
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
long use_count() const noexcept
Return count of how many CloneOnWritePtr objects are currently sharing the referenced object...
Definition: CloneOnWritePtr.h:291
bool operator>(std::nullptr_t, const CloneOnWritePtr< T > &rhs)
nullptr greater-than test defined as rhs < nullptr.
Definition: CloneOnWritePtr.h:478
CloneOnWritePtr & operator=(const CloneOnWritePtr &src) noexcept
Copy assignment replaces the currently-held object by a deferred copy of the object held in the sourc...
Definition: CloneOnWritePtr.h:133
bool operator!=(const CloneOnWritePtr< T > &lhs, std::nullptr_t)
nullptr inequality test defined as !(lhs==nullptr).
Definition: CloneOnWritePtr.h:455
CloneOnWritePtr(const T &x)
Given a read-only reference to an object, create a new heap-allocated copy of that object via its clo...
Definition: CloneOnWritePtr.h:95
bool empty() const noexcept
Return true if this container is empty, which is the state the container is in immediately after defa...
Definition: CloneOnWritePtr.h:301
~CloneOnWritePtr() noexcept
Destructor decrements the reference count and deletes the object if the count goes to zero...
Definition: CloneOnWritePtr.h:189
void detach()
(Advanced) Force the contained object to be unique, that is, not shared with any other container...
Definition: CloneOnWritePtr.h:329
bool operator>=(const CloneOnWritePtr< T > &lhs, const CloneOnWritePtr< U > &rhs)
Pointer greater-or-equal test defined as !(lhs < rhs).
Definition: CloneOnWritePtr.h:485
const T * operator->() const
Dereference a const pointer to the contained object.
Definition: CloneOnWritePtr.h:234
CloneOnWritePtr(std::nullptr_t) noexcept
Constructor from nullptr is the same as the default constructor.
Definition: CloneOnWritePtr.h:76
CloneOnWritePtr(CloneOnWritePtr &&src) noexcept
Move constructor is very fast and leaves the source empty.
Definition: CloneOnWritePtr.h:114
void reset(T *x)
Replace the contents of this container with the supplied heap-allocated object, taking over ownership...
Definition: CloneOnWritePtr.h:271
void swap(CloneOnWritePtr &other) noexcept
Swap the contents of this CloneOnWritePtr with another one, with ownership changing hands but no copy...
Definition: CloneOnWritePtr.h:282
void swap(CloneOnWritePtr< T > &p1, CloneOnWritePtr< T > &p2)
This is an overload of the STL std::swap() algorithm which uses the cheap built-in swap() member of t...
Definition: CloneOnWritePtr.h:381
CloneOnWritePtr(const CloneOnWritePtr &src) noexcept
Copy constructor is deep but deferred so very fast here; the new CloneOnWritePtr object initially sha...
Definition: CloneOnWritePtr.h:102
bool operator>=(const CloneOnWritePtr< T > &lhs, std::nullptr_t)
nullptr greater-or-equal test defined as !(lhs < nullptr).
Definition: CloneOnWritePtr.h:491
const T * get() const noexcept
Return a const pointer to the contained object if any, or nullptr.
Definition: CloneOnWritePtr.h:200
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
T * upd()
Clone if necessary to ensure the contained object is not shared, then return a writable pointer to th...
Definition: CloneOnWritePtr.h:208
bool operator==(std::nullptr_t, const CloneOnWritePtr< T > &rhs)
Comparison against nullptr; same as rhs.empty().
Definition: CloneOnWritePtr.h:413
CloneOnWritePtr & operator=(CloneOnWritePtr< U > &&src) noexcept
Move assignment from a compatible CloneOnWritePtr.
Definition: CloneOnWritePtr.h:163
T element_type
Type of the contained object.
Definition: CloneOnWritePtr.h:61
bool operator>(const CloneOnWritePtr< T > &lhs, const CloneOnWritePtr< U > &rhs)
Pointer greater-than test defined as rhs < lhs.
Definition: CloneOnWritePtr.h:466
Mandatory first inclusion for any Simbody source or header file.
bool operator>=(std::nullptr_t, const CloneOnWritePtr< T > &rhs)
nullptr greater-or-equal test defined as !(nullptr < rhs).
Definition: CloneOnWritePtr.h:497
bool operator==(const CloneOnWritePtr< T > &lhs, const CloneOnWritePtr< U > &rhs)
Compare for equality the managed pointers contained in two compatible CloneOnWritePtr containers...
Definition: CloneOnWritePtr.h:400
bool operator!=(std::nullptr_t, const CloneOnWritePtr< T > &rhs)
nullptr inequality test defined as !(nullptr==rhs).
Definition: CloneOnWritePtr.h:460
const T & getRef() const
Return a const reference to the contained object.
Definition: CloneOnWritePtr.h:213
bool unique() const noexcept
Is this the only user of the referenced object? Note that this means there is exactly one; if the man...
Definition: CloneOnWritePtr.h:296
CloneOnWritePtr & operator=(CloneOnWritePtr &&src) noexcept
Move assignment replaces the currently-held object by the source object, leaving the source empty...
Definition: CloneOnWritePtr.h:154
CloneOnWritePtr(CloneOnWritePtr< U > &&src) noexcept
Move construction from a compatible CloneOnWritePtr.
Definition: CloneOnWritePtr.h:120
bool operator>(const CloneOnWritePtr< T > &lhs, std::nullptr_t)
nullptr greater-than test defined as nullptr < lhs.
Definition: CloneOnWritePtr.h:472
T & operator*()
Clone if necessary, then return a writable reference to the contained object.
Definition: CloneOnWritePtr.h:250
T & reference
Type of a reference to the contained object.
Definition: CloneOnWritePtr.h:63
bool operator==(const CloneOnWritePtr< T > &lhs, std::nullptr_t)
Comparison against nullptr; same as lhs.empty().
Definition: CloneOnWritePtr.h:407
void reset() noexcept
Make this container empty, decrementing the use count of the contained object (if any)...
Definition: CloneOnWritePtr.h:260
T * pointer
Type of a pointer to the contained object.
Definition: CloneOnWritePtr.h:62
bool operator!=(const CloneOnWritePtr< T > &lhs, const CloneOnWritePtr< U > &rhs)
Pointer inequality test defined as !(lhs==rhs).
Definition: CloneOnWritePtr.h:449
CloneOnWritePtr & operator=(T *x) noexcept
This form of assignment replaces the currently-held object by the given source object and takes over ...
Definition: CloneOnWritePtr.h:181
CloneOnWritePtr & operator=(const T &x)
This form of assignment replaces the currently-held object by a heap-allocated copy of the source obj...
Definition: CloneOnWritePtr.h:174