CmDynLib.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmString.h"
  4. #if CM_PLATFORM == CM_PLATFORM_WIN32
  5. # define DYNLIB_HANDLE hInstance
  6. # define DYNLIB_LOAD( a ) LoadLibraryEx( a, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
  7. # define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
  8. # define DYNLIB_UNLOAD( a ) !FreeLibrary( a )
  9. struct HINSTANCE__;
  10. typedef struct HINSTANCE__* hInstance;
  11. #elif CM_PLATFORM == CM_PLATFORM_LINUX
  12. # define DYNLIB_HANDLE void*
  13. # define DYNLIB_LOAD( a ) dlopen( a, RTLD_LAZY | RTLD_GLOBAL)
  14. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  15. # define DYNLIB_UNLOAD( a ) dlclose( a )
  16. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  17. # define DYNLIB_HANDLE void*
  18. # define DYNLIB_LOAD( a ) mac_loadDylib( a )
  19. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  20. # define DYNLIB_UNLOAD( a ) dlclose( a )
  21. #endif
  22. namespace CamelotFramework
  23. {
  24. /**
  25. * @brief Class that holds data about a dynamic library.
  26. */
  27. class CM_UTILITY_EXPORT DynLib
  28. {
  29. public:
  30. ~DynLib();
  31. /**
  32. * @brief Loads the library. Does nothing if library is already loaded.
  33. */
  34. void load();
  35. /**
  36. * @brief Unloads the library. Does nothing if library is not loaded.
  37. */
  38. void unload();
  39. /**
  40. * @brief Get the name of the library.
  41. */
  42. const String& getName() const { return mName; }
  43. /**
  44. * @brief Returns the address of the given symbol from the loaded library.
  45. *
  46. * @param strName The name of the symbol to search for.
  47. *
  48. * @returns If the function succeeds, the returned value is a handle to
  49. * the symbol. Otherwise null.
  50. */
  51. void* getSymbol(const String& strName) const;
  52. protected:
  53. friend class DynLibManager;
  54. DynLib(const String& name);
  55. /**
  56. * @brief Gets the last loading error.
  57. */
  58. String dynlibError();
  59. protected:
  60. String mName;
  61. DYNLIB_HANDLE m_hInst; // Handle to the loaded library.
  62. };
  63. }