Add DynamicLib

This commit is contained in:
Isuru Samarathunga
2025-11-05 23:55:15 +05:30
parent cdc44137e8
commit c1cad88224
4 changed files with 114 additions and 0 deletions

View File

@ -3,3 +3,7 @@ add_library(IACore STATIC imp/cpp/dummy.cpp)
target_include_directories(IACore PUBLIC inc/hpp imp/inl) target_include_directories(IACore PUBLIC inc/hpp imp/inl)
target_compile_definitions(IACore PUBLIC _CRT_SECURE_NO_WARNINGS) 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()

View File

@ -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 <https://www.gnu.org/licenses/>.
#pragma once
#include <IACore/Exception.hpp>
#include <IACore/String.hpp>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#elif defined(__linux__) || defined(__APPLE__)
#include <dlfcn.h>
#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<typename FunctionType> FunctionType GetFunction(IN CONST String &name)
{
return reinterpret_cast<FunctionType *>(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<HMODULE>(m_moduleHandle));
}
PVOID DynamicLib::GetSymbol(IN CONST String &name)
{
const auto symbol = GetProcAddress(static_cast<HMODULE>(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

View File

@ -7,6 +7,8 @@
#include <IACore/Process.hpp> #include <IACore/Process.hpp>
#include <IACore/Environment.hpp> #include <IACore/Environment.hpp>
#include <IACore/DynamicLib.hpp>
using namespace ia; using namespace ia;
template<typename _value_type> VOID print(IN CONST Span<_value_type> &s) template<typename _value_type> VOID print(IN CONST Span<_value_type> &s)