diff --git a/.cache/clangd/index/Main.cpp.8DF821074DA7C9F4.idx b/.cache/clangd/index/Main.cpp.8DF821074DA7C9F4.idx index a9e1f8a..ce9edb7 100644 Binary files a/.cache/clangd/index/Main.cpp.8DF821074DA7C9F4.idx and b/.cache/clangd/index/Main.cpp.8DF821074DA7C9F4.idx differ 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)