Files
salmanoff/hcore/senseApis/senseApiManager.cpp
T

65 lines
1.7 KiB
C++
Raw Normal View History

#include <iostream>
#include <stdexcept>
#include <optional>
#include <senseApis/senseApiManager.h>
#include <senseApis/senseApiLib.h>
namespace hk {
namespace sense_api {
SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
{
auto lib = std::make_unique<SenseApiLib>(libraryPath);
// Clear any existing error
dlerror();
lib->handle.reset(dlopen(libraryPath.c_str(), RTLD_LAZY));
if (!lib->handle)
{
std::string error = dlerror()
? dlerror()
: "Unknown error while opening shlib";
throw std::runtime_error(
std::string(__func__) + " - Cannot load library '"
+ libraryPath + "': "
+ error);
}
senseApiLibs.push_back(std::move(lib));
return *senseApiLibs.back();
}
std::optional<std::reference_wrapper<SenseApiLib>>
SenseApiManager::getSenseApiLib(const std::string& libraryPath)
{
auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(),
[&libPath = libraryPath](const std::unique_ptr<SenseApiLib>& lib) {
return lib->libraryPath == libPath;
}
);
if (it != senseApiLibs.end()) { return **it; }
return std::nullopt;
}
void SenseApiManager::unloadSenseApiLib(const std::string& libraryPath)
{
auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(),
[&lpath = libraryPath](const std::unique_ptr<SenseApiLib>& lib) {
return lib->libraryPath == lpath;
}
);
if (it != senseApiLibs.end())
{
senseApiLibs.erase(it);
return;
}
std::cerr << std::string(__func__) + ": Library not found: "
<< libraryPath << '\n';
}
} // namespace sense_api
} // namespace hk