8 #ifndef Sawyer_SharedPtr_H
9 #define Sawyer_SharedPtr_H
11 #include <Sawyer/Assert.h>
12 #include <Sawyer/Optional.h>
13 #include <Sawyer/Sawyer.h>
14 #include <Sawyer/SharedObject.h>
15 #include <Sawyer/Synchronization.h>
17 #include <boost/serialization/access.hpp>
18 #include <boost/serialization/nvp.hpp>
19 #include <boost/serialization/split_member.hpp>
74 static void acquireOwnership(Pointee *rawPtr);
77 static size_t releaseOwnership(Pointee *rawPtr);
80 friend class boost::serialization::access;
83 void save(S &s,
const unsigned )
const {
84 s << BOOST_SERIALIZATION_NVP(pointee_);
88 void load(S &s,
const unsigned ) {
89 if (pointee_ !=
nullptr && 0==releaseOwnership(pointee_))
92 s >> BOOST_SERIALIZATION_NVP(pointee_);
93 acquireOwnership(pointee_);
96 BOOST_SERIALIZATION_SPLIT_MEMBER();
106 acquireOwnership(pointee_);
110 acquireOwnership(pointee_);
121 if (pointee_ !=
nullptr)
122 acquireOwnership(pointee_);
127 if (0==releaseOwnership(pointee_))
135 return operator=<T>(other);
140 if (pointee_ !=
nullptr && 0 == releaseOwnership(pointee_))
143 acquireOwnership(pointee_);
151 if (pointee_ !=
nullptr && 0 == releaseOwnership(pointee_))
160 ASSERT_not_null2(pointee_,
"shared pointer points to no object");
167 ASSERT_not_null2(pointee_,
"shared pointer points to no object");
218 return pointee_ == ptr;
222 return pointee_ != ptr;
226 return pointee_ < ptr;
230 return pointee_ <= ptr;
234 return pointee_ > ptr;
238 return pointee_ >= ptr;
264 void this_type_does_not_support_comparisons()
const {}
277 operator unspecified_bool()
const {
278 return pointee_ ? &SharedPointer::this_type_does_not_support_comparisons :
nullptr;
315 template<
class Po
inter>
316 typename Pointer::Pointee*
317 getRawPointer(Pointer& ptr) {
318 return ptr.getRawPointer();
374 T *derived =
dynamic_cast<T*
>(
this);
375 ASSERT_not_null(derived);
379 const T *derived =
dynamic_cast<const T*
>(
this);
380 ASSERT_not_null(derived);
393 SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
394 return rawPtr->SharedObject::nrefs_;
400 inline void SharedPointer<T>::acquireOwnership(Pointee *rawPtr) {
401 if (rawPtr !=
nullptr) {
402 SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
403 ++rawPtr->SharedObject::nrefs_;
408 inline size_t SharedPointer<T>::releaseOwnership(Pointee *rawPtr) {
409 if (rawPtr !=
nullptr) {
410 SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
411 assert(rawPtr->SharedObject::nrefs_ > 0);
412 return --rawPtr->SharedObject::nrefs_;
bool operator>=(const U *ptr) const
Comparison of two pointers.
SharedPointer< const T > sharedFromThis() const
Create a shared pointer from this.
bool operator<=(const SharedPointer< U > &other) const
Comparison of two pointers.
void clear(SharedPointer< T > &ptr)
Make pointer point to nothing.
SharedPointer(Y *rawPtr)
Constructs a shared pointer for an object.
SharedPointer()
Constructs an empty shared pointer.
bool operator==(const SharedPointer< U > &other) const
Comparison of two pointers.
SharedPointer< T > sharedFromThis()
Create a shared pointer from this.
bool operator!=(const SharedPointer< U > &other) const
Comparison of two pointers.
bool operator==(const U *ptr) const
Comparison of two pointers.
bool operator<=(const U *ptr) const
Comparison of two pointers.
~SharedPointer()
Conditionally deletes the pointed-to object.
bool operator<(const U *ptr) const
Comparison of two pointers.
bool operator!=(const U *ptr) const
Comparison of two pointers.
bool operator>(const SharedPointer< U > &other) const
Comparison of two pointers.
bool operator>=(const SharedPointer< U > &other) const
Comparison of two pointers.
bool operator>(const U *ptr) const
Comparison of two pointers.
Reference-counting intrusive smart pointer.
SharedPointer< U > dynamicCast() const
Dynamic cast.
Name space for the entire library.
SharedPointer & operator=(const Sawyer::Nothing &)
Assignment.
SharedPointer(const SharedPointer &other)
Constructs a new pointer that shares ownership of the pointed-to object with the other pointer.
bool operator!() const
Boolean complement.
Creates SharedPointer from this.
SharedPointer & operator=(const SharedPointer< Y > &other)
Assignment.
friend std::ostream & operator<<(std::ostream &out, const SharedPointer &ptr)
Print a shared pointer.
T * operator->() const
Dereference pointed-to object.
bool operator<(const SharedPointer< U > &other) const
Comparison of two pointers.
SharedPointer & operator=(const SharedPointer &other)
Assignment.
friend size_t ownershipCount(const SharedPointer &ptr)
Returns the pointed-to object's reference count.
Pointee * getRawPointer()
Obtain the pointed-to object.
T & operator*() const
Reference to the pointed-to object.
SharedPointer(const SharedPointer< Y > &other)
Constructs a new pointer that shares ownership of the pointed-to object with the other pointer.