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
64{
65 SharedObjectRaw m_resource = nullptr;
66
67public:
69 constexpr SharedObject(std::nullptr_t = nullptr) noexcept
70 : m_resource(nullptr)
71 {
72 }
73
81 constexpr explicit SharedObject(const SharedObjectRaw resource) noexcept
82 : m_resource(resource)
83 {
84 }
85
86protected:
88 constexpr SharedObject(const SharedObject& other) noexcept = default;
89
90public:
92 constexpr SharedObject(SharedObject&& other) noexcept
93 : SharedObject(other.release())
94 {
95 }
96
97 constexpr SharedObject(const SharedObjectRef& other) = delete;
98
99 constexpr SharedObject(SharedObjectRef&& other) = delete;
100
116
118 ~SharedObject() { SDL_UnloadObject(m_resource); }
119
121 constexpr SharedObject& operator=(SharedObject&& other) noexcept
122 {
123 std::swap(m_resource, other.m_resource);
124 return *this;
125 }
126
127protected:
129 constexpr SharedObject& operator=(const SharedObject& other) noexcept =
130 default;
131
132public:
134 constexpr SharedObjectRaw get() const noexcept { return m_resource; }
135
137 constexpr SharedObjectRaw release() noexcept
138 {
139 auto r = m_resource;
140 m_resource = nullptr;
141 return r;
142 }
143
145 constexpr auto operator<=>(const SharedObject& other) const noexcept =
146 default;
147
149 constexpr explicit operator bool() const noexcept { return !!m_resource; }
150
163 void Unload();
164
192};
193
200{
202
211 : SharedObject(resource)
212 {
213 }
214
222 constexpr SharedObjectRef(const SharedObject& resource) noexcept
223 : SharedObject(resource.get())
224 {
225 }
226
234 constexpr SharedObjectRef(SharedObject&& resource) noexcept
235 : SharedObject(std::move(resource).release())
236 {
237 }
238
240 constexpr SharedObjectRef(const SharedObjectRef& other) noexcept
241 : SharedObject(other.get())
242 {
243 }
244
246 constexpr SharedObjectRef(SharedObjectRef&& other) noexcept
247 : SharedObject(other.get())
248 {
249 }
250
253
255 constexpr SharedObjectRef& operator=(const SharedObjectRef& other) noexcept =
256 default;
257
259 constexpr operator SharedObjectRaw() const noexcept { return get(); }
260};
261
277{
278 return SharedObject(std::move(sofile));
279}
280
282 : m_resource(SDL_LoadObject(sofile))
283{
284}
285
314{
315 return SDL_LoadFunction(handle, name);
316}
317
319{
320 return SDL::LoadFunction(m_resource, std::move(name));
321}
322
338inline void UnloadObject(SharedObjectRaw handle) { SDL_UnloadObject(handle); }
339
341
343
344} // namespace SDL
345
346#endif /* SDL3PP_LOADSO_H_ */
An opaque datatype that represents a loaded shared object.
Definition: SDL3pp_loadso.h:64
constexpr SharedObject(const SharedObjectRaw resource) noexcept
Constructs from SharedObjectRef.
Definition: SDL3pp_loadso.h:81
constexpr SharedObject & operator=(const SharedObject &other) noexcept=default
Assignment operator.
constexpr SharedObjectRaw get() const noexcept
Retrieves underlying SharedObjectRaw.
Definition: SDL3pp_loadso.h:134
constexpr SharedObject(const SharedObject &other) noexcept=default
Copy constructor.
constexpr auto operator<=>(const SharedObject &other) const noexcept=default
Comparison.
constexpr SharedObject(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_loadso.h:69
constexpr SharedObjectRaw release() noexcept
Retrieves underlying SharedObjectRaw and clear this.
Definition: SDL3pp_loadso.h:137
~SharedObject()
Destructor.
Definition: SDL3pp_loadso.h:118
constexpr SharedObject & operator=(SharedObject &&other) noexcept
Assignment operator.
Definition: SDL3pp_loadso.h:121
constexpr SharedObject(SharedObject &&other) noexcept
Move constructor.
Definition: SDL3pp_loadso.h:92
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:318
SharedObject LoadObject(StringParam sofile)
Dynamically load a shared object.
Definition: SDL3pp_loadso.h:276
void UnloadObject(SharedObjectRaw handle)
Unload a shared object from memory.
Definition: SDL3pp_loadso.h:338
void Unload()
Unload a shared object from memory.
Definition: SDL3pp_loadso.h:340
FunctionPointer LoadFunction(SharedObjectRef handle, StringParam name)
Look up the address of the named function in a shared object.
Definition: SDL3pp_loadso.h:313
SDL_SharedObject * SharedObjectRaw
Alias to raw representation for SharedObject.
Definition: SDL3pp_loadso.h:47
void(SDLCALL *)() FunctionPointer
A generic function pointer.
Definition: SDL3pp_stdinc.h:6415
Main include header for the SDL3pp library.
Reference for SharedObject.
Definition: SDL3pp_loadso.h:200
constexpr SharedObjectRef(const SharedObject &resource) noexcept
Constructs from SharedObject.
Definition: SDL3pp_loadso.h:222
constexpr SharedObjectRef(SharedObject &&resource) noexcept
Constructs from SharedObject.
Definition: SDL3pp_loadso.h:234
constexpr SharedObjectRef(const SharedObjectRef &other) noexcept
Copy constructor.
Definition: SDL3pp_loadso.h:240
SharedObjectRef(SharedObjectRaw resource) noexcept
Constructs from raw SharedObject.
Definition: SDL3pp_loadso.h:210
~SharedObjectRef()
Destructor.
Definition: SDL3pp_loadso.h:252
constexpr SharedObjectRef(SharedObjectRef &&other) noexcept
Move constructor.
Definition: SDL3pp_loadso.h:246
constexpr SharedObjectRef & operator=(const SharedObjectRef &other) noexcept=default
Assignment operator.