2015-12-08 04:51:55 +08:00
|
|
|
#pragma once
|
2015-10-25 23:11:52 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class DynamicLib
|
|
|
|
{
|
2016-02-11 06:34:49 +08:00
|
|
|
private:
|
|
|
|
using produce_t = typename T::produce;
|
|
|
|
using destruct_t = typename T::destruct;
|
|
|
|
|
2015-10-25 23:11:52 +08:00
|
|
|
public:
|
2016-02-11 06:34:49 +08:00
|
|
|
produce_t produce_method;
|
|
|
|
destruct_t destruct_method;
|
2015-10-25 23:11:52 +08:00
|
|
|
|
|
|
|
DynamicLib(const std::string& lib_path) :
|
|
|
|
lib_path(lib_path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-07 03:48:40 +08:00
|
|
|
// TODO debug why this doesn't work
|
|
|
|
typename T::lib_object* instance()
|
|
|
|
{
|
|
|
|
// TODO singleton, lazy, concurrency
|
2016-02-11 06:34:49 +08:00
|
|
|
// ifs are uncommented -> SEGMENTATION FAULT
|
|
|
|
// TODO debug
|
|
|
|
// if (dynamic_lib == nullptr)
|
|
|
|
this->load();
|
|
|
|
// if (dynamic_lib == nullptr)
|
|
|
|
lib_object = this->produce_method();
|
2016-02-07 03:48:40 +08:00
|
|
|
return lib_object;
|
|
|
|
}
|
|
|
|
|
2015-10-25 23:11:52 +08:00
|
|
|
void load()
|
|
|
|
{
|
|
|
|
load_lib();
|
|
|
|
load_produce_func();
|
|
|
|
load_destruct_func();
|
|
|
|
}
|
|
|
|
|
2016-02-07 03:48:40 +08:00
|
|
|
~DynamicLib()
|
|
|
|
{
|
|
|
|
if (lib_object != nullptr) {
|
|
|
|
destruct_method(lib_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 23:11:52 +08:00
|
|
|
private:
|
|
|
|
std::string lib_path;
|
|
|
|
void *dynamic_lib;
|
2016-02-07 03:48:40 +08:00
|
|
|
typename T::lib_object* lib_object;
|
2015-10-25 23:11:52 +08:00
|
|
|
|
|
|
|
void load_lib()
|
|
|
|
{
|
|
|
|
dynamic_lib = dlopen(lib_path.c_str(), RTLD_LAZY);
|
|
|
|
if (!dynamic_lib) {
|
|
|
|
throw std::runtime_error(dlerror());
|
|
|
|
}
|
|
|
|
dlerror();
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_produce_func()
|
|
|
|
{
|
2016-02-11 06:34:49 +08:00
|
|
|
produce_method = (produce_t) dlsym(
|
2015-10-25 23:11:52 +08:00
|
|
|
dynamic_lib,
|
|
|
|
T::produce_name.c_str()
|
|
|
|
);
|
|
|
|
const char* dlsym_error = dlerror();
|
|
|
|
if (dlsym_error) {
|
|
|
|
throw std::runtime_error(dlsym_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_destruct_func()
|
|
|
|
{
|
2016-02-11 06:34:49 +08:00
|
|
|
destruct_method = (destruct_t) dlsym(
|
2015-10-25 23:11:52 +08:00
|
|
|
dynamic_lib,
|
|
|
|
T::destruct_name.c_str()
|
|
|
|
);
|
|
|
|
const char *dlsym_error = dlerror();
|
|
|
|
if (dlsym_error) {
|
|
|
|
throw std::runtime_error(dlsym_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|