forked from apolukhin/Boost.DLL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetting_started.cpp
83 lines (65 loc) · 2.58 KB
/
getting_started.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
// Copyright 2014 Renato Tegon Forti, Antony Polukhin.
// Copyright 2015-2016 Antony Polukhin.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/core/lightweight_test.hpp>
#include <boost/dll.hpp>
#include <boost/function.hpp>
#include <string>
#include "b2_workarounds.hpp"
// Unit Tests
int main(int argc, char* argv[]) {
using namespace boost;
filesystem::path path_to_shared_library = b2_workarounds::first_lib_from_argv(argc, argv);
BOOST_TEST(path_to_shared_library.string().find("getting_started_library") != std::string::npos);
//[getting_started_imports_c_function
// Importing pure C function
function<int(int)> c_func = dll::import<int(int)>(
path_to_shared_library, "c_func_name"
);
//]
int c_func_res = c_func(1); // calling the function
BOOST_TEST(c_func_res == 2);
//[getting_started_imports_c_variable
// Importing pure C variable
shared_ptr<int> c_var = dll::import<int>(
path_to_shared_library, "c_variable_name"
);
//]
int c_var_old_contents = *c_var; // using the variable
*c_var = 100;
BOOST_TEST(c_var_old_contents == 1);
//[getting_started_imports_alias
// Importing function by alias name
/*<-*/ function<std::string(const std::string&)> /*->*/ /*=auto*/ cpp_func = dll::import_alias<std::string(const std::string&)>(
path_to_shared_library, "pretty_name"
);
//]
// calling the function
std::string cpp_func_res = cpp_func(std::string("In importer."));
BOOST_TEST(cpp_func_res == "In importer. Hello from lib!");
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
//[getting_started_imports_cpp11_function
// Importing function.
auto cpp11_func = dll::import<int(std::string&&)>(
path_to_shared_library, "i_am_a_cpp11_function"
);
//]
// calling the function
int cpp11_func_res = cpp11_func(std::string("In importer."));
BOOST_TEST(cpp11_func_res == sizeof("In importer.") - 1);
#endif
//[getting_started_imports_cpp_variable
// Importing variable.
shared_ptr<std::string> cpp_var = dll::import<std::string>(
path_to_shared_library, "cpp_variable_name"
);
//]
std::string cpp_var_old_contents = *cpp_var; // using the variable
*cpp_var = "New value";
BOOST_TEST(cpp_var_old_contents == "some value");
BOOST_TEST(*cpp_var == "New value");
return boost::report_errors();
}