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() = default;
97
105 constexpr explicit SharedObject(const SharedObjectRaw resource)
106 : m_resource(resource)
107 {
108 }
109
111 constexpr SharedObject(const SharedObject& other) = delete;
112
114 constexpr SharedObject(SharedObject&& other)
115 : SharedObject(other.release())
116 {
117 }
118
119 constexpr SharedObject(const SharedObjectRef& other) = delete;
120
121 constexpr SharedObject(SharedObjectRef&& other) = delete;
122
138 : m_resource(SDL_LoadObject(sofile))
139 {
140 }
141
143 ~SharedObject() { SDL_UnloadObject(m_resource); }
144
147 {
148 std::swap(m_resource, other.m_resource);
149 return *this;
150 }
151
153 constexpr SharedObjectRaw get() const { return m_resource; }
154
157 {
158 auto r = m_resource;
159 m_resource = nullptr;
160 return r;
161 }
162
164 constexpr auto operator<=>(const SharedObject& other) const = default;
165
167 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
168
170 constexpr explicit operator bool() const { return !!m_resource; }
171
173 constexpr operator SharedObjectParam() const { return {m_resource}; }
174
188 void Unload();
189
217};
218
221{
230 : SharedObject(resource.value)
231 {
232 }
233
236 : SharedObject(other.get())
237 {
238 }
239
242};
243
259{
260 return SharedObject(std::move(sofile));
261}
262
291{
292 return SDL_LoadFunction(handle, name);
293}
294
296{
297 return SDL::LoadFunction(m_resource, std::move(name));
298}
299
315inline void UnloadObject(SharedObjectRaw handle) { SDL_UnloadObject(handle); }
316
318
320
321} // namespace SDL
322
323#endif /* SDL3PP_LOADSO_H_ */
An opaque datatype that represents a loaded shared object.
Definition: SDL3pp_loadso.h:91
constexpr SharedObject()=default
Default ctor.
SharedObject(StringParam sofile)
Dynamically load a shared object.
Definition: SDL3pp_loadso.h:137
SharedObject & operator=(SharedObject other)
Assignment operator.
Definition: SDL3pp_loadso.h:146
constexpr auto operator<=>(const SharedObject &other) const =default
Comparison.
constexpr SharedObject(const SharedObject &other)=delete
Copy constructor.
constexpr SharedObject(SharedObject &&other)
Move constructor.
Definition: SDL3pp_loadso.h:114
~SharedObject()
Destructor.
Definition: SDL3pp_loadso.h:143
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_loadso.h:167
constexpr SharedObjectRaw release()
Retrieves underlying SharedObjectRaw and clear this.
Definition: SDL3pp_loadso.h:156
constexpr SharedObject(const SharedObjectRaw resource)
Constructs from SharedObjectParam.
Definition: SDL3pp_loadso.h:105
constexpr SharedObjectRaw get() const
Retrieves underlying SharedObjectRaw.
Definition: SDL3pp_loadso.h:153
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:295
SharedObject LoadObject(StringParam sofile)
Dynamically load a shared object.
Definition: SDL3pp_loadso.h:258
void UnloadObject(SharedObjectRaw handle)
Unload a shared object from memory.
Definition: SDL3pp_loadso.h:315
void Unload()
Unload a shared object from memory.
Definition: SDL3pp_loadso.h:317
FunctionPointer LoadFunction(SharedObjectParam handle, StringParam name)
Look up the address of the named function in a shared object.
Definition: SDL3pp_loadso.h:290
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:6206
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:221
~SharedObjectRef()
Destructor.
Definition: SDL3pp_loadso.h:241
SharedObjectRef(const SharedObjectRef &other)
Copy constructor.
Definition: SDL3pp_loadso.h:235
SharedObjectRef(SharedObjectParam resource)
Constructs from SharedObjectParam.
Definition: SDL3pp_loadso.h:229