Skip to content

Shared Ptr

DryPerspective edited this page Sep 22, 2023 · 7 revisions

Like the standard std::shared_ptr, a dp::shared_ptr is a reference-counted smart pointer class. The pointers can be copied and duplicated as many times as the user wishes, and the underlying resource will be automatically cleaned up then the last shared_ptr which points to it is destroyed. As is true for all reference counting pointers, there is a little cost to pay for such bookkeeping, but this has been minimised and is outweighed by leak safety. Shared_ptr supports custom deleters and custom allocators.

dp::shared_ptr should behave exactly like its standard library counterpart; except that the internal reference counting is not done atomically. This is because C++98 does not feature any concurrency or atomicity primitives. Take care when using this class across threads to avoid race conditions and data races.

The header also includes dp::weak_ptr - a smart pointer which points to a resource managed by a shared_ptr but which does not own that resource. Good for breaking dependency cycles.

dp::shared_ptr includes a constructor for dp::auto_ptr. If, for whatever reason, you are using this library in C++17, you can disable references to auto_ptr by defining DP_CPP17.

Functions

Shared_Ptr

reset Replaces the managed object
swap Swaps the managed objects
get Gets the stored pointer
operator* operator-> Single value only: Dereferences the stored pointer
operator[] Array only: Provides indexed access
use_count Returns the number of shared_ptr objects which refer to the same managed object
unique Returns whether the pointer is the only pointer which refers to its managed object
operator bool Checks whether the stored pointer is not null
owner_before Provides owner-based ordering of smart pointers
make_shared Creates a resource and binds it to a smart pointer in an operation which is atomic to the caller
allocate_shared make_shared for a custom allocator
static_pointer_cast
dynamic_pointer_cast
const_pointer_cast
reinterpret_pointer_cast
Performs the specified cast on the held pointer, and returns a shared_ptr object of the desired type, linked to the same managed resource
get_deleter Returns the deleter of the specified type if owned, and NULL otherwise
operator==
operator!=
operator<
operator<=
operator>
operator>=
Performs comparison of the stored pointers
operator<< Places the value of the stored pointer into the output stream

Weak_ptr

reset Releases ownership of the managed object
swap Swaps the managed objects
use_count Returns the number of dp::shared_ptr objects which manage the instance
expired Checks of the underlying resource has been destroyed
lock Creates a dp::shared_ptr which manages the underlying object
owner_before Provides owner-based ordering of weak pointers

Sample Code

#include <iostream>
#include "cpp98/shared_ptr.h"
 
void observe(dp::weak_ptr<int> gw)
{
    std::cout << "gw.use_count() == " << gw.use_count() << "; ";
    // we have to make a copy of shared pointer before usage:
    if (dp::shared_ptr<int> spt = gw.lock())
        std::cout << "*spt == " << *spt << '\n';
    else
        std::cout << "gw is expired\n";
}
 
int main()
{
    dp::weak_ptr<int> gw;
    {
        dp::shared_ptr<int> sp = dp::make_shared<int>(42);
        gw = sp;
 
        observe(gw);
    }
 
    observe(gw);
}

Clone this wiki locally