Browse Source

Implement UNIX library open functionality

Patrick-Christopher Mattulat 2 years ago
parent
commit
cd9eb8e38c

+ 2 - 0
CMakeLists.txt

@@ -609,6 +609,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             gtest
             gmock
             gtest_main
+            dl
             "${MODULE_NAME_OS}")
 endif ()
 
@@ -636,6 +637,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             gtest
             gmock
             gtest_main
+            dl
             "${MODULE_NAME_CORE}"
             "${MODULE_NAME_BOXING}"
             "${MODULE_NAME_ENCODING}"

+ 12 - 0
include/ls_std/os/library/RuntimeLibraryLoader.hpp

@@ -21,9 +21,21 @@ namespace ls::std::os
       explicit RuntimeLibraryLoader(ls::std::os::RuntimeLibraryLoaderParameter _parameter);
       ~RuntimeLibraryLoader() = default;
 
+      bool open();
+
     private:
 
+      #if defined(unix) || defined(__APPLE__)
+      void* handle{};
+      #endif
       ls::std::os::RuntimeLibraryLoaderParameter parameter{};
+
+      #if defined(unix) || defined(__APPLE__)
+      [[nodiscard]] bool _openUnix();
+      #endif
+      #ifdef _WIN32
+      bool _openWindows();
+      #endif
   };
 }
 

+ 23 - 0
source/ls_std/os/library/RuntimeLibraryLoader.cpp

@@ -9,6 +9,9 @@
 
 #include <ls_std/os/library/RuntimeLibraryLoader.hpp>
 #include <ls_std/core/exception/IllegalArgumentException.hpp>
+#if defined(unix) || defined(__APPLE__)
+#include <dlfcn.h>
+#endif
 
 ls::std::os::RuntimeLibraryLoader::RuntimeLibraryLoader(ls::std::os::RuntimeLibraryLoaderParameter _parameter) : parameter(::std::move(_parameter))
 {
@@ -18,4 +21,24 @@ ls::std::os::RuntimeLibraryLoader::RuntimeLibraryLoader(ls::std::os::RuntimeLibr
   }
 }
 
+bool ls::std::os::RuntimeLibraryLoader::open()
+{
+  bool opened;
+
+  #if defined(unix) || defined(__APPLE__)
+  opened = this->_openUnix();
+  #endif
+  #ifdef _WIN32
+  opened = this->_openWindows();
+  #endif
+
+  return opened;
+}
 
+#if defined(unix) || defined(__APPLE__)
+bool ls::std::os::RuntimeLibraryLoader::_openUnix()
+{
+  this->handle = dlopen(this->parameter.path.c_str(), RTLD_LAZY);
+  return this->handle != nullptr;
+}
+#endif

+ 26 - 0
test/cases/os/library/RuntimeLibraryLoaderTest.cpp

@@ -10,6 +10,7 @@
 #include <gtest/gtest.h>
 #include <ls_std/ls_std_os.hpp>
 #include <ls_std/ls_std_core.hpp>
+#include "TestHelper.hpp"
 
 namespace
 {
@@ -25,6 +26,16 @@ namespace
 
       void TearDown() override
       {}
+
+      static ls::std::os::RuntimeLibraryLoaderParameter createParameter()
+      {
+        ls::std::os::RuntimeLibraryLoaderParameter parameter{};
+        #if defined(unix) || defined(__APPLE__)
+        parameter.path = ls_std_test::TestHelper::getResourcesFolderLocation() + "libls_std_time.so";
+        #endif
+
+        return parameter;
+      }
   };
 
   TEST_F(RuntimeLibraryLoaderTest, constructor_empty_path_parameter)
@@ -40,4 +51,19 @@ namespace
                    }
                  }, ls::std::core::IllegalArgumentException);
   }
+
+  TEST_F(RuntimeLibraryLoaderTest, open)
+  {
+    ls::std::os::RuntimeLibraryLoader loader{createParameter()};
+    ASSERT_TRUE(loader.open());
+  }
+
+  TEST_F(RuntimeLibraryLoaderTest, open_no_library_found)
+  {
+    ls::std::os::RuntimeLibraryLoaderParameter parameter{};
+    parameter.path = ls_std_test::TestHelper::getResourcesFolderLocation() + "libno_exist.so";
+
+    ls::std::os::RuntimeLibraryLoader loader{parameter};
+    ASSERT_FALSE(loader.open());
+  }
 }

BIN
test/resources/libls_std_time.so