libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
SharedPtr< T, CopyOnWrite, ST > Class Template Reference

A smart pointer class with reference counting and optional copy-on-write semantics. More...

#include <sharedptr.h>

Public Member Functions

 SharedPtr (const SharedPtr &sp)
 
 SharedPtr (SharedPtr &&sp) noexcept
 
SharedPtroperator= (const SharedPtr &sp)
 
SharedPtroperator= (SharedPtr &&sp) noexcept
 
void swap (SharedPtr &other) noexcept
 
void clear ()
 
void detach ()
 
bool isNull () const
 
bool isValid () const
 
 operator bool () const
 
bool operator== (const SharedPtr &other) const
 
bool operator!= (const SharedPtr &other) const
 
bool operator== (std::nullptr_t) const
 
bool operator!= (std::nullptr_t) const
 
int referenceCount () const
 
const Tptr () const
 
Tmodify ()
 
const Toperator-> () const
 
const Toperator* () const
 

Static Public Member Functions

template<typename... Args>
static SharedPtr create (Args &&... args)
 
static SharedPtr takeOwnership (T *obj)
 

Static Public Attributes

static constexpr bool isNative = IsSharedObject<T>::value
 
static constexpr bool isCopyOnWrite = CopyOnWrite
 

Detailed Description

template<typename T, bool CopyOnWrite = true, typename ST = std::conditional_t<IsSharedObject<T>::value, T, SharedPtrProxy<T>>>
class SharedPtr< T, CopyOnWrite, ST >

A smart pointer class with reference counting and optional copy-on-write semantics.

This class provides a reference-counted smart pointer for managing the lifetime of objects. It supports both native reference counting and proxy-based reference counting for objects that do not natively support it. Additionally, it offers (default enabled) copy-on-write semantics to optimize performance when multiple references exist.

Typical use case:

// Create a new shared object in-place
// Or take ownership of an existing raw pointer
SharedPtr<MyClass> copy = ptr; // Reference count is incremented
// You can now push the copy into another thread and the reference counting
// will be thread safe.
anotherThread->pushToThread(copy);
// Anytime you're calling a const function, you can dereference directly.
// If using CopyOnWrite, this will not cause a copy operation to happen
ptr->constFunc();
// Anytime you're calling a non-const function, i.e. a function that may
// modify the object, you must access it via the modify() function. If
// CopyOnWrite is enabled, this will cause a copy of the object (if it
// has a reference count > 1) and that new copy will be returned (and now
// stored as the version of the object pointed to by this SharedPtr).
ptr.modify()->nonConstFunc();
Dynamic array container wrapping std::vector.
Definition list.h:40
A smart pointer class with reference counting and optional copy-on-write semantics.
Definition sharedptr.h:252

Thread Safety:

  • The reference counting operations are thread-safe.
  • It is assumed that multiple threads may hold SharedPtr instances to the same object.
  • Only one thread will delete the shared data when the reference count drops to zero.
  • Individual SharedPtr instances are not thread-safe and should be used with appropriate synchronization if shared between threads. If you're doing this, however, you're probably doing something wrong.

Limitations:

  • This class does not support custom deleters.
  • Copy-on-write might introduce performance overhead in some scenarios.
  • Using non native objects is possible, however has the additonal limitations:
    • They incur an extra level of indirection, as a proxy object is used to hold the reference count and the pointer to the non-native object.
    • They can't do CopyOnWrite if they use polymorphism, since if T is the base class only the base class copy constructors will be used (thus splitting your object). There's an assert that ensures this will fail. If you need polymorphism, use native objects.
Template Parameters
TThe type of the object being managed.
CopyOnWriteWhether copy-on-write semantics are enabled.
STThe storage type for the managed object, for native objects this is the same as T. For non-native types, this is a proxy object.

The documentation for this class was generated from the following file: