libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::Wrapper< T > Class Template Reference

A nullable smart wrapper around a raw pointer. More...

#include <wrapper.hpp>

Public Member Functions

 Wrapper ()
 Default constructor — initialises to nullopt (no value).
 Wrapper (const T &t)
 Construct from a raw pointer.
 Wrapper (std::nullopt_t n)
 Construct in the empty (nullopt) state.
 Wrapper (const Wrapper< T > &w)
 Copy constructor.
 Wrapper (Wrapper< T > &&w)
 Move constructor.
 ~Wrapper ()=default
Wrapper< T > & operator= (const T &t)
 Assign from a raw pointer.
Wrapper< T > & operator= (std::nullopt_t n)
 Reset to the empty (nullopt) state.
Wrapper< T > & operator= (const Wrapper< T > &w)
 Copy-assign from another Wrapper.
Wrapper< T > & operator= (Wrapper< T > &&w)
 Move-assign from another Wrapper.
bool has_value () const
 Check whether a non-null value is held.
T value ()
 Access the stored pointer without null checking.
T expect (const std::string &msg)
 Unwrap with a custom panic message on null.
T unwrap ()
 Unwrap the stored pointer, panicking on null.
T unwrap_or (T value)
 Return the stored pointer or a fallback if null.

Private Attributes

std::optional< T > type
 Internal optional storage.

Detailed Description

template<WrapType T>
class mx::Wrapper< T >

A nullable smart wrapper around a raw pointer.

Stores an optional pointer. Accessors throw mx::Exception if the pointer is null (unwrap, expect) or return a fallback (unwrap_or).

Template Parameters
TA raw pointer type (must satisfy WrapType concept).

Definition at line 36 of file wrapper.hpp.

Constructor & Destructor Documentation

◆ Wrapper() [1/5]

template<WrapType T>
mx::Wrapper< T >::Wrapper ( )
inline

Default constructor — initialises to nullopt (no value).

Definition at line 39 of file wrapper.hpp.

References type.

Referenced by operator=(), operator=(), operator=(), operator=(), Wrapper(), and Wrapper().

◆ Wrapper() [2/5]

template<WrapType T>
mx::Wrapper< T >::Wrapper ( const T & t)
inline

Construct from a raw pointer.

Parameters
tPointer to wrap.

Definition at line 45 of file wrapper.hpp.

References type.

◆ Wrapper() [3/5]

template<WrapType T>
mx::Wrapper< T >::Wrapper ( std::nullopt_t n)
inline

Construct in the empty (nullopt) state.

Parameters
nstd::nullopt.

Definition at line 51 of file wrapper.hpp.

References type.

◆ Wrapper() [4/5]

template<WrapType T>
mx::Wrapper< T >::Wrapper ( const Wrapper< T > & w)
inline

Copy constructor.

Definition at line 54 of file wrapper.hpp.

References type, and Wrapper().

◆ Wrapper() [5/5]

template<WrapType T>
mx::Wrapper< T >::Wrapper ( Wrapper< T > && w)
inline

Move constructor.

Definition at line 57 of file wrapper.hpp.

References type, and Wrapper().

◆ ~Wrapper()

template<WrapType T>
mx::Wrapper< T >::~Wrapper ( )
default

Member Function Documentation

◆ expect()

template<WrapType T>
T mx::Wrapper< T >::expect ( const std::string & msg)
inline

Unwrap with a custom panic message on null.

Parameters
msgMessage to include in the thrown mx::Exception.
Returns
The stored pointer.
Exceptions
mx::Exceptionif the value is null or absent.

Definition at line 118 of file wrapper.hpp.

References type.

◆ has_value()

template<WrapType T>
bool mx::Wrapper< T >::has_value ( ) const
inline

Check whether a non-null value is held.

Returns
true if a non-null pointer is stored.

Definition at line 97 of file wrapper.hpp.

References type.

◆ operator=() [1/4]

template<WrapType T>
Wrapper< T > & mx::Wrapper< T >::operator= ( const T & t)
inline

Assign from a raw pointer.

Parameters
tPointer to store.
Returns
Reference to this.

Definition at line 66 of file wrapper.hpp.

References type, and Wrapper().

◆ operator=() [2/4]

template<WrapType T>
Wrapper< T > & mx::Wrapper< T >::operator= ( const Wrapper< T > & w)
inline

Copy-assign from another Wrapper.

Definition at line 82 of file wrapper.hpp.

References type, and Wrapper().

◆ operator=() [3/4]

template<WrapType T>
Wrapper< T > & mx::Wrapper< T >::operator= ( std::nullopt_t n)
inline

Reset to the empty (nullopt) state.

Parameters
nstd::nullopt.
Returns
Reference to this.

Definition at line 76 of file wrapper.hpp.

References type, and Wrapper().

◆ operator=() [4/4]

template<WrapType T>
Wrapper< T > & mx::Wrapper< T >::operator= ( Wrapper< T > && w)
inline

Move-assign from another Wrapper.

Definition at line 88 of file wrapper.hpp.

References type, and Wrapper().

◆ unwrap()

template<WrapType T>
T mx::Wrapper< T >::unwrap ( )
inline

Unwrap the stored pointer, panicking on null.

Returns
The stored pointer.
Exceptions
mx::Exceptionif the value is null or absent.

Definition at line 132 of file wrapper.hpp.

References type.

◆ unwrap_or()

template<WrapType T>
T mx::Wrapper< T >::unwrap_or ( T value)
inline

Return the stored pointer or a fallback if null.

Parameters
valueFallback pointer returned when no value is held.
Returns
The stored pointer, or value if null/absent.

Definition at line 147 of file wrapper.hpp.

References type, and value().

◆ value()

template<WrapType T>
T mx::Wrapper< T >::value ( )
inline

Access the stored pointer without null checking.

Returns
The stored pointer (may be nullptr if constructed from nullopt).

Definition at line 108 of file wrapper.hpp.

References type.

Referenced by unwrap_or().

Member Data Documentation

◆ type

template<WrapType T>
std::optional<T> mx::Wrapper< T >::type
private

Internal optional storage.

Definition at line 154 of file wrapper.hpp.

Referenced by expect(), has_value(), operator=(), operator=(), operator=(), operator=(), unwrap(), unwrap_or(), value(), Wrapper(), Wrapper(), Wrapper(), Wrapper(), and Wrapper().


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