Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
uni20::async::shared_storage< T > Class Template Reference

A lightweight, thread-safe, reference-counted storage for a single object. More...

#include <shared_storage.hpp>

Public Types

using element_type = T
 

Public Member Functions

bool constructed () const noexcept
 Reports whether a value is currently constructed.
 
void const * control_address () const noexcept
 Returns the storage control-block address for diagnostics.
 
void destroy () noexcept
 Destroy the contained object while keeping the control block alive.
 
template<typename... Args>
requires std::constructible_from<T, Args...>
T & emplace (Args &&... args)
 Destroy any existing value and construct a new one in place.
 
const T * get () const noexcept
 Returns a const pointer to the constructed value.
 
T * get () noexcept
 Returns a mutable pointer to the constructed value.
 
bool has_lifetime_owner () const noexcept
 Reports whether this value retains another storage control block.
 
 operator bool () const noexcept
 Returns true when a control block is present.
 
bool operator! () const noexcept
 Returns true when no control block is present.
 
const T & operator* () const noexcept
 Dereference access to the constructed value.
 
T & operator* () noexcept
 Dereference access to the constructed value.
 
const T * operator-> () const noexcept
 Pointer-style const access to the constructed value.
 
T * operator-> () noexcept
 Pointer-style mutable access to the constructed value.
 
shared_storage & operator= (const shared_storage &other) noexcept
 Copy assignment shares the control block.
 
shared_storage & operator= (shared_storage &&other) noexcept
 Move assignment transfers the control block pointer.
 
void reset () noexcept
 Release this handle's reference to the control block.
 
 shared_storage () noexcept=default
 Construct an empty handle with no control block.
 
 shared_storage (const shared_storage &other) noexcept
 Copy constructor increments control block reference count.
 
 shared_storage (shared_storage &&other) noexcept
 Move constructor transfers ownership of the control block pointer.
 
T const * storage_address () const noexcept
 Return the const address reserved for the contained value.
 
T * storage_address () noexcept
 Return the address reserved for the contained value.
 
T take () noexcept(std::is_nothrow_move_constructible_v< T >)
 Move the contained value out and destroy the in-place object.
 
size_t use_count () const noexcept
 Returns the current strong reference count.
 
bool valid () const noexcept
 Reports whether a control block is present.
 
 ~shared_storage ()
 Destructor releases one reference to the control block.
 

Static Public Member Functions

template<typename... Args>
static shared_storage make_constructed (Args &&... args)
 Construct storage with an immediately-constructed value.
 
template<typename... Args>
static shared_storage make_constructed_alias (detail::shared_storage_control_block *lifetime_owner, Args &&... args)
 Construct a value whose control block retains a separate lifetime owner.
 

Friends

template<typename U , typename... Args>
shared_storage< U > make_shared_storage (Args &&... args)
 Create a new shared_storage<T> with T(args...) in-place.
 
template<typename U , typename Owner , typename... Args>
shared_storage< U > make_shared_storage_alias (shared_storage< Owner > const &owner, Args &&... args)
 Create storage for a value that keeps another storage value alive.
 
template<typename U >
shared_storage< U > make_unconstructed_shared_storage ()
 Create a new shared_storage<T> with an unconstructed T.
 
class NodeInfo
 
bool operator== (const shared_storage &a, const shared_storage &b) noexcept
 Compare whether two handles reference the same control block.
 

Detailed Description

template<typename T>
class uni20::async::shared_storage< T >

A lightweight, thread-safe, reference-counted storage for a single object.

Unlike std::shared_ptr<T>, shared_storage<T> can exist in an unconstructed state, allowing delayed or conditional in-place construction via .emplace().

This is useful for async or deferred initialization scenarios, where the lifetime and ownership of an object must be shared across tasks, but construction may not yet have occurred.

Key features:

Example:

shared_storage<MyType> s = make_shared_storage<MyType>();
if (!s.constructed())
s.emplace(42, "hello");
MyType& ref = s.get();
s.destroy(); // explicitly destroy the contained object
A lightweight, thread-safe, reference-counted storage for a single object.
Definition shared_storage.hpp:94
void destroy() noexcept
Destroy the contained object while keeping the control block alive.
Definition shared_storage.hpp:286
bool constructed() const noexcept
Reports whether a value is currently constructed.
Definition shared_storage.hpp:234
T & emplace(Args &&... args)
Destroy any existing value and construct a new one in place.
Definition shared_storage.hpp:270
T * get() noexcept
Returns a mutable pointer to the constructed value.
Definition shared_storage.hpp:293

Alias storage can own a lightweight value such as a tensor-view descriptor while retaining one strong reference to another shared_storage control block that owns the underlying data.

Member Typedef Documentation

◆ element_type

template<typename T >
using uni20::async::shared_storage< T >::element_type = T

Constructor & Destructor Documentation

◆ shared_storage() [1/3]

template<typename T >
uni20::async::shared_storage< T >::shared_storage ( )
defaultnoexcept

Construct an empty handle with no control block.

◆ shared_storage() [2/3]

template<typename T >
uni20::async::shared_storage< T >::shared_storage ( const shared_storage< T > &  other)
inlinenoexcept

Copy constructor increments control block reference count.

Parameters
otherSource handle.

◆ shared_storage() [3/3]

template<typename T >
uni20::async::shared_storage< T >::shared_storage ( shared_storage< T > &&  other)
inlinenoexcept

Move constructor transfers ownership of the control block pointer.

Parameters
otherSource handle.

◆ ~shared_storage()

template<typename T >
uni20::async::shared_storage< T >::~shared_storage ( )
inline

Destructor releases one reference to the control block.

Member Function Documentation

◆ constructed()

template<typename T >
bool uni20::async::shared_storage< T >::constructed ( ) const
inlinenoexcept

Reports whether a value is currently constructed.

Returns
true if the control block exists and holds a constructed value.

◆ control_address()

template<typename T >
void const * uni20::async::shared_storage< T >::control_address ( ) const
inlinenoexcept

Returns the storage control-block address for diagnostics.

Returns
Raw control-block address, or nullptr when no control block is present.

◆ destroy()

template<typename T >
void uni20::async::shared_storage< T >::destroy ( )
inlinenoexcept

Destroy the contained object while keeping the control block alive.

◆ emplace()

template<typename T >
template<typename... Args>
requires std::constructible_from<T, Args...>
T & uni20::async::shared_storage< T >::emplace ( Args &&...  args)
inline

Destroy any existing value and construct a new one in place.

Template Parameters
ArgsConstructor argument types.
Parameters
argsConstructor arguments forwarded to T.
Returns
Reference to the newly constructed value.

◆ get() [1/2]

template<typename T >
const T * uni20::async::shared_storage< T >::get ( ) const
inlinenoexcept

Returns a const pointer to the constructed value.

Returns
nullptr when no constructed value is present.

◆ get() [2/2]

template<typename T >
T * uni20::async::shared_storage< T >::get ( )
inlinenoexcept

Returns a mutable pointer to the constructed value.

Returns
nullptr when no constructed value is present.

◆ has_lifetime_owner()

template<typename T >
bool uni20::async::shared_storage< T >::has_lifetime_owner ( ) const
inlinenoexcept

Reports whether this value retains another storage control block.

Returns
true for an alias value with a separate lifetime owner.

◆ make_constructed()

template<typename T >
template<typename... Args>
static shared_storage uni20::async::shared_storage< T >::make_constructed ( Args &&...  args)
inlinestatic

Construct storage with an immediately-constructed value.

Template Parameters
ArgsConstructor argument types.
Parameters
argsConstructor arguments forwarded to T.
Returns
New shared_storage<T> containing a constructed value.

◆ make_constructed_alias()

template<typename T >
template<typename... Args>
static shared_storage uni20::async::shared_storage< T >::make_constructed_alias ( detail::shared_storage_control_block *  lifetime_owner,
Args &&...  args 
)
inlinestatic

Construct a value whose control block retains a separate lifetime owner.

◆ operator bool()

template<typename T >
uni20::async::shared_storage< T >::operator bool ( ) const
inlineexplicitnoexcept

Returns true when a control block is present.

Returns
Equivalent to valid().

◆ operator!()

template<typename T >
bool uni20::async::shared_storage< T >::operator! ( ) const
inlinenoexcept

Returns true when no control block is present.

Returns
Negation of valid().

◆ operator*() [1/2]

template<typename T >
const T & uni20::async::shared_storage< T >::operator* ( ) const
inlinenoexcept

Dereference access to the constructed value.

Returns
Const reference to the contained value.

◆ operator*() [2/2]

template<typename T >
T & uni20::async::shared_storage< T >::operator* ( )
inlinenoexcept

Dereference access to the constructed value.

Returns
Reference to the contained value.

◆ operator->() [1/2]

template<typename T >
const T * uni20::async::shared_storage< T >::operator-> ( ) const
inlinenoexcept

Pointer-style const access to the constructed value.

Returns
Pointer to the contained value.

◆ operator->() [2/2]

template<typename T >
T * uni20::async::shared_storage< T >::operator-> ( )
inlinenoexcept

Pointer-style mutable access to the constructed value.

Returns
Pointer to the contained value.

◆ operator=() [1/2]

template<typename T >
shared_storage & uni20::async::shared_storage< T >::operator= ( const shared_storage< T > &  other)
inlinenoexcept

Copy assignment shares the control block.

Parameters
otherSource handle.
Returns
Reference to *this.

◆ operator=() [2/2]

template<typename T >
shared_storage & uni20::async::shared_storage< T >::operator= ( shared_storage< T > &&  other)
inlinenoexcept

Move assignment transfers the control block pointer.

Parameters
otherSource handle.
Returns
Reference to *this.

◆ reset()

template<typename T >
void uni20::async::shared_storage< T >::reset ( )
inlinenoexcept

Release this handle's reference to the control block.

◆ storage_address() [1/2]

template<typename T >
T const * uni20::async::shared_storage< T >::storage_address ( ) const
inlinenoexcept

Return the const address reserved for the contained value.

Warning
Do not dereference the returned pointer unless constructed() is true.

◆ storage_address() [2/2]

template<typename T >
T * uni20::async::shared_storage< T >::storage_address ( )
inlinenoexcept

Return the address reserved for the contained value.

This address is stable for the lifetime of the control block and is available before the value is constructed. It is intended for lazy descriptors that store an address but do not dereference it until their shared epoch is readable.

Warning
Do not dereference the returned pointer unless constructed() is true.

◆ take()

template<typename T >
T uni20::async::shared_storage< T >::take ( )
inlinenoexcept

Move the contained value out and destroy the in-place object.

Returns
Moved value.

◆ use_count()

template<typename T >
size_t uni20::async::shared_storage< T >::use_count ( ) const
inlinenoexcept

Returns the current strong reference count.

Returns
Number of shared_storage handles sharing this control block.

◆ valid()

template<typename T >
bool uni20::async::shared_storage< T >::valid ( ) const
inlinenoexcept

Reports whether a control block is present.

Returns
true when this handle owns or shares storage metadata.

Friends And Related Symbol Documentation

◆ make_shared_storage

template<typename T >
template<typename U , typename... Args>
shared_storage< U > make_shared_storage ( Args &&...  args)
friend

Create a new shared_storage<T> with T(args...) in-place.

Template Parameters
TStored value type.
ArgsConstructor argument types.
Parameters
argsConstructor arguments forwarded to T.
Returns
New shared_storage<T> with a constructed value.

◆ make_shared_storage_alias

template<typename T >
template<typename U , typename Owner , typename... Args>
shared_storage< U > make_shared_storage_alias ( shared_storage< Owner > const &  owner,
Args &&...  args 
)
friend

Create storage for a value that keeps another storage value alive.

The alias has its own local strong count. Its control block retains one reference to owner, which is released after the alias value is destroyed. Copies of the alias increment only the local count.

Template Parameters
TAlias value type.
OwnerLifetime-owner value type.
ArgsAlias constructor argument types.
Parameters
ownerStorage whose lifetime must cover the alias value.
argsArguments forwarded to the alias value constructor.
Returns
Constructed alias storage sharing the owner's lifetime.

◆ make_unconstructed_shared_storage

template<typename T >
template<typename U >
shared_storage< U > make_unconstructed_shared_storage ( )
friend

Create a new shared_storage<T> with an unconstructed T.

◆ NodeInfo

template<typename T >
friend class NodeInfo
friend

◆ operator==

template<typename T >
bool operator== ( const shared_storage< T > &  a,
const shared_storage< T > &  b 
)
friend

Compare whether two handles reference the same control block.

Parameters
aLeft operand.
bRight operand.
Returns
true when both handles share identical storage metadata.

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