TheAtlasEngine
 
Loading...
Searching...
No Matches
atlas::memory::optional_ptr< T > Class Template Reference

Optional, nullable, smart pointer that works with hal::strong_ptr. More...

#include <memory.hpp>

Public Member Functions

constexpr optional_ptr () noexcept
 Default constructor creates a disengaged optional.
 
constexpr optional_ptr (std::nullptr_t) noexcept
 Constructor for nullptr (creates a disengaged optional)
 
constexpr optional_ptr (optional_ptr &&p_other) noexcept=delete
 Move constructor is deleted.
 
constexpr optional_ptr (strong_ptr< T > const &value) noexcept
 Construct from a strong_ptr lvalue.
 
constexpr optional_ptr (optional_ptr const &p_other)
 Copy constructor.
 
template<typename U >
requires (std::is_convertible_v<U*, T*>)
constexpr optional_ptr (strong_ptr< U > const &p_value)
 Converting constructor from a strong_ptr.
 
constexpr optional_ptroperator= (optional_ptr &&other) noexcept=delete
 Move assignment operator is deleted.
 
constexpr optional_ptroperator= (optional_ptr const &other)
 Copy assignment operator.
 
constexpr optional_ptroperator= (strong_ptr< T > const &value) noexcept
 Assignment from a strong_ptr.
 
template<typename U >
requires (std::is_convertible_v<U*, T*>)
constexpr optional_ptroperator= (strong_ptr< U > const &p_value) noexcept
 Converting assignment from a strong_ptr.
 
constexpr optional_ptroperator= (std::nullptr_t) noexcept
 Assignment from nullptr (resets to disengaged state)
 
 ~optional_ptr ()
 Destructor.
 
constexpr bool has_value () const noexcept
 Check if the optional_ptr is engaged.
 
constexpr operator bool () const noexcept
 Check if the optional_ptr is engaged.
 
constexpr strong_ptr< T > & value ()
 Access the contained value, throw if not engaged.
 
constexpr strong_ptr< T > const & value () const
 Access the contained value, throw if not engaged (const version)
 
constexpr operator strong_ptr< T > ()
 Implicitly convert to a strong_ptr<T>
 
constexpr operator strong_ptr< T > () const
 Implicitly convert to a strong_ptr<T> (const version)
 
template<typename U >
requires (std::is_convertible_v<T*, U*> && !std::is_same_v<T, U>)
constexpr operator strong_ptr< U > ()
 Implicitly convert to a strong_ptr for polymorphic types.
 
template<typename U >
requires (std::is_convertible_v<T*, U*> && !std::is_same_v<T, U>)
constexpr operator strong_ptr< U > () const
 Implicitly convert to a strong_ptr for polymorphic types (const version)
 
constexpr auto * operator-> ()
 Arrow operator for accessing members of the contained object.
 
constexpr auto * operator-> () const
 Arrow operator for accessing members of the contained object (const version)
 
constexpr auto & operator* ()
 Dereference operator for accessing the contained object.
 
constexpr auto & operator* () const
 Dereference operator for accessing the contained object (const version)
 
constexpr void reset () noexcept
 Reset the optional to a disengaged state.
 
template<typename... Args>
constexpr strong_ptr< T > & emplace (Args &&... args)
 Emplace a new value.
 
constexpr void swap (optional_ptr &other) noexcept
 Swap the contents of this optional_ptr with another.
 

Detailed Description

template<typename T>
class atlas::memory::optional_ptr< T >

Optional, nullable, smart pointer that works with hal::strong_ptr.

optional_ptr provides a way to represent a strong_ptr that may or may not be present. Unlike strong_ptr, which is always valid, optional_ptr can be in a "disengaged" state where it doesn't reference any object.

Use optional_ptr when you need a nullable reference to a reference-counted object, such as:

  • Representing the absence of a value
  • Return values from functions that may fail
  • Results of locking a weak_ptr

Example usage:

// Create an empty optional_ptr
optional_ptr<my_driver> opt1;
// Create an optional_ptr from a strong_ptr
auto ptr = make_strong_ptr<my_driver>(allocator, args...);
optional_ptr<my_driver> opt2 = ptr;
// Check if the optional_ptr is engaged
if (opt2) {
// Use the contained object
opt2->doSomething();
}
// Reset to disengage
opt2.reset();
Template Parameters
T- The type pointed to by strong_ptr

Constructor & Destructor Documentation

◆ optional_ptr() [1/3]

template<typename T >
constexpr atlas::memory::optional_ptr< T >::optional_ptr ( strong_ptr< T > const &  value)
inlineconstexprnoexcept

Construct from a strong_ptr lvalue.

Parameters
valueThe strong_ptr to wrap

◆ optional_ptr() [2/3]

template<typename T >
constexpr atlas::memory::optional_ptr< T >::optional_ptr ( optional_ptr< T > const &  p_other)
inlineconstexpr

Copy constructor.

Parameters
p_otherThe optional_ptr to copy from

◆ optional_ptr() [3/3]

template<typename T >
template<typename U >
requires (std::is_convertible_v<U*, T*>)
constexpr atlas::memory::optional_ptr< T >::optional_ptr ( strong_ptr< U > const &  p_value)
inlineconstexpr

Converting constructor from a strong_ptr.

Template Parameters
UA type convertible to T
Parameters
p_valueThe strong_ptr to wrap

◆ ~optional_ptr()

template<typename T >
atlas::memory::optional_ptr< T >::~optional_ptr ( )
inline

Destructor.

Properly destroys the contained strong_ptr if engaged.

Member Function Documentation

◆ emplace()

template<typename T >
template<typename... Args>
constexpr strong_ptr< T > & atlas::memory::optional_ptr< T >::emplace ( Args &&...  args)
inlineconstexpr

Emplace a new value.

Reset the optional and construct a new strong_ptr in-place.

Template Parameters
ArgsTypes of arguments to forward to the constructor
Parameters
argsArguments to forward to the constructor
Returns
Reference to the newly constructed strong_ptr

◆ has_value()

template<typename T >
constexpr bool atlas::memory::optional_ptr< T >::has_value ( ) const
inlineconstexprnoexcept

Check if the optional_ptr is engaged.

Returns
true if the optional_ptr contains a value, false otherwise

◆ operator bool()

template<typename T >
constexpr atlas::memory::optional_ptr< T >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Check if the optional_ptr is engaged.

Returns
true if the optional_ptr contains a value, false otherwise

◆ operator strong_ptr< T >() [1/2]

template<typename T >
constexpr atlas::memory::optional_ptr< T >::operator strong_ptr< T > ( )
inlineconstexpr

Implicitly convert to a strong_ptr<T>

This allows optional_ptr to be used anywhere a strong_ptr is expected when the optional_ptr is engaged.

Returns
A copy of the contained strong_ptr
Exceptions
hal::bad_optional_ptr_accessif *this is disengaged

◆ operator strong_ptr< T >() [2/2]

template<typename T >
constexpr atlas::memory::optional_ptr< T >::operator strong_ptr< T > ( ) const
inlineconstexpr

Implicitly convert to a strong_ptr<T> (const version)

Returns
A copy of the contained strong_ptr
Exceptions
hal::bad_optional_ptr_accessif *this is disengaged

◆ operator strong_ptr< U >() [1/2]

template<typename T >
template<typename U >
requires (std::is_convertible_v<T*, U*> && !std::is_same_v<T, U>)
constexpr atlas::memory::optional_ptr< T >::operator strong_ptr< U > ( )
inlineconstexpr

Implicitly convert to a strong_ptr for polymorphic types.

This allows optional_ptr<Derived> to be converted to strong_ptr<Base> when Derived is convertible to Base.

Template Parameters
UThe target type (must be convertible from T)
Returns
A copy of the contained strong_ptr, converted to the target type
Exceptions
hal::bad_optional_ptr_accessif *this is disengaged

◆ operator strong_ptr< U >() [2/2]

template<typename T >
template<typename U >
requires (std::is_convertible_v<T*, U*> && !std::is_same_v<T, U>)
constexpr atlas::memory::optional_ptr< T >::operator strong_ptr< U > ( ) const
inlineconstexpr

Implicitly convert to a strong_ptr for polymorphic types (const version)

Template Parameters
UThe target type (must be convertible from T)
Returns
A copy of the contained strong_ptr, converted to the target type
Exceptions
hal::bad_optional_ptr_accessif *this is disengaged

◆ operator*() [1/2]

template<typename T >
constexpr auto & atlas::memory::optional_ptr< T >::operator* ( )
inlineconstexpr

Dereference operator for accessing the contained object.

Returns
Reference to the object managed by the contained strong_ptr

◆ operator*() [2/2]

template<typename T >
constexpr auto & atlas::memory::optional_ptr< T >::operator* ( ) const
inlineconstexpr

Dereference operator for accessing the contained object (const version)

Returns
Reference to the object managed by the contained strong_ptr

◆ operator->() [1/2]

template<typename T >
constexpr auto * atlas::memory::optional_ptr< T >::operator-> ( )
inlineconstexpr

Arrow operator for accessing members of the contained object.

Returns
Pointer to the object managed by the contained strong_ptr

◆ operator->() [2/2]

template<typename T >
constexpr auto * atlas::memory::optional_ptr< T >::operator-> ( ) const
inlineconstexpr

Arrow operator for accessing members of the contained object (const version)

Returns
Pointer to the object managed by the contained strong_ptr

◆ operator=() [1/4]

template<typename T >
constexpr optional_ptr & atlas::memory::optional_ptr< T >::operator= ( optional_ptr< T > const &  other)
inlineconstexpr

Copy assignment operator.

Parameters
otherThe optional_ptr to copy from
Returns
Reference to *this

◆ operator=() [2/4]

template<typename T >
constexpr optional_ptr & atlas::memory::optional_ptr< T >::operator= ( std::nullptr_t  )
inlineconstexprnoexcept

Assignment from nullptr (resets to disengaged state)

Returns
Reference to *this

◆ operator=() [3/4]

template<typename T >
constexpr optional_ptr & atlas::memory::optional_ptr< T >::operator= ( strong_ptr< T > const &  value)
inlineconstexprnoexcept

Assignment from a strong_ptr.

Parameters
valueThe strong_ptr to wrap
Returns
Reference to *this

◆ operator=() [4/4]

template<typename T >
template<typename U >
requires (std::is_convertible_v<U*, T*>)
constexpr optional_ptr & atlas::memory::optional_ptr< T >::operator= ( strong_ptr< U > const &  p_value)
inlineconstexprnoexcept

Converting assignment from a strong_ptr.

Template Parameters
UA type convertible to T
Parameters
p_valueThe strong_ptr to wrap
Returns
Reference to *this

◆ swap()

template<typename T >
constexpr void atlas::memory::optional_ptr< T >::swap ( optional_ptr< T > &  other)
inlineconstexprnoexcept

Swap the contents of this optional_ptr with another.

Parameters
otherThe optional_ptr to swap with

◆ value() [1/2]

template<typename T >
constexpr strong_ptr< T > & atlas::memory::optional_ptr< T >::value ( )
inlineconstexpr

Access the contained value, throw if not engaged.

Returns
A copy of the contained strong_ptr
Exceptions
hal::bad_optional_ptr_accessif *this is disengaged

◆ value() [2/2]

template<typename T >
constexpr strong_ptr< T > const & atlas::memory::optional_ptr< T >::value ( ) const
inlineconstexpr

Access the contained value, throw if not engaged (const version)

Returns
A copy of the contained strong_ptr
Exceptions
hal::bad_optional_ptr_accessif *this is disengaged

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