CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
DynamicLibrary.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
13
14namespace CeresEngine {
15
17 public:
18 enum class Binding {
26 Lazy,
27
34 };
35
36 enum class Sharing {
41 Global,
42
48 Local
49 };
50
51 public:
53 using NativeHandle = void*;
54
57 template<typename = void> struct Symbol;
58
59 private:
62
63 public:
66
75
83
86
89
92 DynamicLibrary(DynamicLibrary&& other) noexcept;
93
98
102
103 public: // Symbol loading
108
114
115 public: // Loading & Unloading
119
122
123 public: // Operator overloading
127
132
140
148
153
158
159 public:
163
167
168 public:
172
175
181
187
193
198 template<typename... Ts> static bool available(Ts&&... names) { return available({names...}); }
199
204
208 template<typename... Ts> static DynamicLibrary tryOpen(Ts&&... names) { return tryOpen({names...}); }
209 };
210
213 template<> struct DynamicLibrary::Symbol<void> {
215 using Ptr = void*;
216
219
221 explicit operator bool() const noexcept { return ptr != nullptr; }
222
226 template<typename T,
227 typename = typename std::enable_if_t<std::is_pointer_v<T>>> operator T() const noexcept { // NOLINT
228 return reinterpret_cast<T>(ptr);
229 }
230
234 template<typename T> operator Symbol<T>() const noexcept { // NOLINT
235 return Symbol<T>(reinterpret_cast<typename Symbol<T>::Ptr>(ptr));
236 }
237 };
238
239 template<typename T> struct DynamicLibrary::Symbol {
241 using Ptr = T*;
242
244 Ptr ptr = nullptr;
245
248 explicit Symbol(const Ptr ptr) : ptr(ptr) {}
249
251 explicit operator bool() const noexcept { return ptr != nullptr; }
252
254 operator T() const { // NOLINT
255 return *ptr;
256 }
257
259 operator const T&() const { // NOLINT
260 return *ptr;
261 }
262
264 operator T&() { // NOLINT
265 return *ptr;
266 }
267
268 Symbol& operator=(T&& value) {
269 *ptr = std::forward<T>(value);
270 return *this;
271 }
272
274 T* operator->() noexcept { return ptr; }
275
276 // \return A pointer to the variable
277 const T* operator->() const noexcept { return ptr; }
278
279 // \return A reference to the variable
280 T& operator*() noexcept { return *ptr; }
281
282 // \return A reference to the variable
283 const T& operator*() const noexcept { return *ptr; }
284 };
285
286 template<typename R, typename... Args> struct DynamicLibrary::Symbol<R(Args...)> {
288 using Ptr = R (*)(Args...);
289
291 Ptr ptr = nullptr;
292
295 explicit Symbol(const Ptr ptr) : ptr(ptr) {}
296
298 explicit operator bool() const noexcept { return ptr != nullptr; }
299
303 R operator()(Args... args) const noexcept { return ptr(std::forward<Args>(args)...); }
304 };
305
306 template<typename R, typename... Args> struct DynamicLibrary::Symbol<R (*)(Args...)> : DynamicLibrary::Symbol<R(Args...)> {
307 using Symbol<R(Args...)>::Symbol;
308 };
309
314 template<typename T> T DynamicLibrary::resolve(const StringView symbolName) const noexcept { return reinterpret_cast<T>(resolve(symbolName).ptr); }
315
316 template<typename T1, typename T2> bool operator==(const DynamicLibrary::Symbol<T1>& a, const DynamicLibrary::Symbol<T2>& b) { return a.ptr == b.ptr; }
317 template<typename T1, typename T2> bool operator!=(const DynamicLibrary::Symbol<T1>& a, const DynamicLibrary::Symbol<T2>& b) { return a.ptr != b.ptr; }
318
319 template<typename T> bool operator==(const DynamicLibrary::Symbol<T>& a, std::nullptr_t) { return a.ptr == nullptr; }
320 template<typename T> bool operator!=(const DynamicLibrary::Symbol<T>& a, std::nullptr_t) { return a.ptr != nullptr; }
321
322} // namespace CeresEngine
Definition DynamicLibrary.hpp:16
DynamicLibrary()
Creates a new unloaded DynamicLibrary.
static DynamicLibrary tryOpen(const NameSequence &names)
Tries to open a DynamicLibrary from a sequence of names and paths.
static const StringView prefix
A prefix added to the library name.
Definition DynamicLibrary.hpp:162
std::initializer_list< NameVariant > NameSequence
A sequence of library name variants.
Definition DynamicLibrary.hpp:174
static DynamicLibrary tryOpen(Ts &&... names)
Tries to open a DynamicLibrary from a sequence of names and paths.
Definition DynamicLibrary.hpp:208
static bool available(const FilePath &path)
Checks if a DynamicLibrary can be loaded with the given path.
DynamicLibrary(const FilePath &path, Binding binding=Binding::Immediate, Sharing sharing=Sharing::Local)
Creates a new DynamicLibrary by loading the library at the given path.
Sharing
Definition DynamicLibrary.hpp:36
@ Global
The symbols defined by this library will be made available for symbol resolution of subsequently load...
@ Local
This is the converse of global.
DynamicLibrary & operator=(const DynamicLibrary &)=delete
Copy assignment of a DynamicLibrary is not allowed.
DynamicLibrary & operator=(DynamicLibrary &&other) noexcept
Assigns the DynamicLibrary by moving the contents of another.
Binding
Definition DynamicLibrary.hpp:18
@ Immediate
All undefined symbols in the library are normalize before the constructor returns.
~DynamicLibrary() noexcept
Unloads the dynamic library (if loaded) and destroy the DynamicLibrary object.
void unload() noexcept
Unloads the library.
bool loaded() const noexcept
Checks if the library is loaded.
NativeHandle mHandle
The native handle used to load the dynamic library.
Definition DynamicLibrary.hpp:61
static const StringView suffix
A suffix added to the library name.
Definition DynamicLibrary.hpp:166
static bool available(Ts &&... names)
Checks if a DynamicLibrary can be loaded with the given name.
Definition DynamicLibrary.hpp:198
DynamicLibrary(const DynamicLibrary &)=delete
Copy of a DynamicLibrary is not allowed.
Symbol resolve(StringView symbolName) const noexcept
Resolves a symbol by it's name.
DynamicLibrary(DynamicLibrary &&other) noexcept
Creates a new DynamicLibrary by moving the contents of another.
void * NativeHandle
A type that represents the native dynamic library handle.
Definition DynamicLibrary.hpp:53
DynamicLibrary(StringView name, Binding binding=Binding::Immediate, Sharing sharing=Sharing::Local)
Creates a new DynamicLibrary by loading the library by it's name.
Path to file or directory.
Definition FilePath.hpp:37
Definition Variant.hpp:15
Definition Application.hpp:19
bool operator!=(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:416
bool operator==(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:411
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
R(*)(Args...) Ptr
A function pointer type.
Definition DynamicLibrary.hpp:288
Symbol(const Ptr ptr)
Creates a new Symbol wrapper.
Definition DynamicLibrary.hpp:295
R operator()(Args... args) const noexcept
Invokes the function.
Definition DynamicLibrary.hpp:303
void * Ptr
The void* pointer type.
Definition DynamicLibrary.hpp:215
Ptr ptr
The loaded function pointer.
Definition DynamicLibrary.hpp:218
A helper structure that provides an implicit conversion operator from a loaded void* function pointer...
Definition DynamicLibrary.hpp:239
Symbol(const Ptr ptr)
Creates a new Symbol wrapper.
Definition DynamicLibrary.hpp:248
Symbol & operator=(T &&value)
Definition DynamicLibrary.hpp:268
T * Ptr
A variable pointer type.
Definition DynamicLibrary.hpp:241
T & operator*() noexcept
Definition DynamicLibrary.hpp:280
T * operator->() noexcept
Definition DynamicLibrary.hpp:274
const T * operator->() const noexcept
Definition DynamicLibrary.hpp:277
const T & operator*() const noexcept
Definition DynamicLibrary.hpp:283
Ptr ptr
A pointer to the variable.
Definition DynamicLibrary.hpp:244