RuntimeLibraryLoader.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-09-06
  6. * Changed: 2022-10-20
  7. *
  8. * */
  9. #include <ls_std/os/library/RuntimeLibraryLoader.hpp>
  10. #include <ls_std/core/exception/IllegalArgumentException.hpp>
  11. #if defined(unix) || defined(__APPLE__)
  12. #include <dlfcn.h>
  13. #endif
  14. ls::std::os::RuntimeLibraryLoader::RuntimeLibraryLoader(ls::std::os::RuntimeLibraryLoaderParameter _parameter) : parameter(::std::move(_parameter))
  15. {
  16. if (this->parameter.path.empty())
  17. {
  18. throw ls::std::core::IllegalArgumentException{};
  19. }
  20. }
  21. bool ls::std::os::RuntimeLibraryLoader::close()
  22. {
  23. return this->_close();
  24. }
  25. void *ls::std::os::RuntimeLibraryLoader::getSymbol(const ::std::string &_symbolName)
  26. {
  27. #if defined(unix) || defined(__APPLE__)
  28. return this->_getSymbolUnix(_symbolName);
  29. #endif
  30. }
  31. bool ls::std::os::RuntimeLibraryLoader::hasSymbol(const ::std::string &_symbolName)
  32. {
  33. return this->_hasSymbolUnix(_symbolName);
  34. }
  35. bool ls::std::os::RuntimeLibraryLoader::loadSymbol(const ::std::string &_symbolName)
  36. {
  37. bool loaded{};
  38. #if defined(unix) || defined(__APPLE__)
  39. loaded = this->_loadSymbolUnix(_symbolName);
  40. #endif
  41. return loaded;
  42. }
  43. bool ls::std::os::RuntimeLibraryLoader::open()
  44. {
  45. bool opened;
  46. #if defined(unix) || defined(__APPLE__)
  47. opened = this->_openUnix();
  48. #endif
  49. #ifdef _WIN32
  50. opened = this->_openWindows();
  51. #endif
  52. return opened;
  53. }
  54. #if defined(unix) || defined(__APPLE__)
  55. bool ls::std::os::RuntimeLibraryLoader::_close()
  56. {
  57. bool closed{};
  58. if (this->handle != nullptr)
  59. {
  60. closed = dlclose(this->handle) == 0;
  61. }
  62. return closed;
  63. }
  64. void *ls::std::os::RuntimeLibraryLoader::_getSymbolUnix(const ::std::string &_symbolName)
  65. {
  66. void* symbol{};
  67. if (this->_hasSymbolUnix(_symbolName))
  68. {
  69. symbol = this->symbols.at(_symbolName);
  70. }
  71. return symbol;
  72. }
  73. bool ls::std::os::RuntimeLibraryLoader::_hasSymbolUnix(const ::std::string &_symbolName)
  74. {
  75. return this->symbols.find(_symbolName) != this->symbols.end();
  76. }
  77. bool ls::std::os::RuntimeLibraryLoader::_loadSymbolUnix(const ::std::string& _symbolName)
  78. {
  79. bool loaded{};
  80. if (!this->_hasSymbolUnix(_symbolName))
  81. {
  82. void* symbol = dlsym(this->handle, _symbolName.c_str());
  83. this->symbols.insert(::std::pair<::std::string, void*>(_symbolName, symbol));
  84. loaded = symbol != nullptr;
  85. }
  86. return loaded;
  87. }
  88. bool ls::std::os::RuntimeLibraryLoader::_openUnix()
  89. {
  90. this->handle = dlopen(this->parameter.path.c_str(), RTLD_LAZY);
  91. return this->handle != nullptr;
  92. }
  93. #endif