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))
|
2024-04-23 22:29:49 -07:00
|
|
|
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)
|
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
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))
|
2024-04-23 22:29:49 -07:00
|
|
|
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();
|
2024-04-23 22:29:49 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|