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
143 lines (111 loc) · 2.3 KB
/
Module.cpp
File metadata and controls
143 lines (111 loc) · 2.3 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
#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 &module) : lib_handle(module.lib_handle)
{
}
Module::Module(Module &&module) : lib_handle(module.lib_handle)
{
module.lib_handle = nullptr;
}
bool Module::open(const std::string &libPath)
{
if (is_open() )
{
close();
}
#ifdef WIN32
lib_handle = ::LoadLibrary(libPath.c_str() );
#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 &module)
{
if (*this != module)
{
close();
lib_handle = module.lib_handle;
}
return *this;
}
Module &Module::operator =(Module &&module)
{
if (*this != module)
{
close();
lib_handle = module.lib_handle;
module.lib_handle = nullptr;
}
return *this;
}
};