dynamic_library.h 2.8 KB

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