EHS/src/system/Open_UNX.cpp

117 lines
1.6 KiB
C++
Raw Normal View History

2024-02-05 22:25:30 -08:00
#include "ehs/system/Open_UNX.h"
#include "ehs/Log.h"
#include <dlfcn.h>
namespace ehs
{
Open::~Open()
{
if (!Open::IsInitialize())
return;
if (dlclose(hdl))
EHS_LOG_INT(LogType::ERR, 0, "Failed to close.");
2024-02-05 22:25:30 -08:00
}
Open::Open()
: hdl(nullptr)
{
}
Open::Open(Str_8 filePath)
: BaseOpen((Str_8&&)filePath), hdl(nullptr)
{
Open::Initialize();
}
Open::Open(Open&& o) noexcept
: BaseOpen((BaseOpen&&)o), hdl(o.hdl)
{
o.hdl = nullptr;
}
Open::Open(const Open& o)
: BaseOpen(o), hdl(nullptr)
{
Open::Initialize();
}
Open& Open::operator=(Open&& o) noexcept
{
if (this == &o)
return *this;
Open::Release();
BaseOpen::operator=((BaseOpen&&)o);
hdl = o.hdl;
o.hdl = nullptr;
return *this;
}
Open& Open::operator=(const Open& o)
{
if (this == &o)
return *this;
Open::Release();
BaseOpen::operator=(o);
hdl = nullptr;
Open::Initialize();
return *this;
}
void Open::Initialize()
{
if (IsInitialize())
return;
hdl = dlopen(filePath, RTLD_LAZY);
if (!hdl)
{
EHS_LOG_INT(LogType::ERR, 0, dlerror());
2024-02-05 22:25:30 -08:00
return;
}
}
void Open::Release()
{
if (!IsInitialize())
return;
if (dlclose(hdl))
EHS_LOG_INT(LogType::ERR, 0, "Failed to close.");
2024-02-05 22:25:30 -08:00
hdl = nullptr;
}
void* Open::Retrieve(Str_8 symbol)
{
if (!IsInitialize())
return nullptr;
void* func = dlsym(hdl, symbol);
if (!func)
{
dlerror();
EHS_LOG_INT(LogType::ERR, 0, "Undefined symbol, \"" + symbol + "\".");
2024-02-05 22:25:30 -08:00
Release();
return nullptr;
}
return func;
}
bool Open::IsInitialize() const
{
return hdl;
}
}