-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.cpp
More file actions
52 lines (46 loc) · 1.49 KB
/
Module.cpp
File metadata and controls
52 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "Module.h"
#include <stdexcept>
ModuleLoader::ModuleLoader()
: libs()
{
}
ModuleLoader::~ModuleLoader()
{
for(auto it : libs)
unload(it.first);
}
void ModuleLoader::load(const std::string& name)
{
void* handle = dlopen(std::string("./lib" + name + ".so").c_str(), RTLD_LAZY);
if(handle == nullptr)
throw std::runtime_error(
"could not load module " + name + ", error was:\n" + std::string(dlerror())
);
auto res = libs.insert(
std::make_pair(name, handle)
);
if(res.second == false)
throw std::runtime_error("module " + name + " already loaded");
// call the load function
ModuleLoad fun = reinterpret_cast<ModuleLoad>(dlsym(handle, "load"));
(*fun)();
}
void ModuleLoader::unload(const std::string& name)
{
auto lib = libs.find(name);
if(lib == libs.end())
throw std::runtime_error("module " + name + " cannot be unloaded for it was not loaded");
void* handle = lib->second;
ModuleUnload fun = reinterpret_cast<ModuleLoad>(dlsym(handle, "unload"));
(*fun)();
dlclose(handle);
}
void ModuleLoader::call(const std::string& lib, const std::string& sub, Data::XStack& stack,
SharedData& d)
{
auto handle = libs.find(lib);
if(handle == libs.end())
throw std::runtime_error(sub + " cannot be called for " + lib + " was not loaded");
ModuleCall fun = reinterpret_cast<ModuleCall>(dlsym(handle->second, "call"));
(*fun)(sub.c_str(), stack, d);
}