-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodulemanager.cpp
89 lines (74 loc) · 1.78 KB
/
modulemanager.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "modulemanager.h"
#include "directoryinfo.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
namespace openrc {
ModuleManager :: ModuleManager()
{}
ModuleManager :: ~ModuleManager()
{}
void ModuleManager :: RegisterModule(const std::string& moduleName, IModule* module)
{
_modules.insert(std::make_pair(moduleName, module));
}
void ModuleManager :: InitializeAll()
{
for (auto& pair : _modules)
{
pair.second->InitialiseModule();
}
}
void ModuleManager :: ShutdownAll()
{
for (auto& pair : _modules)
{
pair.second->ShutdownModule();
}
}
void ModuleManager::LoadModuleFromFile(const std::string& filepath)
{
#ifdef _WIN32
HMODULE module = LoadLibraryA(filepath.c_str());
#else
void* module = dlopen(filepath.c_str(), RTLD_LAZY);
#endif
typedef IModule* (*ModuleGeter)();
if (module)
{
#ifdef _WIN32
ModuleGeter getFunction = (ModuleGeter)GetProcAddress(module, "GetModule");
#else
ModuleGeter getFunction = (ModuleGeter)dlsym(module, "GetModule");
#endif
if (getFunction)
{
IModule* moduleInstance = getFunction();
if (moduleInstance != nullptr)
{
RegisterModule(moduleInstance->GetModuleName(), moduleInstance);
}
}
}
}
void ModuleManager::LoadModulesFromFolder(const std::string& folderPath)
{
auto files = DirectoryInfo::GetFiles(folderPath);
for (auto& file : files)
{
if (file.Extenstion() == "dll" || file.Extenstion() == "so")
{
const std::string& path = file.GetFullPath();
LoadModuleFromFile(path);
}
}
}
ModuleManager& ModuleManager::Instance()
{
static ModuleManager moduleManager;
return moduleManager;
}
}
// End http.