StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
StormByte::Safe::Shared< T > Class Template Reference

Forward declaration of shared ownership. More...

#include <StormByte/safe/pointers.hxx>

Public Types

using element_type = T
 Pointee type.
using weak_type = Weak<T>
 Matching observer.

Public Member Functions

 Shared () noexcept
 Construct an empty owner.
 Shared (std::nullptr_t null) noexcept
 Construct an empty owner from null.
template<class U>
requires Type::ConvertibleTo<U*, T*>
 Shared (const Shared< U > &other) noexcept
 Share ownership with another owner.
template<class U>
requires Type::ConvertibleTo<U*, T*>
 Shared (Shared< U > &&other) noexcept
 Take ownership from another owner.
 Shared (const Shared &other) noexcept
 Copy an owner.
 Shared (Shared &&other) noexcept
 Take an owner.
 Shared (const Weak< T > &weak)
 Lock an observer.
Shared & operator= (const Shared &other) noexcept
 Share ownership.
Shared & operator= (Shared &&other) noexcept
 Take ownership.
 ~Shared ()
 Drop this owner.
T * get () const noexcept
 Return the stored pointer.
std::add_lvalue_reference_t< T > operator* () const noexcept
 Dereference the stored pointer.
T * operator-> () const noexcept
 Access a member of the pointee.
 operator bool () const noexcept
 Test whether this owner holds an object.
long use_count () const noexcept
 Return the number of owners.
void reset () noexcept
 Drop this owner.
void swap (Shared &other) noexcept
 Exchange owners.
template<class U>
bool owner_before (const Shared< U > &other) const noexcept
 Order control blocks by identity.
template<class U>
bool owner_before (const Weak< U > &other) const noexcept
 Order this control block against an observer.
STORMBYTE_FORCE_INLINE operator std::shared_ptr< T > () const
 Copy the owner into caller-owned STL storage.

Static Public Member Functions

template<class Target, class... Args>
requires Type::SameAs<Target, T> || Type::DerivedFrom<Target, T>
static Shared< T > MakePointer (Args &&... args)
 Allocate Target on Base's heap and own it as T.

Friends

template<class U>
class Shared
template<class U>
class Weak
template<class U>
class AtomicShared
template<class U, class... Args>
Shared< U > MakeShared (Args &&...)
template<class X, class Y>
Shared< X > StaticPointerCast (const Shared< Y > &) noexcept
template<class X, class Y>
Shared< X > DynamicPointerCast (const Shared< Y > &) noexcept
template<class X, class Y>
Shared< X > ConstPointerCast (const Shared< Y > &) noexcept
template<class X, class Y>
Shared< X > ReinterpretPointerCast (const Shared< Y > &) noexcept

Detailed Description

template<class T>
class StormByte::Safe::Shared< T >

Forward declaration of shared ownership.

Shared owner of an object allocated on Base's heap.

Template Parameters
TPointee.
TPointee type. void is allowed, as in std::shared_ptr<void>.

The control block is a Detail::Control allocated with Heap::Allocate. Copying a Shared copies that pointer. There is no std::shared_ptr inside. There is no constructor from a raw pointer, from std::shared_ptr or from std::unique_ptr.

Member Typedef Documentation

◆ element_type

template<class T>
using StormByte::Safe::Shared< T >::element_type = T

Pointee type.

◆ weak_type

template<class T>
using StormByte::Safe::Shared< T >::weak_type = Weak<T>

Matching observer.

Constructor & Destructor Documentation

◆ Shared() [1/7]

template<class T>
StormByte::Safe::Shared< T >::Shared ( )
noexcept

Construct an empty owner.

◆ Shared() [2/7]

template<class T>
StormByte::Safe::Shared< T >::Shared ( std::nullptr_t null)
noexcept

Construct an empty owner from null.

Parameters
nullNull pointer constant.

◆ Shared() [3/7]

template<class T>
template<class U>
requires Type::ConvertibleTo<U*, T*>
StormByte::Safe::Shared< T >::Shared ( const Shared< U > & other)
noexcept

Share ownership with another owner.

Template Parameters
USource pointee. U* must convert to T*.
Parameters
otherOwner to share.

◆ Shared() [4/7]

template<class T>
template<class U>
requires Type::ConvertibleTo<U*, T*>
StormByte::Safe::Shared< T >::Shared ( Shared< U > && other)
noexcept

Take ownership from another owner.

Template Parameters
USource pointee. U* must convert to T*.
Parameters
otherOwner to take.

◆ Shared() [5/7]

template<class T>
StormByte::Safe::Shared< T >::Shared ( const Shared< T > & other)
noexcept

Copy an owner.

The control block stays.

Parameters
otherOwner to copy.

◆ Shared() [6/7]

template<class T>
StormByte::Safe::Shared< T >::Shared ( Shared< T > && other)
noexcept

Take an owner.

other is left empty.

Parameters
otherOwner to take.

◆ Shared() [7/7]

template<class T>
StormByte::Safe::Shared< T >::Shared ( const Weak< T > & weak)
explicit

Lock an observer.

Parameters
weakObserver of a live control block.
Exceptions
ExpiredWeakPointerErrorweak is empty or expired.

◆ ~Shared()

template<class T>
StormByte::Safe::Shared< T >::~Shared ( )

Drop this owner.

The last owner destroys the object.

Member Function Documentation

◆ get()

template<class T>
T * StormByte::Safe::Shared< T >::get ( ) const
noexcept

Return the stored pointer.

Returns
Pointee, or null.

◆ MakePointer()

template<class T>
template<class Target, class... Args>
requires Type::SameAs<Target, T> || Type::DerivedFrom<Target, T>
Shared< T > StormByte::Safe::Shared< T >::MakePointer ( Args &&... args)
static

Allocate Target on Base's heap and own it as T.

Template Parameters
TargetConcrete type. It is T or derived from T.
ArgsConstructor argument types.
Parameters
argsArguments forwarded to Target.
Returns
Owner of the concrete object. The stored destructor is Target's.
Exceptions
AllocationErrorThe block could not be allocated.

◆ operator bool()

template<class T>
StormByte::Safe::Shared< T >::operator bool ( ) const
explicitnoexcept

Test whether this owner holds an object.

Returns
Whether the stored pointer is non-null.

◆ operator std::shared_ptr< T >()

template<class T>
STORMBYTE_FORCE_INLINE StormByte::Safe::Shared< T >::operator std::shared_ptr< T > ( ) const
inlineexplicit

Copy the owner into caller-owned STL storage.

Returns
A std::shared_ptr whose deleter still releases Base's block. There is no conversion back.

◆ operator*()

template<class T>
std::add_lvalue_reference_t< T > StormByte::Safe::Shared< T >::operator* ( ) const
noexcept

Dereference the stored pointer.

Returns
Pointee. void when T is void, so the class can be formed.

◆ operator->()

template<class T>
T * StormByte::Safe::Shared< T >::operator-> ( ) const
noexcept

Access a member of the pointee.

Returns
Stored pointer.

◆ operator=() [1/2]

template<class T>
Shared & StormByte::Safe::Shared< T >::operator= ( const Shared< T > & other)
noexcept

Share ownership.

Parameters
otherOwner to copy.
Returns
This owner.

◆ operator=() [2/2]

template<class T>
Shared & StormByte::Safe::Shared< T >::operator= ( Shared< T > && other)
noexcept

Take ownership.

other is left empty.

Parameters
otherOwner to take.
Returns
This owner.

◆ owner_before() [1/2]

template<class T>
template<class U>
bool StormByte::Safe::Shared< T >::owner_before ( const Shared< U > & other) const
noexcept

Order control blocks by identity.

Template Parameters
UOther pointee.
Parameters
otherOther owner.
Returns
Whether this control block precedes other.

◆ owner_before() [2/2]

template<class T>
template<class U>
bool StormByte::Safe::Shared< T >::owner_before ( const Weak< U > & other) const
noexcept

Order this control block against an observer.

Template Parameters
UOther pointee.
Parameters
otherObserver.
Returns
Whether this control block precedes other.

◆ reset()

template<class T>
void StormByte::Safe::Shared< T >::reset ( )
noexcept

Drop this owner.

◆ swap()

template<class T>
void StormByte::Safe::Shared< T >::swap ( Shared< T > & other)
noexcept

Exchange owners.

Parameters
otherOwner to exchange with.

◆ use_count()

template<class T>
long StormByte::Safe::Shared< T >::use_count ( ) const
noexcept

Return the number of owners.

Returns
Strong count, or zero when empty.

◆ AtomicShared

template<class T>
template<class U>
friend class AtomicShared
friend

◆ ConstPointerCast

template<class T>
template<class X, class Y>
Shared< X > ConstPointerCast ( const Shared< Y > & )
friend

◆ DynamicPointerCast

template<class T>
template<class X, class Y>
Shared< X > DynamicPointerCast ( const Shared< Y > & )
friend

◆ MakeShared

template<class T>
template<class U, class... Args>
Shared< U > MakeShared ( Args && ...)
friend

◆ ReinterpretPointerCast

template<class T>
template<class X, class Y>
Shared< X > ReinterpretPointerCast ( const Shared< Y > & )
friend

◆ Shared

template<class T>
template<class U>
friend class Shared
friend

◆ StaticPointerCast

template<class T>
template<class X, class Y>
Shared< X > StaticPointerCast ( const Shared< Y > & )
friend

◆ Weak

template<class T>
template<class U>
friend class Weak
friend

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