MXVK Vulkan Framework 0.24.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

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.
unwrap () const
 Unwrap the stored pointer, panicking on null.
unwrap_or (T fallback) const noexcept
 Return the stored pointer or a fallback if null.
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 37 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 46 of file mxvk_wrapper.hpp.

46: 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 52 of file mxvk_wrapper.hpp.

52: 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 111 of file mxvk_wrapper.hpp.

111 {
112 if (has_value()) {
113 return type.value();
114 }
115 throw mxvk::Exception(std::format("panic: {}", msg));
116 }
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 92 of file mxvk_wrapper.hpp.

92 {
93 return type.has_value() && type.value() != nullptr;
94 }

◆ operator bool()

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

Check if a value is present and non-null.

Definition at line 97 of file mxvk_wrapper.hpp.

97{ 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 77 of file mxvk_wrapper.hpp.

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

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

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

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

123 {
124 if (has_value()) {
125 return type.value();
126 }
127 throw mxvk::Exception("mxvk panic: Wrapper value is null");
128 }

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

135 {
136 if (has_value()) {
137 return type.value();
138 }
139 return fallback;
140 }

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

103{ return type.value_or(nullptr); }

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