dynamic_library.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. 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. static std::string GetVersionedFilename(const char* libname, int major = -1, int minor = -1);
  35. /// Returns true if a module is loaded, otherwise false.
  36. 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. 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. 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. 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