BsDynLib.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #if BS_PLATFORM == BS_PLATFORM_WIN32
  6. struct HINSTANCE__;
  7. typedef struct HINSTANCE__* hInstance;
  8. #endif
  9. namespace bs
  10. {
  11. /** @addtogroup General
  12. * @{
  13. */
  14. #if BS_PLATFORM == BS_PLATFORM_WIN32
  15. # define DYNLIB_HANDLE hInstance
  16. # define DYNLIB_LOAD( a ) LoadLibraryEx( a, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
  17. # define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
  18. # define DYNLIB_UNLOAD( a ) !FreeLibrary( a )
  19. #elif BS_PLATFORM == BS_PLATFORM_LINUX || BS_PLATFORM == BS_PLATFORM_OSX
  20. # define DYNLIB_HANDLE void*
  21. # define DYNLIB_LOAD( a ) dlopen( a, RTLD_LAZY | RTLD_GLOBAL)
  22. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  23. # define DYNLIB_UNLOAD( a ) dlclose( a )
  24. #endif
  25. /** Class that holds data about a dynamic library. */
  26. class BS_UTILITY_EXPORT DynLib
  27. {
  28. public:
  29. /** Platform-specific file extension for a dynamic library (e.g. "dll"). */
  30. static const char* EXTENSION;
  31. /** Platform-specific name suffix for a dynamic library (e.g. "lib" on Unix) */
  32. static const char* PREFIX;
  33. /** Constructs the dynamic library object and loads the library with the specified name. */
  34. DynLib(const String& name);
  35. ~DynLib();
  36. /** Loads the library. Does nothing if library is already loaded. */
  37. void load();
  38. /** Unloads the library. Does nothing if library is not loaded. */
  39. void unload();
  40. /** Get the name of the library. */
  41. const String& getName() const { return mName; }
  42. /**
  43. * Returns the address of the given symbol from the loaded library.
  44. *
  45. * @param[in] strName The name of the symbol to search for.
  46. * @return If the function succeeds, the returned value is a handle to the symbol. Otherwise null.
  47. */
  48. void* getSymbol(const String& strName) const;
  49. protected:
  50. friend class DynLibManager;
  51. /** Gets the last loading error. */
  52. String dynlibError();
  53. protected:
  54. String mName;
  55. DYNLIB_HANDLE mHandle;
  56. };
  57. /** @} */
  58. }