SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_resource.h
1#ifndef SDL3PP_RESOURCE_H_
2#define SDL3PP_RESOURCE_H_
3
4#include <optional>
5
6namespace SDL {
7
15template<class T>
17{
18 T m_resource;
19
20public:
22 constexpr Resource(T resource = {})
23 : m_resource(std::move(resource))
24 {
25 }
26
28 constexpr Resource(std::nullptr_t)
29 : m_resource{0}
30 {
31 }
32
34 constexpr Resource(std::nullopt_t)
35 : m_resource{0}
36 {
37 }
38
39 Resource(const Resource& other) = delete;
40 Resource(Resource&& other) = delete;
41 Resource& operator=(const Resource& other) = delete;
42 Resource& operator=(Resource&& other) = delete;
43
45 constexpr explicit operator bool() const { return m_resource; }
46
48 constexpr bool operator==(const Resource& other) const = default;
49
51 constexpr bool operator==(std::nullopt_t) const { return !m_resource; }
52
54 constexpr bool operator==(std::nullptr_t) const { return !m_resource; }
55
57 constexpr T get() const { return m_resource; }
58
60 constexpr T release(T newResource = {})
61 {
62 T result = std::move(m_resource);
63 m_resource = newResource;
64 return result;
65 }
66
68 constexpr const T operator->() const { return get(); }
69
71 constexpr T operator->() { return get(); }
72};
73
86template<class REF, class UNIQUE>
87class OptionalResource : public REF
88{
89 bool m_owning = false;
90
91public:
92 using REF::REF;
93
95 constexpr OptionalResource(const REF& other)
96 : REF(other)
97 {
98 }
99
101 constexpr OptionalResource(const UNIQUE& other)
102 : REF(other)
103 {
104 }
105
107 constexpr OptionalResource(UNIQUE&& other)
108 : REF(other.release())
109 , m_owning(true)
110 {
111 }
112
113 OptionalResource(const OptionalResource& other) = delete;
114
117 : REF(other.release())
118 , m_owning(other.m_owning)
119 {
120 other.m_owning = false;
121 }
122
123 OptionalResource& operator=(const OptionalResource& other) = delete;
124
127 {
128 REF::operator=(REF(other.release()));
129 m_owning = other.m_owning;
130 other.m_owning = false;
131 return *this;
132 }
133
136 {
137 if (m_owning) REF::reset();
138 }
139};
140
154template<class REF, class UNIQUE>
156{
157 using REF::REF;
158
159 DetachedResource(const DetachedResource& other) = delete;
160
163 : REF(other.release())
164 {
165 }
166
167 DetachedResource& operator=(const DetachedResource& other) = delete;
168
171 {
172 release(other.release());
173 return *this;
174 }
175
177 operator UNIQUE() { return UNIQUE{REF::release()}; }
178};
179
180} // namespace SDL
181
182#endif /* SDL3PP_RESOURCE_H_ */
A optional reference to resource.
Definition SDL3pp_resource.h:88
OptionalResource & operator=(OptionalResource &&other)
Assignment operator.
Definition SDL3pp_resource.h:126
constexpr OptionalResource(const REF &other)
Constructs from a reference.
Definition SDL3pp_resource.h:95
~OptionalResource()
Destructor.
Definition SDL3pp_resource.h:135
OptionalResource(OptionalResource &&other)
Move ctor.
Definition SDL3pp_resource.h:116
constexpr OptionalResource(UNIQUE &&other)
Constructs from a moved from unique.
Definition SDL3pp_resource.h:107
constexpr OptionalResource(const UNIQUE &other)
Constructs from a reference.
Definition SDL3pp_resource.h:101
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr bool operator==(const Resource &other) const =default
Comparison.
constexpr T operator->()
Access to fields.
Definition SDL3pp_resource.h:71
constexpr T release(T newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr Resource(std::nullptr_t)
Equivalent to default ctor.
Definition SDL3pp_resource.h:28
constexpr const T operator->() const
Access to fields.
Definition SDL3pp_resource.h:68
constexpr bool operator==(std::nullptr_t) const
Comparison.
Definition SDL3pp_resource.h:54
constexpr T get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
constexpr bool operator==(std::nullopt_t) const
Comparison.
Definition SDL3pp_resource.h:51
constexpr Resource(std::nullopt_t)
Equivalent to default ctor.
Definition SDL3pp_resource.h:34
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
A detached reference to resource that might be transformed into an owned handle.
Definition SDL3pp_resource.h:156
constexpr DetachedResource(DetachedResource &&other)
Move ctor.
Definition SDL3pp_resource.h:162
constexpr DetachedResource & operator=(DetachedResource &&other)
Move assignment.
Definition SDL3pp_resource.h:170