forked from sivansh11/dynamic-cpp-sripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicmanager.cpp
More file actions
98 lines (88 loc) · 2.66 KB
/
dynamicmanager.cpp
File metadata and controls
98 lines (88 loc) · 2.66 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
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
90
91
92
93
94
95
96
97
98
#include "dynamicmanager.h"
#include <iostream>
#include <stdlib.h>
namespace ds
{
DynamicManager::DynamicManager() {}
DynamicManager::~DynamicManager() {}
void DynamicManager::load(const char *srcFilePath, const char *objFilePath)
{
void* handle = dlopen(objFilePath, RTLD_LAZY);
objToData[objFilePath] = {handle, srcFilePath};
if (!handle)
{
std::cerr << dlerror() << '\n';
return;
}
}
bool DynamicManager::registerSrcAndObj(const char *srcFilePath, const char *objFilePath)
{
// loadedLibs[objFilePath] = {handle, srcFilePath};
srcToObj[srcFilePath] = objFilePath;
watchList.push_back(srcFilePath);
watchers.push_back({srcFilePath});
return true;
}
void DynamicManager::addIncludePath(const char *includePath)
{
includes.push_back({includePath});
}
void* DynamicManager::getFunction(const char *objFilePath, const char *functionName)
{
if (!objToData[objFilePath].handle)
load(objToData[objFilePath].srcFilePath, objFilePath);
return dlsym(objToData.at(objFilePath).handle, functionName);
}
std::deque<std::string>& DynamicManager::getChanges()
{
return changedQueue;
}
bool DynamicManager::hasChanged(const char* file)
{
for (auto changed: changedQueue)
{
if (changed == file)
{
return true;
}
}
return false;
}
void DynamicManager::reload()
{
changedQueue.clear();
for (auto& watcher: watchers)
{
watcher.check();
if (watcher.isChanged())
{
changedQueue.push_back(watcher.filePath);
}
}
}
void DynamicManager::unload(const char *objFilePath)
{
dlclose(objToData[objFilePath].handle);
}
void DynamicManager::update()
{
for (auto& changed: changedQueue)
{
if (objToData[srcToObj[changed]].handle)
unload(srcToObj[changed].c_str());
std::cout << changed << '\n';
std::string cmd;
cmd += "g++ -fPIC -shared ";
for (auto& include: includes)
{
cmd += "-I " + include + ' ';
}
cmd += changed + " -o " + srcToObj[changed];
// std::cout << cmd << '\n';
std::cout << "DYNAMIC-SCRIPT::UPDATE::COMPILING_SCRIPT::" << changed << '\n';
std::cout << cmd << '\n';
system(cmd.c_str());
load(changed.c_str(), srcToObj[changed].c_str());
}
}
} // namespace ds