summaryrefslogtreecommitdiff
path: root/src/common/dynamic_library.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/dynamic_library.h')
-rw-r--r--src/common/dynamic_library.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/common/dynamic_library.h b/src/common/dynamic_library.h
new file mode 100644
index 000000000..2a06372fd
--- /dev/null
+++ b/src/common/dynamic_library.h
@@ -0,0 +1,75 @@
1// Copyright 2019 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8
9namespace Common {
10
11/**
12 * Provides a platform-independent interface for loading a dynamic library and retrieving symbols.
13 * The interface maintains an internal reference count to allow one handle to be shared between
14 * multiple users.
15 */
16class DynamicLibrary final {
17public:
18 /// Default constructor, does not load a library.
19 explicit DynamicLibrary();
20
21 /// Automatically loads the specified library. Call IsOpen() to check validity before use.
22 explicit DynamicLibrary(const char* filename);
23
24 /// Moves the library.
25 DynamicLibrary(DynamicLibrary&&) noexcept;
26 DynamicLibrary& operator=(DynamicLibrary&&) noexcept;
27
28 /// Delete copies, we can't copy a dynamic library.
29 DynamicLibrary(const DynamicLibrary&) = delete;
30 DynamicLibrary& operator=(const DynamicLibrary&) = delete;
31
32 /// Closes the library.
33 ~DynamicLibrary();
34
35 /// Returns the specified library name with the platform-specific suffix added.
36 static std::string GetUnprefixedFilename(const char* filename);
37
38 /// Returns the specified library name in platform-specific format.
39 /// Major/minor versions will not be included if set to -1.
40 /// If libname already contains the "lib" prefix, it will not be added again.
41 /// Windows: LIBNAME-MAJOR-MINOR.dll
42 /// Linux: libLIBNAME.so.MAJOR.MINOR
43 /// Mac: libLIBNAME.MAJOR.MINOR.dylib
44 static std::string GetVersionedFilename(const char* libname, int major = -1, int minor = -1);
45
46 /// Returns true if a module is loaded, otherwise false.
47 bool IsOpen() const {
48 return handle != nullptr;
49 }
50
51 /// Loads (or replaces) the handle with the specified library file name.
52 /// Returns true if the library was loaded and can be used.
53 bool Open(const char* filename);
54
55 /// Unloads the library, any function pointers from this library are no longer valid.
56 void Close();
57
58 /// Returns the address of the specified symbol (function or variable) as an untyped pointer.
59 /// If the specified symbol does not exist in this library, nullptr is returned.
60 void* GetSymbolAddress(const char* name) const;
61
62 /// Obtains the address of the specified symbol, automatically casting to the correct type.
63 /// Returns true if the symbol was found and assigned, otherwise false.
64 template <typename T>
65 bool GetSymbol(const char* name, T* ptr) const {
66 *ptr = reinterpret_cast<T>(GetSymbolAddress(name));
67 return *ptr != nullptr;
68 }
69
70private:
71 /// Platform-dependent data type representing a dynamic library handle.
72 void* handle = nullptr;
73};
74
75} // namespace Common