dynamic_library.h 2.9 KB

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