Skip to main content
Pigweed's logo Pigweed
  1. Home
  2. Reference
  3. C/C++
  4. pw_allocator
  5. Core interfaces
  6. pw::MaybeSharedPtr< T > Class Template Reference
Loading...
Searching...
No Matches
pw::MaybeSharedPtr< T > Class Template Reference

Overview

template<typename T>
class pw::MaybeSharedPtr< T >

A smart pointer whose sole purpose is to support sharing between dynamic and statically allocated code.

MaybeSharedPtr can be constructed from any pw::SharedPtr (retaining its shared ownership semantics) or via MaybeSharedPtr::Unowned(...) (holding a non-owning borrowed reference to a statically allocated object).

Unlike SharedPtr, unowned MaybeSharedPtr instances do not participate in reference counting, will not destroy the object upon leaving scope, and avoid dynamic control block allocations.

MaybeSharedPtr explicitly does not support array types (e.g. pw::MaybeSharedPtr<T[]>). In pw::SharedPtr<T[]>, the array size is stored within the dynamic control block, which unowned instances lack. To pass unowned contiguous buffers, prefer pw::span.

Template Parameters
TThe type being pointed to. Must not be an array type.

Public Types

using element_type = typename SharedPtr< T >::element_type
 

Public Member Functions

constexpr MaybeSharedPtr () noexcept=default
 Creates an empty (nullptr) instance.
 
constexpr MaybeSharedPtr (std::nullptr_t) noexcept
 Creates an empty (nullptr) instance.
 
constexpr MaybeSharedPtr (const MaybeSharedPtr &other) noexcept=default
 Copy-constructs a MaybeSharedPtr<T> from another MaybeSharedPtr<T>.
 
 MaybeSharedPtr (MaybeSharedPtr &&other) noexcept=default
 Move-constructs a MaybeSharedPtr<T> from another MaybeSharedPtr<T>.
 
constexpr MaybeSharedPtroperator= (const MaybeSharedPtr &other) noexcept
 Copy-assigns a MaybeSharedPtr<T> from another MaybeSharedPtr<T>.
 
MaybeSharedPtroperator= (MaybeSharedPtr &&other) noexcept
 Move-assigns a MaybeSharedPtr<T> from another MaybeSharedPtr<T>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
constexpr MaybeSharedPtr (const MaybeSharedPtr< U > &other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
 MaybeSharedPtr (MaybeSharedPtr< U > &&other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
constexpr MaybeSharedPtr (const SharedPtr< U > &other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
 MaybeSharedPtr (SharedPtr< U > &&other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
constexpr MaybeSharedPtroperator= (const MaybeSharedPtr< U > &other) noexcept
 Copy-assigns a MaybeSharedPtr<T> from a MaybeSharedPtr<U>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
MaybeSharedPtroperator= (MaybeSharedPtr< U > &&other) noexcept
 Move-assigns a MaybeSharedPtr<T> from a MaybeSharedPtr<U>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
constexpr MaybeSharedPtroperator= (const SharedPtr< U > &other) noexcept
 Copy-assigns a MaybeSharedPtr<T> from a SharedPtr<U>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
MaybeSharedPtroperator= (SharedPtr< U > &&other) noexcept
 Move-assigns a MaybeSharedPtr<T> from a SharedPtr<U>.
 
MaybeSharedPtroperator= (std::nullptr_t) noexcept
 Sets this MaybeSharedPtr to null, releasing any held value.
 
constexpr bool is_owned () const noexcept
 
 operator bool () const =delete
 
constexpr element_type * get () const noexcept
 Returns the underlying (possibly null) pointer.
 
constexpr element_type * operator-> () const noexcept
 Permits access to members of T.
 
constexpr element_type & operator* () const noexcept
 Returns a reference to the underlying object.
 
Allocatorallocator () const noexcept
 
template<typename U >
constexpr operator MaybeSharedPtr< U > () const noexcept
 Explicit conversion operator for downcasting.
 
void reset () noexcept
 
void swap (MaybeSharedPtr &other) noexcept
 Swaps the managed pointer with another object.
 

Static Public Member Functions

static constexpr MaybeSharedPtr Unowned (element_type &value) noexcept
 Constructs an unowned MaybeSharedPtr referencing an lvalue object.
 
static constexpr MaybeSharedPtr Unowned (const element_type &&)=delete
 Disallows constructing an unowned MaybeSharedPtr from a temporary.
 

Friends

template<typename To , typename From >
constexpr MaybeSharedPtr< To > static_pointer_cast (const MaybeSharedPtr< From > &p) noexcept
 Creates a new MaybeSharedPtr by static casting the given pointer.
 
template<typename To , typename From >
constexpr MaybeSharedPtr< To > const_pointer_cast (const MaybeSharedPtr< From > &p) noexcept
 Creates a new MaybeSharedPtr by const casting the given pointer.
 
constexpr bool operator== (const MaybeSharedPtr &lhs, std::nullptr_t) noexcept
 
constexpr bool operator== (std::nullptr_t, const MaybeSharedPtr &rhs) noexcept
 
constexpr bool operator!= (const MaybeSharedPtr &lhs, std::nullptr_t) noexcept
 
constexpr bool operator!= (std::nullptr_t, const MaybeSharedPtr &rhs) noexcept
 
template<typename U >
constexpr bool operator== (const MaybeSharedPtr &lhs, const MaybeSharedPtr< U > &rhs) noexcept
 
template<typename U >
constexpr bool operator!= (const MaybeSharedPtr &lhs, const MaybeSharedPtr< U > &rhs) noexcept
 
template<typename U >
constexpr bool operator== (const MaybeSharedPtr &lhs, const SharedPtr< U > &rhs) noexcept
 
template<typename U >
constexpr bool operator== (const SharedPtr< U > &lhs, const MaybeSharedPtr &rhs) noexcept
 
template<typename U >
constexpr bool operator!= (const MaybeSharedPtr &lhs, const SharedPtr< U > &rhs) noexcept
 
template<typename U >
constexpr bool operator!= (const SharedPtr< U > &lhs, const MaybeSharedPtr &rhs) noexcept
 

Constructor & Destructor Documentation

◆ MaybeSharedPtr() [1/4]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
constexpr pw::MaybeSharedPtr< T >::MaybeSharedPtr ( const MaybeSharedPtr< U > &  other)
inlineconstexprnoexcept

Copy-constructs a MaybeSharedPtr<T> from a MaybeSharedPtr<U>.

This allows converting construction where T is a base class of U.

◆ MaybeSharedPtr() [2/4]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
pw::MaybeSharedPtr< T >::MaybeSharedPtr ( MaybeSharedPtr< U > &&  other)
inlinenoexcept

Move-constructs a MaybeSharedPtr<T> from a MaybeSharedPtr<U>.

This allows converting construction where T is a base class of U.

◆ MaybeSharedPtr() [3/4]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
constexpr pw::MaybeSharedPtr< T >::MaybeSharedPtr ( const SharedPtr< U > &  other)
inlineconstexprnoexcept

Implicitly constructs a MaybeSharedPtr<T> from an owned SharedPtr<U>.

This allows converting construction where T is a base class of U.

◆ MaybeSharedPtr() [4/4]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
pw::MaybeSharedPtr< T >::MaybeSharedPtr ( SharedPtr< U > &&  other)
inlinenoexcept

Implicitly move-constructs a MaybeSharedPtr<T> from an owned SharedPtr<U>.

Member Function Documentation

◆ allocator()

template<typename T >
Allocator * pw::MaybeSharedPtr< T >::allocator ( ) const
inlinenoexcept

Returns the allocator that owns this object, or nullptr if empty or unowned.

◆ is_owned()

template<typename T >
constexpr bool pw::MaybeSharedPtr< T >::is_owned ( ) const
inlineconstexprnoexcept

Returns whether this pointer owns a dynamically allocated object backed by a control block.

◆ operator bool()

template<typename T >
pw::MaybeSharedPtr< T >::operator bool ( ) const
explicitdelete

operator bool is deleted to match SharedPtr and UniquePtr conventions. Use ptr == nullptr or ptr != nullptr.

◆ reset()

template<typename T >
void pw::MaybeSharedPtr< T >::reset ( )
inlinenoexcept

Resets this object to an empty state (nullptr).

The implications depend on whether this instance is owned or unowned:

  • Owned (SharedPtr): Decrements the shared reference count. If this was the last shared pointer referencing the object, the object is destroyed. If no weak pointers remain to the control block, memory is deallocated by its allocator.
  • Unowned (Unowned()): Clears the pointer to nullptr. The referenced object is NOT destroyed, and NO memory is freed.
  • Empty (nullptr): Has no effect.

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