MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::Wrapper< T > Class Template Reference

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

#include <mxvk/include/mxvk/mxvk_wrapper.hpp>

Public Member Functions

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

Detailed Description

template<WrapType T>
class mxvk::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 mxvk_wrapper.hpp.

Constructor & Destructor Documentation

◆ Wrapper() [1/5]

template<WrapType T>
mxvk::Wrapper< T >::Wrapper ( )
default

Default constructor — initialises to nullopt (no value).

◆ Wrapper() [2/5]

template<WrapType T>
mxvk::Wrapper< T >::Wrapper ( T t)
inline

Construct from a raw pointer.

Parameters
tPointer to wrap.

Definition at line 45 of file mxvk_wrapper.hpp.

45: type{t} {}
A nullable smart wrapper around a raw pointer.

◆ Wrapper() [3/5]

template<WrapType T>
mxvk::Wrapper< T >::Wrapper ( std::nullopt_t nullopt)
inline

Construct in the empty (nullopt) state.

Parameters
nulloptstd::nullopt.

Definition at line 51 of file mxvk_wrapper.hpp.

51: type{nullopt} {}

◆ Wrapper() [4/5]

template<WrapType T>
mxvk::Wrapper< T >::Wrapper ( const Wrapper< T > & )
default

Copy constructor.

◆ Wrapper() [5/5]

template<WrapType T>
mxvk::Wrapper< T >::Wrapper ( Wrapper< T > && )
defaultnoexcept

Move constructor.

◆ ~Wrapper()

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

Member Function Documentation

◆ expect()

template<WrapType T>
T mxvk::Wrapper< T >::expect ( const std::string & msg) const
inlinenodiscard

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 108 of file mxvk_wrapper.hpp.

108 {
109 if (has_value()) {
110 return type.value();
111 }
112 throw mxvk::Exception(std::format("panic: {}", msg));
113 }
bool has_value() const noexcept
Check whether a non-null value is held.

◆ has_value()

template<WrapType T>
bool mxvk::Wrapper< T >::has_value ( ) const
inlinenodiscardnoexcept

Check whether a non-null value is held.

Returns
true if a non-null pointer is stored.

Definition at line 91 of file mxvk_wrapper.hpp.

91{ return type.has_value() && type.value() != nullptr; }

◆ operator bool()

template<WrapType T>
mxvk::Wrapper< T >::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Check if a value is present and non-null.

Definition at line 94 of file mxvk_wrapper.hpp.

94{ return has_value(); }

◆ operator=() [1/4]

template<WrapType T>
Wrapper< T > & mxvk::Wrapper< T >::operator= ( const Wrapper< T > & )
default

Copy-assign from another Wrapper.

◆ operator=() [2/4]

template<WrapType T>
Wrapper< T > & mxvk::Wrapper< T >::operator= ( std::nullopt_t nullopt)
inline

Reset to the empty (nullopt) state.

Parameters
nulloptstd::nullopt.
Returns
Reference to this.

Definition at line 76 of file mxvk_wrapper.hpp.

76 {
77 type = nullopt;
78 return *this;
79 }

◆ operator=() [3/4]

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

Assign from a raw pointer.

Parameters
tPointer to store.
Returns
Reference to this.

Definition at line 66 of file mxvk_wrapper.hpp.

66 {
67 type = t;
68 return *this;
69 }

◆ operator=() [4/4]

template<WrapType T>
Wrapper< T > & mxvk::Wrapper< T >::operator= ( Wrapper< T > && )
defaultnoexcept

Move-assign from another Wrapper.

◆ unwrap()

template<WrapType T>
T mxvk::Wrapper< T >::unwrap ( ) const
inlinenodiscard

Unwrap the stored pointer, panicking on null.

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

Definition at line 120 of file mxvk_wrapper.hpp.

120 {
121 if (has_value()) {
122 return type.value();
123 }
124 throw mxvk::Exception("mxvk panic: Wrapper value is null");
125 }

◆ unwrap_or()

template<WrapType T>
T mxvk::Wrapper< T >::unwrap_or ( T fallback) const
inlinenodiscardnoexcept

Return the stored pointer or a fallback if null.

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

Definition at line 132 of file mxvk_wrapper.hpp.

132 {
133 if (has_value()) {
134 return type.value();
135 }
136 return fallback;
137 }

◆ value()

template<WrapType T>
T mxvk::Wrapper< T >::value ( ) const
inlinenodiscard

Access the stored pointer without null checking.

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

Definition at line 100 of file mxvk_wrapper.hpp.

100{ return type.value_or(nullptr); }

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