dynamic_library.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /// Moves the library.
  18. DynamicLibrary(DynamicLibrary&&) noexcept;
  19. DynamicLibrary& operator=(DynamicLibrary&&) noexcept;
  20. /// Delete copies, we can't copy a dynamic library.
  21. DynamicLibrary(const DynamicLibrary&) = delete;
  22. DynamicLibrary& operator=(const DynamicLibrary&) = delete;
  23. /// Closes the library.
  24. ~DynamicLibrary();
  25. /// Returns the specified library name with the platform-specific suffix added.
  26. [[nodiscard]] static std::string GetUnprefixedFilename(const char* filename);
  27. /// Returns the specified library name in platform-specific format.
  28. /// Major/minor versions will not be included if set to -1.
  29. /// If libname already contains the "lib" prefix, it will not be added again.
  30. /// Windows: LIBNAME-MAJOR-MINOR.dll
  31. /// Linux: libLIBNAME.so.MAJOR.MINOR
  32. /// Mac: libLIBNAME.MAJOR.MINOR.dylib
  33. [[nodiscard]] static std::string GetVersionedFilename(const char* libname, int major = -1,
  34. int minor = -1);
  35. /// Returns true if a module is loaded, otherwise false.
  36. [[nodiscard]] bool IsOpen() const {
  37. return handle != nullptr;
  38. }
  39. /// Loads (or replaces) the handle with the specified library file name.
  40. /// Returns true if the library was loaded and can be used.
  41. [[nodiscard]] bool Open(const char* filename);
  42. /// Unloads the library, any function pointers from this library are no longer valid.
  43. void Close();
  44. /// Returns the address of the specified symbol (function or variable) as an untyped pointer.
  45. /// If the specified symbol does not exist in this library, nullptr is returned.
  46. [[nodiscard]] void* GetSymbolAddress(const char* name) const;
  47. /// Obtains the address of the specified symbol, automatically casting to the correct type.
  48. /// Returns true if the symbol was found and assigned, otherwise false.
  49. template <typename T>
  50. [[nodiscard]] bool GetSymbol(const char* name, T* ptr) const {
  51. *ptr = reinterpret_cast<T>(GetSymbolAddress(name));
  52. return *ptr != nullptr;
  53. }
  54. private:
  55. /// Platform-dependent data type representing a dynamic library handle.
  56. void* handle = nullptr;
  57. };
  58. } // namespace Common