#include "ehs/system/Open_UNX.h" #include "ehs/Log.h" #include namespace ehs { Open::~Open() { if (!Open::IsInitialize()) return; if (dlclose(hdl)) EHS_LOG_INT(LogType::ERR, 0, "Failed to close."); } 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()); return; } } void Open::Release() { if (!IsInitialize()) return; if (dlclose(hdl)) EHS_LOG_INT(LogType::ERR, 0, "Failed to close."); 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 + "\"."); Release(); return nullptr; } return func; } bool Open::IsInitialize() const { return hdl; } }