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();
103 : pointee_(nullptr) {}
105 : pointee_(nullptr) {}
107 : pointee_(nullptr) {}
114 acquireOwnership(pointee_);
118 acquireOwnership(pointee_);
129 if (pointee_ !=
nullptr)
130 acquireOwnership(pointee_);
135 if (0==releaseOwnership(pointee_))
143 return operator=<T>(other);
148 if (pointee_ !=
nullptr && 0 == releaseOwnership(pointee_))
151 acquireOwnership(pointee_);
161 if (pointee_ !=
nullptr && 0 == releaseOwnership(pointee_))
167 if (pointee_ !=
nullptr && 0 == releaseOwnership(pointee_))
177 ASSERT_not_null2(pointee_,
"shared pointer points to no object");
184 ASSERT_not_null2(pointee_,
"shared pointer points to no object");
212 bool operator<(const SharedPointer<U> &other)
const {
213 return pointee_ < other.getRawPointer();
216 bool operator<=(const SharedPointer<U> &other)
const {
217 return pointee_ <= other.getRawPointer();
235 return pointee_ == ptr;
239 return pointee_ != ptr;
243 return pointee_ < ptr;
247 return pointee_ <= ptr;
251 return pointee_ > ptr;
255 return pointee_ >= ptr;
281 void this_type_does_not_support_comparisons()
const {}
294 operator unspecified_bool()
const {
295 return pointee_ ? &SharedPointer::this_type_does_not_support_comparisons :
nullptr;
332 template<
class Po
inter>
333 typename Pointer::Pointee*
334 getRawPointer(Pointer& ptr) {
335 return ptr.getRawPointer();
391 T *derived =
dynamic_cast<T*
>(
this);
392 ASSERT_not_null(derived);
396 const T *derived =
dynamic_cast<const T*
>(
this);
397 ASSERT_not_null(derived);
410 SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
411 return rawPtr->SharedObject::nrefs_;
417 inline void SharedPointer<T>::acquireOwnership(Pointee *rawPtr) {
418 if (rawPtr !=
nullptr) {
419 SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
420 ++rawPtr->SharedObject::nrefs_;
425 inline size_t SharedPointer<T>::releaseOwnership(Pointee *rawPtr) {
426 if (rawPtr !=
nullptr) {
427 SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
428 assert(rawPtr->SharedObject::nrefs_ > 0);
429 return --rawPtr->SharedObject::nrefs_;
void clear(SharedPointer< T > &ptr)
Make pointer point to nothing.
bool operator!=(const SharedPointer< U > &other) const
Comparison of two pointers.
bool operator>=(const SharedPointer< U > &other) const
Comparison of two pointers.
~SharedPointer()
Conditionally deletes the pointed-to object.
bool operator!() const
Boolean complement.
SharedPointer(const Sawyer::Nothing &)
Constructs an empty shared pointer.
SharedPointer & operator=(const SharedPointer< Y > &other)
Assignment.
bool operator>=(const U *ptr) const
Comparison of two pointers.
SharedPointer & operator=(std::nullptr_t)
Assignment.
Reference-counting intrusive smart pointer.
Pointee * getRawPointer()
Obtain the pointed-to object.
Name space for the entire library.
bool operator>(const SharedPointer< U > &other) const
Comparison of two pointers.
SharedPointer< T > sharedFromThis()
Create a shared pointer from this.
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.
SharedPointer(const SharedPointer &other)
Constructs a new pointer that shares ownership of the pointed-to object with the other pointer...
T & operator*() const
Reference to the pointed-to object.
Creates SharedPointer from this.
SharedPointer< const T > sharedFromThis() const
Create a shared pointer from this.
friend size_t ownershipCount(const SharedPointer &ptr)
Returns the pointed-to object's reference count.
SharedPointer & operator=(const SharedPointer &other)
Assignment.
SharedPointer< U > dynamicCast() const
Dynamic cast.
bool operator!=(const U *ptr) const
Comparison of two pointers.
SharedPointer(std::nullptr_t)
Constructs an empty shared pointer.
friend std::ostream & operator<<(std::ostream &out, const SharedPointer &ptr)
Print a shared pointer.
T * operator->() const
Dereference pointed-to object.
SharedPointer(const SharedPointer< Y > &other)
Constructs a new pointer that shares ownership of the pointed-to object with the other pointer...
bool operator==(const U *ptr) const
Comparison of two pointers.
SharedPointer()
Constructs an empty shared pointer.
SharedPointer(Y *rawPtr)
Constructs a shared pointer for an object.
SharedPointer & operator=(const Sawyer::Nothing &)
Assignment.
bool operator>(const U *ptr) const
Comparison of two pointers.