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> 
   71    static void acquireOwnership(Pointee *rawPtr);
 
   74    static size_t releaseOwnership(Pointee *rawPtr);
 
   76#ifdef SAWYER_HAVE_BOOST_SERIALIZATION 
   78    friend class boost::serialization::access;
 
   81    void save(S &s, 
const unsigned )
 const {
 
   82        s << BOOST_SERIALIZATION_NVP(pointee_);
 
   86    void load(S &s, 
const unsigned ) {
 
   87        if (pointee_ != 
nullptr && 0==releaseOwnership(pointee_))
 
   90        s >> BOOST_SERIALIZATION_NVP(pointee_);
 
   91        acquireOwnership(pointee_);
 
   94    BOOST_SERIALIZATION_SPLIT_MEMBER();
 
   97#ifdef SAWYER_HAVE_CEREAL 
   99    friend class cereal::access;
 
  101    template<
class Archive>
 
  102    void CEREAL_SAVE_FUNCTION_NAME(Archive &archive)
 const {
 
  103        archive(CEREAL_NVP(pointee_));
 
  106    template<
class Archive>
 
  107    void CEREAL_LOAD_FUNCTION_NAME(Archive &archive) {
 
  108        if (pointee_ != 
nullptr && 0 == releaseOwnership(pointee_))
 
  111        archive(CEREAL_NVP(pointee_));
 
  112        acquireOwnership(pointee_);
 
  121        : pointee_(nullptr) {}
 
 
  123        : pointee_(nullptr) {}
 
 
  125        : pointee_(nullptr) {}
 
 
  132        acquireOwnership(pointee_);
 
 
  136        acquireOwnership(pointee_);
 
 
  147        if (pointee_ != 
nullptr)
 
  148            acquireOwnership(pointee_);
 
 
  153        if (0==releaseOwnership(pointee_))
 
 
  161        return operator=<T>(other);
 
 
  166            if (pointee_ != 
nullptr && 0 == releaseOwnership(pointee_))
 
  169            acquireOwnership(pointee_);
 
 
  179        if (pointee_ != 
nullptr && 0 == releaseOwnership(pointee_))
 
 
  185        if (pointee_ != 
nullptr && 0 == releaseOwnership(pointee_))
 
 
  195        ASSERT_not_null2(pointee_, 
"shared pointer points to no object");
 
 
  202        ASSERT_not_null2(pointee_, 
"shared pointer points to no object");
 
 
  252        return pointee_ == ptr;
 
 
  256        return pointee_ != ptr;
 
 
  260        return pointee_ < ptr;
 
 
  264        return pointee_ <= ptr;
 
 
  268        return pointee_ > ptr;
 
 
  272        return pointee_ >= ptr;
 
 
  298    void this_type_does_not_support_comparisons()
 const {}
 
  311    operator unspecified_bool()
 const {
 
  312        return pointee_ ? &SharedPointer::this_type_does_not_support_comparisons : 
nullptr;
 
 
 
  349template<
class Po
inter>
 
  350typename Pointer::Pointee*
 
  351getRawPointer(Pointer& ptr) {
 
  352    return ptr.getRawPointer();
 
  408        T *derived = 
dynamic_cast<T*
>(
this);
 
  409        ASSERT_not_null(derived);
 
 
  413        const T *derived = 
dynamic_cast<const T*
>(
this);
 
  414        ASSERT_not_null(derived);
 
 
 
  430#define SAWYER_LEAK_SHARED_POINTERS 
  436#if defined(SAWYER_LEAK_SHARED_POINTERS) 
  439        SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
 
  440        return rawPtr->SharedObject::nrefs_;
 
  447inline void SharedPointer<T>::acquireOwnership(Pointee *rawPtr) {
 
  448#if defined(SAWYER_LEAK_SHARED_POINTERS) 
  451    if (rawPtr != 
nullptr) {
 
  452        SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
 
  453        ++rawPtr->SharedObject::nrefs_;
 
  459inline size_t SharedPointer<T>::releaseOwnership(Pointee *rawPtr) {
 
  460    if (rawPtr != 
nullptr) {
 
  461#if defined(SAWYER_LEAK_SHARED_POINTERS) 
  464        SAWYER_THREAD_TRAITS::LockGuard lock(rawPtr->SharedObject::mutex_);
 
  465        assert(rawPtr->SharedObject::nrefs_ > 0);
 
  466        return --rawPtr->SharedObject::nrefs_;
 
Creates SharedPointer from this.
 
SharedPointer< T > sharedFromThis()
Create a shared pointer from this.
 
SharedPointer< const T > sharedFromThis() const
Create a shared pointer from this.
 
Reference-counting intrusive smart pointer.
 
friend std::ostream & operator<<(std::ostream &out, const SharedPointer &ptr)
Print a shared pointer.
 
bool operator<=(const U *ptr) const
Comparison of two pointers.
 
bool operator!=(const U *ptr) const
Comparison of two pointers.
 
SharedPointer(Y *rawPtr)
Constructs a shared pointer for an object.
 
SharedPointer< U > dynamicCast() const
Dynamic cast.
 
SharedPointer()
Constructs an empty shared pointer.
 
SharedPointer & operator=(const Sawyer::Nothing &)
Assignment.
 
bool operator<(const U *ptr) const
Comparison of two pointers.
 
SharedPointer(const Sawyer::Nothing &)
Constructs an empty shared pointer.
 
bool operator!() const
Boolean complement.
 
bool operator>=(const SharedPointer< U > &other) const
Comparison of two pointers.
 
friend size_t ownershipCount(const SharedPointer &ptr)
Returns the pointed-to object's reference count.
 
bool operator<=(const SharedPointer< U > &other) const
Comparison of two pointers.
 
~SharedPointer()
Conditionally deletes 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.
 
T * operator->() const
Dereference pointed-to object.
 
SharedPointer & operator=(std::nullptr_t)
Assignment.
 
SharedPointer & operator=(const SharedPointer< Y > &other)
Assignment.
 
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.
 
SharedPointer(std::nullptr_t)
Constructs an empty shared pointer.
 
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.
 
bool operator>(const U *ptr) const
Comparison of two pointers.
 
T & operator*() const
Reference to the pointed-to object.
 
SharedPointer & operator=(const SharedPointer &other)
Assignment.
 
SharedPointer(const SharedPointer &other)
Constructs a new pointer that shares ownership of the pointed-to object with the other pointer.
 
Pointee * getRawPointer()
Obtain the pointed-to object.
 
void clear(SharedPointer< T > &ptr)
Make pointer point to nothing.