5 #include "gutilities.h"
17 static dlhandle load_lib(
const std::string& path);
19 static void close_lib(
dlhandle handle);
22 #define ERR_DLNOTFOUND 1001
23 #define ERR_FACTORYNOTFOUND 1002
24 #define ERR_DLHANDLENOTFOUND 1003
34 std::string dlFileName;
36 bool doesFileExists(
const std::string& name) {
38 return (stat(name.c_str(), &buffer) == 0);
41 std::shared_ptr<GLogger> log;
48 DynamicLib(std::shared_ptr<GLogger> logger, std::string path) : dlFileName(path), log(logger),
handle(nullptr) {
49 log->debug(CONSTRUCTOR,
"Instantiating ", path);
50 log->debug(NORMAL,
"Trying ", dlFileName);
53 if (!doesFileExists(dlFileName)) {
54 log->debug(NORMAL, dlFileName,
" not found...");
56 std::filesystem::path gemcRoot = gutilities::gemc_root();
57 dlFileName = gemcRoot.string() +
"/lib/" + path;
59 log->debug(NORMAL,
"Trying ", dlFileName);
62 if (doesFileExists(dlFileName)) {
63 handle = load_lib(dlFileName);
65 char const*
const dlopen_error = dlerror();
69 else { log->info(0,
"Loaded ", dlFileName); }
71 else { log->error(
ERR_DLNOTFOUND,
"could not find ", dlFileName); }
79 log->debug(DESTRUCTOR,
"Destroying ", dlFileName);
87 dlhandle load_lib(
const std::string& lib)
92 if (lib.find(
'/') != std::string::npos) { h = dlopen(lib.c_str(), RTLD_NOW); }
95 std::string cwdPath =
"./" + lib;
96 h = dlopen(cwdPath.c_str(), RTLD_NOW);
100 if (!h) { h = dlopen(lib.c_str(), RTLD_NOW); }
105 static void close_lib(
dlhandle handle) { dlclose(handle); }
#define ERR_DLHANDLENOTFOUND
Structure to load dynamically symbols from a shared library.
DynamicLib(std::shared_ptr< GLogger > logger, std::string path)