| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #pragma once |
| 5 | #include <atomic> |
| 6 | #include <string> |
| 7 | |
| 8 | namespace Common |
| 9 | { |
| 10 | /** |
| 11 | * Provides a platform-independent interface for loading a dynamic library and retrieving symbols. |
| 12 | * The interface maintains an internal reference count to allow one handle to be shared between |
| 13 | * multiple users. |
| 14 | */ |
| 15 | class DynamicLibrary final |
| 16 | { |
| 17 | public: |
| 18 | // Default constructor, does not load a library. |
| 19 | DynamicLibrary(); |
| 20 | |
| 21 | // Automatically loads the specified library. Call IsOpen() to check validity before use. |
| 22 | DynamicLibrary(const char* filename); |
| 23 | |
| 24 | DynamicLibrary(void* handle); |
| 25 | |
| 26 | // Closes the library. |
| 27 | ~DynamicLibrary(); |
| 28 | |
| 29 | DynamicLibrary(const DynamicLibrary&) = delete; |
| 30 | DynamicLibrary(DynamicLibrary&&) = delete; |
| 31 | |
| 32 | DynamicLibrary& operator=(const DynamicLibrary&) = delete; |
| 33 | DynamicLibrary& operator=(DynamicLibrary&&) = delete; |
| 34 | |
| 35 | DynamicLibrary& operator=(void*); |
| 36 | |
| 37 | // Returns the specified library name with the platform-specific suffix added. |
| 38 | static std::string GetUnprefixedFilename(const char* filename); |
| 39 | |
| 40 | // Returns the specified library name in platform-specific format. |
| 41 | // Major/minor versions will not be included if set to -1. |
| 42 | // If libname already contains the "lib" prefix, it will not be added again. |
| 43 | // Windows: LIBNAME-MAJOR-MINOR.dll |
| 44 | // Linux: libLIBNAME.so.MAJOR.MINOR |
| 45 | // Mac: libLIBNAME.MAJOR.MINOR.dylib |
| 46 | static std::string GetVersionedFilename(const char* libname, int major = -1, int minor = -1); |
| 47 | |
| 48 | // Returns true if a module is loaded, otherwise false. |
| 49 | bool IsOpen() const { return m_handle != nullptr; } |
| 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 | { |
| 67 | *ptr = reinterpret_cast<T>(GetSymbolAddress(name)); |
| 68 | return *ptr != nullptr; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | // Platform-dependent data type representing a dynamic library handle. |
| 73 | void* m_handle = nullptr; |
| 74 | }; |
| 75 | |
| 76 | } // namespace Common |