SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_loadso.h
1#ifndef SDL3PP_LOADSO_H_
2#define SDL3PP_LOADSO_H_
3
4#include <SDL3/SDL_loadso.h>
5#include "SDL3pp_stdinc.h"
6
7namespace SDL {
8
43// Forward decl
44struct SharedObject;
45
47using SharedObjectRaw = SDL_SharedObject*;
48
49// Forward decl
50struct SharedObjectRef;
51
54{
56
59 : value(value)
60 {
61 }
62
64 constexpr SharedObjectParam(std::nullptr_t _ = nullptr)
65 : value(nullptr)
66 {
67 }
68
70 constexpr explicit operator bool() const { return !!value; }
71
73 constexpr auto operator<=>(const SharedObjectParam& other) const = default;
74
76 constexpr operator SharedObjectRaw() const { return value; }
77};
78
91{
92 SharedObjectRaw m_resource = nullptr;
93
94public:
96 constexpr SharedObject(std::nullptr_t = nullptr) noexcept
97 : m_resource(0)
98 {
99 }
100
108 constexpr explicit SharedObject(const SharedObjectRaw resource) noexcept
109 : m_resource(resource)
110 {
111 }
112
114 constexpr SharedObject(const SharedObject& other) = delete;
115
117 constexpr SharedObject(SharedObject&& other) noexcept
118 : SharedObject(other.release())
119 {
120 }
121
122 constexpr SharedObject(const SharedObjectRef& other) = delete;
123
124 constexpr SharedObject(SharedObjectRef&& other) = delete;
125
141 : m_resource(SDL_LoadObject(sofile))
142 {
143 }
144
146 ~SharedObject() { SDL_UnloadObject(m_resource); }
147
149 constexpr SharedObject& operator=(SharedObject&& other) noexcept
150 {
151 std::swap(m_resource, other.m_resource);
152 return *this;
153 }
154
155protected:
157 constexpr SharedObject& operator=(const SharedObject& other) noexcept =
158 default;
159
160public:
162 constexpr SharedObjectRaw get() const noexcept { return m_resource; }
163
165 constexpr SharedObjectRaw release() noexcept
166 {
167 auto r = m_resource;
168 m_resource = nullptr;
169 return r;
170 }
171
173 constexpr auto operator<=>(const SharedObject& other) const noexcept =
174 default;
175
177 constexpr explicit operator bool() const noexcept { return !!m_resource; }
178
180 constexpr operator SharedObjectParam() const noexcept { return {m_resource}; }
181
194 void Unload();
195
223};
224
227{
229
238 : SharedObject(resource.value)
239 {
240 }
241
250 : SharedObject(resource)
251 {
252 }
253
255 SharedObjectRef(const SharedObjectRef& other) noexcept
256 : SharedObject(other.get())
257 {
258 }
259
262};
263
279{
280 return SharedObject(std::move(sofile));
281}
282
311{
312 return SDL_LoadFunction(handle, name);
313}
314
316{
317 return SDL::LoadFunction(m_resource, std::move(name));
318}
319
335inline void UnloadObject(SharedObjectRaw handle) { SDL_UnloadObject(handle); }
336
338
340
341} // namespace SDL
342
343#endif /* SDL3PP_LOADSO_H_ */
An opaque datatype that represents a loaded shared object.
Definition: SDL3pp_loadso.h:91
constexpr SharedObject(const SharedObjectRaw resource) noexcept
Constructs from SharedObjectParam.
Definition: SDL3pp_loadso.h:108
constexpr SharedObject & operator=(const SharedObject &other) noexcept=default
Assignment operator.
constexpr SharedObjectRaw get() const noexcept
Retrieves underlying SharedObjectRaw.
Definition: SDL3pp_loadso.h:162
SharedObject(StringParam sofile)
Dynamically load a shared object.
Definition: SDL3pp_loadso.h:140
constexpr auto operator<=>(const SharedObject &other) const noexcept=default
Comparison.
constexpr SharedObject(const SharedObject &other)=delete
Copy constructor.
constexpr SharedObject(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_loadso.h:96
constexpr SharedObjectRaw release() noexcept
Retrieves underlying SharedObjectRaw and clear this.
Definition: SDL3pp_loadso.h:165
~SharedObject()
Destructor.
Definition: SDL3pp_loadso.h:146
constexpr SharedObject & operator=(SharedObject &&other) noexcept
Assignment operator.
Definition: SDL3pp_loadso.h:149
constexpr SharedObject(SharedObject &&other) noexcept
Move constructor.
Definition: SDL3pp_loadso.h:117
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
FunctionPointer LoadFunction(StringParam name)
Look up the address of the named function in a shared object.
Definition: SDL3pp_loadso.h:315
SharedObject LoadObject(StringParam sofile)
Dynamically load a shared object.
Definition: SDL3pp_loadso.h:278
void UnloadObject(SharedObjectRaw handle)
Unload a shared object from memory.
Definition: SDL3pp_loadso.h:335
void Unload()
Unload a shared object from memory.
Definition: SDL3pp_loadso.h:337
FunctionPointer LoadFunction(SharedObjectParam handle, StringParam name)
Look up the address of the named function in a shared object.
Definition: SDL3pp_loadso.h:310
SDL_SharedObject * SharedObjectRaw
Alias to raw representation for SharedObject.
Definition: SDL3pp_loadso.h:47
SDL_FunctionPointer FunctionPointer
A generic function pointer.
Definition: SDL3pp_stdinc.h:6305
Main include header for the SDL3pp library.
Safely wrap SharedObject for non owning parameters.
Definition: SDL3pp_loadso.h:54
SharedObjectRaw value
parameter's SharedObjectRaw
Definition: SDL3pp_loadso.h:55
constexpr SharedObjectParam(SharedObjectRaw value)
Constructs from SharedObjectRaw.
Definition: SDL3pp_loadso.h:58
constexpr auto operator<=>(const SharedObjectParam &other) const =default
Comparison.
constexpr SharedObjectParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_loadso.h:64
Semi-safe reference for SharedObject.
Definition: SDL3pp_loadso.h:227
SharedObjectRef(SharedObjectParam resource) noexcept
Constructs from SharedObjectParam.
Definition: SDL3pp_loadso.h:237
SharedObjectRef(SharedObjectRaw resource) noexcept
Constructs from SharedObjectParam.
Definition: SDL3pp_loadso.h:249
~SharedObjectRef()
Destructor.
Definition: SDL3pp_loadso.h:261
SharedObjectRef(const SharedObjectRef &other) noexcept
Copy constructor.
Definition: SDL3pp_loadso.h:255