SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_optionalRef.h
1#ifndef SDL3PP_OPTIONAL_REF_H_
2#define SDL3PP_OPTIONAL_REF_H_
3
4#include <optional>
5
6namespace SDL {
7
18template<class T>
20{
21 T* ptr;
22
23public:
25 constexpr OptionalRef(std::nullopt_t = std::nullopt)
26 : ptr(nullptr)
27 {
28 }
29
31 constexpr OptionalRef(T& value)
32 : ptr(&value)
33 {
34 }
35
37 constexpr auto operator<=>(const OptionalRef&) const = default;
38
40 constexpr bool has_value() const { return ptr != nullptr; }
41
43 constexpr const T& value() const { return *ptr; }
44
46 constexpr T& value() { return *ptr; }
47
49 constexpr operator T*() const { return ptr; }
50
52 constexpr bool operator==(std::nullopt_t) const { return ptr == nullptr; }
53
55 constexpr T& operator*() { return *ptr; }
56
58 constexpr const T& operator*() const { return *ptr; }
59
61 constexpr T* operator->() { return ptr; }
62
64 constexpr const T* operator->() const { return ptr; }
65};
66
70template<class T>
72{
73 if (ptr) return {*ptr};
74 return std::nullopt;
75}
76
77} // namespace SDL
78
79#endif /* SDL3PP_OPTIONAL_REF_H_ */
Optional-like shim for references.
Definition: SDL3pp_optionalRef.h:20
constexpr T & value()
Returns contained value.
Definition: SDL3pp_optionalRef.h:46
constexpr T & operator*()
Equivalent to value()
Definition: SDL3pp_optionalRef.h:55
constexpr auto operator<=>(const OptionalRef &) const =default
Constructor.
constexpr OptionalRef(T &value)
Constructor.
Definition: SDL3pp_optionalRef.h:31
constexpr T * operator->()
Equivalent to value()
Definition: SDL3pp_optionalRef.h:61
constexpr bool has_value() const
true if has a value.
Definition: SDL3pp_optionalRef.h:40
constexpr const T & value() const
Returns contained value.
Definition: SDL3pp_optionalRef.h:43
constexpr bool operator==(std::nullopt_t) const
Comparison operator.
Definition: SDL3pp_optionalRef.h:52
constexpr const T * operator->() const
Equivalent to value()
Definition: SDL3pp_optionalRef.h:64
constexpr OptionalRef(std::nullopt_t=std::nullopt)
Constructor.
Definition: SDL3pp_optionalRef.h:25
constexpr const T & operator*() const
Equivalent to value()
Definition: SDL3pp_optionalRef.h:58
Main include header for the SDL3pp library.
constexpr OptionalRef< T > fromNullable(T *ptr)
Create OptionalRef from a nullable pointer.
Definition: SDL3pp_optionalRef.h:71