-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
374 lines (316 loc) · 14 KB
/
main.cpp
File metadata and controls
374 lines (316 loc) · 14 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <iostream>
#include <string>
#include "dynamic_library/dynamic_library.hpp"
/*
* 为了在没有头文件的情况下调用 libdynamic.so 中的内容,你需要使用 动态链接库的运行时加载机制,
* 即通过 dlopen、dlsym 等函数(在 POSIX 系统上)或等效的方法(在 Windows 上,如 LoadLibrary 和 GetProcAddress)。
*/
/// =========== 定义动态库中函数指针类型 start ===========
using sayHello_func = void (*)(); // 函数指针类型
using intAdd_func = int (&)(int, int); // 函数左值引用类型
using floatAdd_func = float (&&)(float, float); // 函数右值引用类型
using doubleAdd_func = double(double, double); // 函数类型
// 动态库中的struct
struct point_t
{
double x;
double y;
double z;
};
struct box_t
{
int id; // box 编号
char name[64]; // box 名称,固定长度字符串
point_t min; // 最小点
point_t max; // 最大点
};
using getPoint_func = point_t (*)();
using printPoint_func = void (*)(point_t);
// 函数指针类型定义
typedef void (*double_callback_t)(double x, double y, double z); // 简单函数回调
typedef void (*point_callback_t)(point_t p); // 按值传递 point_t
typedef void (*box_callback_t)(box_t *p); // 指针传递 box_t
/// =========== 定义动态库中函数指针类型 end ===========
void func();
void testHasSymbol(const dll::dynamic_library &lib);
void testGetVariable(const dll::dynamic_library &lib);
void testGetVariable2(const dll::dynamic_library &lib);
void testNotExistSymbol(const dll::dynamic_library &lib);
void testCallback(const dll::dynamic_library &lib);
void testNullLibrary();
int main()
{
std::cout << "====================================================" << std::endl;
func();
std::cout << "====================================================" << std::endl;
return 0;
}
/// @brief 使用封装的动态库加载流程
void func()
{
try
{
const std::string libPath =
#if defined(_WIN32) || defined(_WIN64)
"dynamic.dll";
#else
"./libdynamic.so";
#endif
// 加载动态库
dll::dynamic_library lib0(libPath);
dll::dynamic_library lib1(libPath);
// dll::dynamic_library lib = lib0; 错误: 禁止拷贝构造
// lib0 = lib1; 错误: 禁止拷贝赋值
lib0 = std::move(lib1); // 支持移动赋值
dll::dynamic_library lib(std::move(lib0)); // 支持移动构造
if (lib)
{
std::cout << "lib is vaild." << std::endl;
}
// 加载函数符号
auto sayHello = lib.get<sayHello_func>("sayHello");
auto intAdd = lib.get<intAdd_func>("intAdd");
auto floatAdd = lib.get<floatAdd_func>("floatAdd");
auto doubleAdd = lib.get<doubleAdd_func>("doubleAdd");
auto getPoint = lib.get<getPoint_func>("getPoint");
auto printPoint = lib.get<printPoint_func>("printPoint");
// 直接调用函数符号
int ret = lib.invoke<int(int, int)>("intAdd", 1, 2);
double ret2 = lib.invoke<double(double, double)>("doubleAdd", 1.8, 2.5);
ret = lib.invoke<int(int, int)>("intAdd", 2, 3);
ret = lib.invoke<int(int, int)>("intAdd", 3, 4);
ret = lib.invoke<int (*)(int, int)>("intAdd", 4, 5);
ret = lib.invoke<int (*)(int, int)>("intAdd", 5, 6);
ret = lib.invoke<int (&)(int, int)>("intAdd", 6, 7);
ret = lib.invoke<int (&)(int, int)>("intAdd", 7, 8);
ret = lib.invoke<int (&&)(int, int)>("intAdd", 8, 9);
// ret = lib.invoke<int>("g_counter"); // ❌你想获取变量, 这个模板不允许, 请使用 get_variable
double ret3 = lib.invoke_uncached<double(double, double)>("doubleAdd", 1.8, 2.5);
std::cout << "invoke: intAdd(8, 9) = " << ret << std::endl;
std::cout << "invoke: doubleAdd(1.8, 2.5) = " << ret2 << std::endl;
std::cout << "invoke_uncached: doubleAdd(1.8, 2.5) = " << ret3 << std::endl;
// 调用函数
sayHello();
int a = 5, b = 3;
std::cout << "intAdd(" << a << ", " << b << ") = " << intAdd(a, b) << std::endl;
float fa = 1.5f, fb = 2.3f;
std::cout << "floatAdd(" << fa << ", " << fb << ") = " << floatAdd(fa, fb) << std::endl;
double da = 3.14159, db = 2.71828;
std::cout << "doubleAdd(" << da << ", " << db << ") = " << doubleAdd(da, db) << std::endl;
point_t p = getPoint();
std::cout << "getPoint() = {x: " << p.x << ", y: " << p.y << ", z: " << p.z << "}" << std::endl;
std::cout << "printPoint() output: ";
printPoint(p);
std::cout << std::endl;
testHasSymbol(lib);
testGetVariable(lib);
testGetVariable2(lib);
testNullLibrary();
testNotExistSymbol(lib);
testCallback(lib);
}
catch (const std::exception &ex)
{
std::cerr << "Error: " << ex.what() << std::endl;
return;
}
}
void testNullLibrary()
{
std::cout << "--------- testNullLibrary ----------" << std::endl;
dll::dynamic_library lib; // 默认构造函数创建一个空的动态库对象
lib.unload(); // 显式释放资源, 但此时 handle_ 仍然是 nullptr
if (!lib)
{
std::cout << "lib is not valid." << std::endl;
}
else
{
std::cout << "lib is valid." << std::endl;
}
try
{
lib.get<intAdd_func>("intAdd"); // 尝试获取一个函数符号, 会抛出异常
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
std::cout << "--------- testNullLibrary ----------" << std::endl;
}
void testHasSymbol(const dll::dynamic_library &lib)
{
std::cout << "------ testHasSymbol ------" << std::endl;
std::cout << "has_symbol(\"intAdd\"): " << lib.has_symbol("intAdd") << std::endl;
std::cout << "has_symbol(\"g_version\"): " << lib.has_symbol("g_version") << std::endl;
std::cout << "has_symbol(\"non_exist\"): " << lib.has_symbol("non_exist") << std::endl;
std::cout << "has_symbol(\"floatAdd\"): " << lib.has_symbol("floatAdd") << std::endl;
std::cout << "has_symbol(\"g_point\"): " << lib.has_symbol("g_point") << std::endl;
std::cout << "has_symbol(\"g_point_ptr\"): " << lib.has_symbol("g_point_ptr") << std::endl;
std::cout << "has_symbol(\"g_point_ptr\"): " << lib.has_symbol("g_point_ptr") << std::endl;
std::cout << "has_symbol(\"g_point_ptr\"): " << lib.has_symbol("g_point_ptr") << std::endl;
std::cout << "has_symbol(\"g_point_ptr\"): " << lib.has_symbol("g_point_ptr") << std::endl;
std::cout << "has_symbol(\"g_point_ptr0\"): " << lib.has_symbol("g_point_ptr1") << std::endl;
std::cout << "------ testHasSymbol ------" << std::endl;
}
/// @brief 测试获取动态库中的变量
void testGetVariable(const dll::dynamic_library &lib)
{
std::cout << "--------- testGetVariable ----------" << std::endl;
// 获取动态库版本号
const char *version = lib.get_variable<const char *>("g_version");
std::cout << "[get_variable] Dynamic Library Version: " << version << std::endl;
// 获取动态库变量
int counter = lib.get_variable<int>("g_counter");
std::cout << "[get_variable] g_counter value = " << counter << std::endl;
// 获取动态库指针变量
int *counter_ptr = lib.get_variable<int *>("g_counter_ptr");
std::cout << "[get_variable] g_counter_ptr value = " << *counter_ptr << std::endl;
// 直接修改动态库中变量的值
*counter_ptr = 101;
// 获取动态库结构体变量
point_t &point = lib.get_variable<point_t>("g_point");
std::cout << "[get_variable] g_point value x = " << point.x << ", y = " << point.y << ", z = " << point.z
<< std::endl;
// 直接修改动态库中变量的值, point是引用
point.x = 8;
// 获取动态库结构体指针变量
point_t *point_ptr = lib.get_variable<point_t *>("g_point_ptr");
std::cout << "[get_variable] g_point_ptr value x = " << point_ptr->x << ", y = " << point_ptr->y
<< ", z = " << point_ptr->z << std::endl;
// // 错误用法:传函数类型
// auto& f = lib.get_variable<void()>("my_function"); // ❌你想获取函数, 这个模板不允许, 请使用 get
//////////////////////// try_get_variable
// 获取动态库版本号
const char **version2 = lib.try_get_variable<const char *>("g_version");
std::cout << "[try_get_variable] Dynamic Library Version: " << *version2 << std::endl;
// 获取动态库变量
int *counter2 = lib.try_get_variable<int>("g_counter");
std::cout << "[try_get_variable] g_counter value = " << *counter2 << std::endl;
// 获取动态库结构体变量
point_t *point2 = lib.try_get_variable<point_t>("g_point");
std::cout << "[try_get_variable] g_point value x = " << point2->x << ", y = " << point2->y << ", z = " << point2->z
<< std::endl;
std::cout << "--------- testGetVariable ----------" << std::endl;
}
void testGetVariable2(const dll::dynamic_library &lib)
{
std::cout << "--------- testGetVariable2 ----------" << std::endl;
// 获取动态库版本号, 注意需要是 const char **
const char **version = lib.get<const char **>("g_version");
const char *ver = lib.get_variable<const char *>("g_version");
std::cout << "g_version ptr = " << static_cast<const void *>(version) << std::endl;
if (version)
{
std::cout << "Dynamic Library Version: " << *version << ", " << ver << std::endl;
}
else
{
std::cout << "Failed to load version string" << std::endl;
}
// 获取动态库变量
int *counter = lib.get<int *>("g_counter");
std::cout << "g_counter addr = " << static_cast<void *>(counter) << ", value = " << (counter ? *counter : -1)
<< std::endl;
// 获取动态库指针变量
int **counter_ptr = lib.get<int **>("g_counter_ptr");
std::cout << "g_counter_ptr addr = " << static_cast<void *>(counter_ptr)
<< ", value = " << (counter_ptr && *counter_ptr ? **counter_ptr : -1) << std::endl;
// 获取动态库结构体变量
point_t *point = lib.get<point_t>("g_point");
std::cout << "g_point addr = " << static_cast<void *>(point);
if (point) std::cout << ", value = (" << point->x << ", " << point->y << ", " << point->z << ")";
std::cout << std::endl;
// 获取动态库结构体指针变量
point_t **point_ptr = lib.get<point_t **>("g_point_ptr");
std::cout << "g_point_ptr addr = " << static_cast<void *>(point_ptr);
if (point_ptr && *point_ptr)
std::cout << ", value = (" << (*point_ptr)->x << ", " << (*point_ptr)->y << ", " << (*point_ptr)->z << ")";
std::cout << std::endl;
std::cout << "--------- testGetVariable2 ----------" << std::endl;
}
/// @brief 测试符号信息不存在的情况
void testNotExistSymbol(const dll::dynamic_library &lib)
{
std::cout << "---------testNotExistSymbol----------" << std::endl;
// 测试不存在的函数符号加载
auto unknownFunc = lib.try_get<printPoint_func>("notExistFunc"); // 加载失败不抛异常,返回nullptr
if (unknownFunc == nullptr)
{
std::cout << "lib.try_get<printPoint_func>(\"notExistFunc\"); load failed, return nullptr." << std::endl;
}
try
{
auto unknownFunc2 = lib.get<printPoint_func>("notExistFunc"); // 加载失败抛出异常
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
try
{
// 正常调用: symbol是对的, 函数签名也正常
double ret = lib.invoke<double(double, double)>("doubleAdd", 1.5, 3.0);
std::cout << "lib.invoke ret = " << ret << std::endl;
// 未定义行为: symbol是对的,但是函数签名不一致
double ret2 = lib.invoke<double(double, double, double)>("doubleAdd", 1.5, 3.0, 1.0);
std::cout << "[UB] lib.invoke ret2 = " << ret2 << std::endl;
// 未定义行为: symbol是对的,但是函数签名不一致
double ret3 = lib.invoke<double()>("doubleAdd");
std::cout << "[UB] lib.invoke ret3 = " << ret3 << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "invoke error: " << e.what() << '\n';
}
std::cout << "---------testNotExistSymbol----------" << std::endl;
}
typedef void (*double_callback_t)(double x, double y, double z); // 简单函数回调
typedef void (*point_callback_t)(point_t p); // 按值传递 point_t
typedef void (*box_callback_t)(box_t *p); // 指针传递 box_t
/// 自定义的回调函数
void my_double_callback(double x, double y, double z)
{
std::cout << "[my_double_callback]: " << x * x << ", " << y * y << ", " << z * z << std::endl;
}
void my_point_callback(point_t p)
{
std::cout << "[my_point_callback]: (" << p.x << ", " << p.y << ", " << p.z << ")" << std::endl;
}
void my_box_callback(box_t *p)
{
if (p)
{
std::cout << "[my_box_callback]: (" << p->min.x << ", " << p->min.y << ", " << p->min.z << ") - (" << p->max.x
<< ", " << p->max.y << ", " << p->max.z << ")" << std::endl;
}
}
void testCallback(const dll::dynamic_library &lib)
{
std::cout << "---------testCallback----------" << std::endl;
auto fn_hellostr = lib.get<const char *()>("getHelloString");
auto fn_getbox = lib.get<box_t()>("getBox");
box_t box = fn_getbox();
// 函数签名一定要正确
auto fn_box2String = lib.get<void(box_t, char *, unsigned int)>("box2String");
auto fn_point2String = lib.get<void(point_t *, char *, unsigned int)>("point2String");
std::cout << "getHelloString: " << fn_hellostr() << std::endl;
std::string boxStr(256, '\0');
fn_box2String(box, const_cast<char *>(boxStr.data()), boxStr.size());
std::cout << "box2String: " << boxStr << std::endl;
std::string pointStr(256, '\0');
fn_point2String(&box.min, const_cast<char *>(pointStr.data()), pointStr.size());
std::cout << "point2String: " << pointStr << std::endl;
// 回调函数
auto fn_set_double_callback = lib.get<void(double_callback_t)>("register_double_callback");
auto fn_set_point_callback = lib.get<void(point_callback_t)>("register_point_callback");
auto fn_set_box_callback = lib.get<void(box_callback_t)>("register_box_callback");
auto fn_trigger_callbacks = lib.get<void(int)>("trigger_callbacks");
fn_set_double_callback(my_double_callback);
fn_set_point_callback(my_point_callback);
fn_set_box_callback(my_box_callback);
fn_trigger_callbacks(1);
fn_trigger_callbacks(2);
fn_trigger_callbacks(4);
std::cout << "---------testCallback----------" << std::endl;
}