From c1cad882244eb08f5601738d0b07ee29b95e8384 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Wed, 5 Nov 2025 23:55:15 +0530 Subject: [PATCH] Add DynamicLib --- .../index/Main.cpp.8DF821074DA7C9F4.idx | Bin 1296 -> 1858 bytes Src/IACore/CMakeLists.txt | 4 + Src/IACore/inc/hpp/IACore/DynamicLib.hpp | 108 ++++++++++++++++++ Src/IACoreTest/imp/cpp/Main.cpp | 2 + 4 files changed, 114 insertions(+) create mode 100644 Src/IACore/inc/hpp/IACore/DynamicLib.hpp diff --git a/.cache/clangd/index/Main.cpp.8DF821074DA7C9F4.idx b/.cache/clangd/index/Main.cpp.8DF821074DA7C9F4.idx index a9e1f8a5bf6ed185db88869d19c587f5f00e091e..ce9edb736db84f56c34aa875432b9ffa712c4d63 100644 GIT binary patch delta 701 zcmbQhb%;+c$kWZuik*SMImNRgH?<^@g@J)V7>J8YiZbW2Y?NEe#O<0_mRXdamz$bb zGWk4{*W}Yoer!JZ>FKFOK<;$*fTH~5)Z${WR2p+hy&MCB47UomCYS)41O^@FHMe?b zOg+fMAPbZhWD(>56ENwb)U@JFj0_C%J)*K&yzBTFd1Sbaxy{u-?u=TcwcZp)GjgFA zT0Kc7{H0%}J5V*B0G|j%RsDL?)jQrE$vjyS2Qo#M+X!Zf%Bj2GBAzlQfRuCa@xct8 z+s@9#wfuA%6OW9XxttB$r7{ZU3UJ=h4K)G_vX2#mjFjhAh8g)g$0_0slhVK%VH$VRl1LZTwf YoE%(&EUawYOuXED?EIoU0*t~80BdBvF8}}l delta 347 zcmX@aH-Sqo$kWY@gO!27ImNRgH?<^@g@J)V7>J8YiZYioZ-gSJ8 zJd&IuoD%9EcSfz!T5r0g@oRT~2HD`C=%TqrL2JbiY~OKsU7Ak}=FAa8;}QEE;ih*4aWT&xdbh)ur0CTcAK zVuAs)B*WjV diff --git a/Src/IACore/CMakeLists.txt b/Src/IACore/CMakeLists.txt index 6799b23..750c763 100644 --- a/Src/IACore/CMakeLists.txt +++ b/Src/IACore/CMakeLists.txt @@ -3,3 +3,7 @@ add_library(IACore STATIC imp/cpp/dummy.cpp) target_include_directories(IACore PUBLIC inc/hpp imp/inl) target_compile_definitions(IACore PUBLIC _CRT_SECURE_NO_WARNINGS) + +if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") + target_link_libraries(IACore PUBLIC dl) +endif() diff --git a/Src/IACore/inc/hpp/IACore/DynamicLib.hpp b/Src/IACore/inc/hpp/IACore/DynamicLib.hpp new file mode 100644 index 0000000..f67ec08 --- /dev/null +++ b/Src/IACore/inc/hpp/IACore/DynamicLib.hpp @@ -0,0 +1,108 @@ +// IACore-OSS; The Core Library for All IA Open Source Projects +// Copyright (C) 2024 IAS (ias@iasoft.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#pragma once + +#include +#include + +#if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN +#include +#elif defined(__linux__) || defined(__APPLE__) +#include +#endif + +namespace ia +{ + class DynamicLib + { + public: + STATIC INLINE DynamicLib Load(IN CONST String &searchPath, IN CONST String &name); + + INLINE ~DynamicLib(); + + public: + INLINE PVOID GetSymbol(IN CONST String &name); + + template FunctionType GetFunction(IN CONST String &name) + { + return reinterpret_cast(GetSymbol(name)); + } + + private: + INLINE DynamicLib(IN PVOID moduleHandle); + + private: + CONST PVOID m_moduleHandle; + }; +} // namespace ia + +namespace ia +{ + DynamicLib::DynamicLib(IN PVOID moduleHandle) : m_moduleHandle(moduleHandle) + { + } + +#if defined(_WIN32) + DynamicLib DynamicLib::Load(IN CONST String &searchPath, IN CONST String &name) + { + const auto handle = LoadLibraryA(BuildString(searchPath, "/", name, ".dll").c_str()); + if (handle == NULL) + THROW_UNKNOWN("DynamicLib: Failed to load the library \"", name, "\" with error: ", GetLastError()); + return DynamicLib((PVOID) handle); + } + + DynamicLib::~DynamicLib() + { + if (m_moduleHandle) + FreeLibrary(static_cast(m_moduleHandle)); + } + + PVOID DynamicLib::GetSymbol(IN CONST String &name) + { + const auto symbol = GetProcAddress(static_cast(m_moduleHandle), name.c_str()); + if (symbol == NULL) + THROW_UNKNOWN("DynamicLib: Failed to find the symbol \"", name, "\" with error: ", GetLastError()); + return (PVOID) symbol; + } +#else + DynamicLib DynamicLib::Load(IN CONST String &searchPath, IN CONST String &name) + { + dlerror(); + const auto handle = dlopen(BuildString(searchPath, "/", name, ".so").c_str(), RTLD_LAZY); + if (handle == NULL) + THROW_UNKNOWN("DynamicLib: Failed to load the library \"", name, "\" with error: ", dlerror()); + return DynamicLib((PVOID) handle); + } + + DynamicLib::~DynamicLib() + { + if (m_moduleHandle) + dlclose(m_moduleHandle); + } + + PVOID DynamicLib::GetSymbol(IN CONST String &name) + { + dlerror(); + const auto symbol = dlsym(handle, name.c_str()); + const char *dlsym_error = dlerror(); + if (dlsym_error) + THROW_UNKNOWN("DynamicLib: Failed to find the symbol \"", name, "\" with error: ", dlerror()); + return (PVOID)symbol; + } +#endif +} // namespace ia \ No newline at end of file diff --git a/Src/IACoreTest/imp/cpp/Main.cpp b/Src/IACoreTest/imp/cpp/Main.cpp index d0517e2..7aad1ae 100644 --- a/Src/IACoreTest/imp/cpp/Main.cpp +++ b/Src/IACoreTest/imp/cpp/Main.cpp @@ -7,6 +7,8 @@ #include #include +#include + using namespace ia; template VOID print(IN CONST Span<_value_type> &s)