forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.cpp
More file actions
171 lines (132 loc) · 2.87 KB
/
Module.cpp
File metadata and controls
171 lines (132 loc) · 2.87 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "Module.h"
#include <iostream>
namespace HttpServer
{
Module::Module(): lib_handle(nullptr)
{
}
Module::Module(const std::string &libPath): lib_handle(nullptr)
{
open(libPath);
}
Module::Module(const Module &obj) : lib_handle(obj.lib_handle)
{
}
Module::Module(Module &&obj) : lib_handle(obj.lib_handle)
{
obj.lib_handle = nullptr;
}
bool Module::open(const std::string &libPath)
{
if (is_open() )
{
close();
}
#ifdef WIN32
const size_t pos_slash = libPath.rfind('\\');
const size_t pos_slash_back = libPath.rfind('/');
size_t pos = std::string::npos;
if (pos_slash != std::string::npos && pos_slash > pos_slash_back)
{
pos = pos_slash;
}
else if (pos_slash_back != std::string::npos)
{
pos = pos_slash_back;
}
DLL_DIRECTORY_COOKIE cookie = nullptr;
if (std::string::npos != pos)
{
std::wstring directory(libPath.cbegin(), libPath.cbegin() + pos + 1);
cookie = ::AddDllDirectory(directory.data() );
}
lib_handle = ::LoadLibraryEx(libPath.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (cookie)
{
::RemoveDllDirectory(cookie);
}
#elif POSIX
lib_handle = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL);
#else
#error "Undefine platform"
#endif
if (nullptr == lib_handle)
{
#ifdef POSIX
std::cout << ::dlerror() << std::endl;
#endif
return false;
}
return true;
}
void Module::close()
{
if (lib_handle)
{
#ifdef WIN32
::FreeLibrary(lib_handle);
#elif POSIX
::dlclose(lib_handle);
#else
#error "Undefine platform"
#endif
lib_handle = nullptr;
}
}
bool Module::find(const std::string &symbolName, void *(**addr)(void *) ) const
{
if (lib_handle)
{
#ifdef WIN32
*addr = reinterpret_cast<void *(*)(void *)>(::GetProcAddress(lib_handle, symbolName.c_str() ) );
return nullptr != *addr;
#elif POSIX
char *error = ::dlerror();
*addr = reinterpret_cast<void *(*)(void *)>(::dlsym(lib_handle, symbolName.c_str() ) );
error = ::dlerror();
return nullptr == error;
#else
#error "Undefine platform"
#endif
}
return false;
}
bool Module::find(const char *symbolName, void *(**addr)(void *) ) const
{
if (lib_handle)
{
#ifdef WIN32
*addr = reinterpret_cast<void *(*)(void *)>(::GetProcAddress(lib_handle, symbolName) );
return nullptr != *addr;
#elif POSIX
char *error = ::dlerror();
*addr = reinterpret_cast<void *(*)(void *)>(::dlsym(lib_handle, symbolName) );
error = ::dlerror();
return nullptr == error;
#else
#error "Undefine platform"
#endif
}
return false;
}
Module &Module::operator =(const Module &obj)
{
if (*this != obj)
{
close();
lib_handle = obj.lib_handle;
}
return *this;
}
Module &Module::operator =(Module &&obj)
{
if (*this != obj)
{
close();
lib_handle = obj.lib_handle;
obj.lib_handle = nullptr;
}
return *this;
}
};