| 1 | // Copyright 2016 Klemens Morgenstern |
| 2 | // Copyright Antony Polukhin, 2019-2024 |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt |
| 6 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | // For more information, see http://www.boost.org |
| 9 | |
| 10 | #include <boost/config.hpp> |
| 11 | #include <boost/predef.h> |
| 12 | |
| 13 | #if (__cplusplus >= 201103L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L) |
| 14 | // Make sure that it at least compiles |
| 15 | # include <boost/dll/smart_library.hpp> |
| 16 | #endif |
| 17 | |
| 18 | #if (__cplusplus >= 201402L) || (BOOST_COMP_MSVC >= BOOST_VERSION_NUMBER(14,0,0)) |
| 19 | |
| 20 | #include "../example/b2_workarounds.hpp" |
| 21 | |
| 22 | #include <boost/core/lightweight_test.hpp> |
| 23 | #include <boost/filesystem.hpp> |
| 24 | #include <boost/variant.hpp> |
| 25 | |
| 26 | #include <iostream> |
| 27 | |
| 28 | struct override_class |
| 29 | { |
| 30 | int arr[32]; |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | int main(int argc, char* argv[]) |
| 35 | { |
| 36 | using namespace boost::dll; |
| 37 | using namespace boost::dll::experimental; |
| 38 | boost::dll::fs::path pt = b2_workarounds::first_lib_from_argv(argc, argv); |
| 39 | |
| 40 | BOOST_TEST(!pt.empty()); |
| 41 | std::cout << "Library: " << pt << std::endl; |
| 42 | std::cerr << 1 << ' '; |
| 43 | smart_library sm(pt); |
| 44 | |
| 45 | auto& unscoped_var = sm.get_variable<int>(name: "unscoped_var" ); |
| 46 | BOOST_TEST(unscoped_var == 42); |
| 47 | BOOST_TEST(unscoped_var == get<int>(sm, "unscoped_var" )); |
| 48 | BOOST_TEST(&unscoped_var == &get<int>(sm, "unscoped_var" )); |
| 49 | |
| 50 | std::cerr << 2 << ' '; |
| 51 | auto& unscoped_c_var = sm.get_variable<const double>(name: "unscoped_c_var" ); |
| 52 | BOOST_TEST(unscoped_c_var == 1.234); |
| 53 | std::cerr << 3 << ' '; |
| 54 | auto& sp_variable = sm.get_variable<double>(name: "some_space::variable" ); |
| 55 | BOOST_TEST(sp_variable == 0.2); |
| 56 | |
| 57 | std::cerr << 4 << ' '; |
| 58 | auto scoped_fun = sm.get_function<const int&()>(name: "some_space::scoped_fun" ); |
| 59 | BOOST_TEST(scoped_fun != nullptr); |
| 60 | { std::cerr << 5 << ' '; |
| 61 | auto &res = scoped_fun(); |
| 62 | const int expected = 0xDEADBEEF; |
| 63 | BOOST_TEST(res == expected); |
| 64 | } |
| 65 | std::cerr << 6 << ' '; |
| 66 | auto ovl1 = sm.get_function<void(int)> (name: "overloaded" ); |
| 67 | auto ovl2 = sm.get_function<void(double)>(name: "overloaded" ); |
| 68 | std::cerr << 7 << ' '; |
| 69 | BOOST_TEST(ovl1 != nullptr); |
| 70 | BOOST_TEST(ovl2 != nullptr); |
| 71 | BOOST_TEST(reinterpret_cast<void*>(ovl1) != reinterpret_cast<void*>(ovl2)); |
| 72 | BOOST_TEST(ovl1 == get<void(int)>(sm, "overloaded" )); |
| 73 | std::cerr << 8 << ' '; |
| 74 | ovl1(12); |
| 75 | BOOST_TEST(unscoped_var == 12); |
| 76 | ovl2(5.0); |
| 77 | BOOST_TEST(sp_variable == 5.0); |
| 78 | std::cerr << 9 << ' '; |
| 79 | |
| 80 | |
| 81 | // TODO: ms.get_name on Clang has space after comma `boost::variant<double, int>` |
| 82 | #if !(defined(BOOST_TRAVISCI_BUILD) && defined(_MSC_VER) && defined(BOOST_CLANG)) |
| 83 | auto var1 = sm.get_function<void(boost::variant<int, double> &)>(name: "use_variant" ); |
| 84 | auto var2 = sm.get_function<void(boost::variant<double, int> &)>(name: "use_variant" ); |
| 85 | std::cerr << 10 << ' '; |
| 86 | BOOST_TEST(var1 != nullptr); |
| 87 | BOOST_TEST(var2 != nullptr); |
| 88 | BOOST_TEST(reinterpret_cast<void*>(var1) != reinterpret_cast<void*>(var2)); |
| 89 | |
| 90 | { |
| 91 | boost::variant<int, double> v1 = 232.22; |
| 92 | boost::variant<double, int> v2 = -1; |
| 93 | std::cerr << 11 << ' '; |
| 94 | var1(v1); |
| 95 | var2(v2); |
| 96 | |
| 97 | struct : boost::static_visitor<void> |
| 98 | { |
| 99 | void operator()(double) {BOOST_TEST(false);} |
| 100 | void operator()(int i) {BOOST_TEST(i == 42);} |
| 101 | } vis1; |
| 102 | |
| 103 | struct : boost::static_visitor<void> |
| 104 | { |
| 105 | void operator()(double d) {BOOST_TEST(d == 3.124);} |
| 106 | void operator()(int ) {BOOST_TEST(false);} |
| 107 | } vis2; |
| 108 | |
| 109 | boost::apply_visitor(visitor&: vis1, visitable&: v1); |
| 110 | boost::apply_visitor(visitor&: vis2, visitable&: v2); |
| 111 | |
| 112 | } |
| 113 | #endif |
| 114 | std::cerr << 12 << ' '; |
| 115 | /* now test the class stuff */ |
| 116 | |
| 117 | //first we import and test the global variables |
| 118 | |
| 119 | auto& father_val = sm.get_variable<int>(name: "some_space::father_value" ); |
| 120 | auto& static_val = sm.get_variable<int>(name: "some_space::some_class::value" ); |
| 121 | BOOST_TEST(father_val == 12); |
| 122 | BOOST_TEST(static_val == -1); |
| 123 | std::cerr << 13 << ' '; |
| 124 | //now get the static function. |
| 125 | auto set_value = sm.get_function<void(const int &)>(name: "some_space::some_class::set_value" ); |
| 126 | BOOST_TEST(set_value != nullptr); |
| 127 | std::cerr << 14 << ' '; |
| 128 | set_value(42); |
| 129 | BOOST_TEST(static_val == 42); //alright, static method works. |
| 130 | |
| 131 | |
| 132 | //alright, now import the class members |
| 133 | //first add the type alias. |
| 134 | sm.add_type_alias<override_class>(name: "some_space::some_class" ); |
| 135 | std::cerr << 15 << ' '; |
| 136 | auto set = sm.get_mem_fn<override_class, void(int)>(name: "set" ); |
| 137 | |
| 138 | std::cerr << 16 << ' '; |
| 139 | try { |
| 140 | sm.get_mem_fn<override_class, int()>(name: "get" ); |
| 141 | BOOST_TEST(false); |
| 142 | } catch(boost::dll::fs::system_error &) {} |
| 143 | auto get = sm.get_mem_fn<const override_class, int()>(name: "get" ); |
| 144 | std::cerr << 17 << ' '; |
| 145 | BOOST_TEST(get != nullptr); |
| 146 | BOOST_TEST(set != nullptr); |
| 147 | std::cerr << 18 << ' '; |
| 148 | auto func_dd = sm.get_mem_fn<override_class, double(double, double)>(name: "func" ); |
| 149 | auto func_ii = sm.get_mem_fn<override_class, int(int, int)> (name: "func" ); |
| 150 | auto func_iiv = sm.get_mem_fn<volatile override_class, int(int, int)> (name: "func" ); |
| 151 | auto func_ddc = sm.get_mem_fn<const volatile override_class, double(double, double)>(name: "func" ); |
| 152 | |
| 153 | std::cerr << 19 << ' '; |
| 154 | BOOST_TEST(func_dd != nullptr); |
| 155 | BOOST_TEST(func_ii != nullptr); |
| 156 | |
| 157 | std::cerr << 20 << ' '; |
| 158 | auto ctor_v = sm.get_constructor<override_class()>(); |
| 159 | auto ctor_i = sm.get_constructor<override_class(int)>(); |
| 160 | |
| 161 | auto dtor = sm.get_destructor<override_class>(); |
| 162 | std::cerr << 21 << ' '; |
| 163 | //actually never used. |
| 164 | if (ctor_v.has_allocating()) |
| 165 | { |
| 166 | //allocate |
| 167 | auto p = ctor_v.call_allocating(); |
| 168 | |
| 169 | //assert it works |
| 170 | auto val = (p->*get)(); |
| 171 | BOOST_TEST(val == 123); |
| 172 | //deallocate |
| 173 | dtor.call_deleting(ptr: p); |
| 174 | //now i cannot assert that it deletes, since it would crash. |
| 175 | } |
| 176 | //More tests to assure the correct this-ptr |
| 177 | std::cerr << 22 << ' '; |
| 178 | typedef override_class * override_class_p; |
| 179 | override_class_p &this_dll = sm.shared_lib().get<override_class_p>(symbol_name: "this_" ); |
| 180 | |
| 181 | std::cerr << 23 << ' '; |
| 182 | //ok, now load the ctor/dtor |
| 183 | override_class oc; |
| 184 | |
| 185 | override_class_p this_exe = &oc; |
| 186 | |
| 187 | for (auto& i : oc.arr) { |
| 188 | i = 0; |
| 189 | } |
| 190 | std::cerr << 24 << ' '; |
| 191 | |
| 192 | BOOST_TEST((oc.*get)() == 0); BOOST_TEST(this_dll == this_exe); |
| 193 | |
| 194 | ctor_i.call_standard(ptr: &oc, args: 12); BOOST_TEST(this_dll == this_exe); |
| 195 | |
| 196 | BOOST_TEST(static_val == 12); |
| 197 | BOOST_TEST((oc.*get)() == 456); BOOST_TEST(this_dll == this_exe); |
| 198 | (oc.*set)(42); |
| 199 | BOOST_TEST((oc.*get)() == 42); BOOST_TEST(this_dll == this_exe); |
| 200 | std::cerr << 25 << ' '; |
| 201 | |
| 202 | BOOST_TEST((oc.*func_dd)(3,2) == 6); BOOST_TEST(this_dll == this_exe); |
| 203 | BOOST_TEST((oc.*func_ii)(1,2) == 3); BOOST_TEST(this_dll == this_exe); |
| 204 | BOOST_TEST((oc.*func_ddc)(10,2) == 5); BOOST_TEST(this_dll == this_exe); |
| 205 | BOOST_TEST((oc.*func_iiv)(9,2) == 7); BOOST_TEST(this_dll == this_exe); |
| 206 | std::cerr << 26 << ' '; |
| 207 | dtor.call_standard(ptr: &oc); BOOST_TEST(this_dll == this_exe); |
| 208 | BOOST_TEST(static_val == 0); |
| 209 | |
| 210 | #ifndef BOOST_NO_RTTI |
| 211 | const auto& ti = sm.get_type_info<override_class>(); |
| 212 | BOOST_TEST(ti.name() != nullptr); |
| 213 | #endif |
| 214 | std::cerr << 27 << ' '; |
| 215 | //test the ovls helper. |
| 216 | { |
| 217 | namespace ex = boost::dll::experimental; |
| 218 | auto &var = ex::get<double>(sm, name: "some_space::variable" ); |
| 219 | BOOST_TEST(&var == &sp_variable); |
| 220 | |
| 221 | auto fun = ex::get<void(int)>(sm, name: "overloaded" ); |
| 222 | BOOST_TEST(fun == ovl1); |
| 223 | |
| 224 | auto func_ii = sm.get_mem_fn<override_class, int(int, int)> (name: "func" ); |
| 225 | |
| 226 | auto mem_fn = ex::get<override_class, int(int, int)>(sm, name: "func" ); |
| 227 | |
| 228 | BOOST_TEST(mem_fn == func_ii); |
| 229 | } |
| 230 | |
| 231 | std::cerr << 28 << ' '; |
| 232 | return boost::report_errors(); |
| 233 | } |
| 234 | |
| 235 | #else |
| 236 | int main() {return 0;} |
| 237 | #endif |
| 238 | |