1
0

mptLibrary.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * mptLibrary.h
  3. * ------------
  4. * Purpose: Shared library handling.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. OPENMPT_NAMESPACE_BEGIN
  12. namespace mpt
  13. {
  14. typedef void* (*FuncPtr)(); // pointer to function returning void*
  15. class LibraryHandle;
  16. enum class LibrarySearchPath
  17. {
  18. Invalid,
  19. Default,
  20. Application,
  21. System,
  22. FullPath,
  23. };
  24. class LibraryPath
  25. {
  26. private:
  27. mpt::LibrarySearchPath searchPath;
  28. mpt::PathString fileName;
  29. private:
  30. LibraryPath(mpt::LibrarySearchPath searchPath, const mpt::PathString &fileName);
  31. public:
  32. mpt::LibrarySearchPath GetSearchPath() const;
  33. mpt::PathString GetFileName() const;
  34. public:
  35. // "lib" on Unix-like systems, "" on Windows
  36. static mpt::PathString GetDefaultPrefix();
  37. // ".so" or ".dylib" or ".dll"
  38. static mpt::PathString GetDefaultSuffix();
  39. // Returns the library path in the application directory, with os-specific prefix and suffix added to basename.
  40. // e.g.: basename = "unmo3" --> "libunmo3.so" / "apppath/unmo3.dll"
  41. static LibraryPath App(const mpt::PathString &basename);
  42. // Returns the library path in the application directory, with os-specific suffix added to fullname.
  43. // e.g.: fullname = "libunmo3" --> "libunmo3.so" / "apppath/libunmo3.dll"
  44. static LibraryPath AppFullName(const mpt::PathString &fullname);
  45. // Returns a system library name with os-specific prefix and suffix added to basename, but without any full path in order to be searched in the default search path.
  46. // e.g.: basename = "unmo3" --> "libunmo3.so" / "unmo3.dll"
  47. static LibraryPath System(const mpt::PathString &basename);
  48. // Returns a system library name with os-specific suffix added to path.
  49. // e.g.: path = "somepath/foo" --> "somepath/foo.so" / "somepath/foo.dll"
  50. static LibraryPath FullPath(const mpt::PathString &path);
  51. };
  52. class Library
  53. {
  54. protected:
  55. std::shared_ptr<LibraryHandle> m_Handle;
  56. public:
  57. Library();
  58. Library(const mpt::LibraryPath &path);
  59. public:
  60. void Unload();
  61. bool IsValid() const;
  62. FuncPtr GetProcAddress(const std::string &symbol) const;
  63. template <typename Tfunc>
  64. bool Bind(Tfunc * & f, const std::string &symbol) const
  65. {
  66. #if !(MPT_OS_WINDOWS && MPT_COMPILER_GCC)
  67. // MinGW64 std::is_function is always false for non __cdecl functions.
  68. // See https://connect.microsoft.com/VisualStudio/feedback/details/774720/stl-is-function-bug .
  69. static_assert(std::is_function<Tfunc>::value);
  70. #endif
  71. const FuncPtr addr = GetProcAddress(symbol);
  72. f = reinterpret_cast<Tfunc*>(addr);
  73. return (addr != nullptr);
  74. }
  75. };
  76. } // namespace mpt
  77. OPENMPT_NAMESPACE_END