| 1 | // Copyright 2016 Klemens D. Morgenstern. |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt |
| 5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include "../b2_workarounds.hpp" // contains dll_test::replace_with_full_path |
| 8 | |
| 9 | //[smart_lib_setup |
| 10 | #include <boost/dll/smart_library.hpp> // for import_alias |
| 11 | #include <iostream> |
| 12 | #include <memory> |
| 13 | |
| 14 | namespace dll = boost::dll; |
| 15 | |
| 16 | struct alias; |
| 17 | |
| 18 | int main(int argc, char* argv[]) { |
| 19 | /*<-*/ b2_workarounds::argv_to_path_guard guard(argc, argv); /*->*/ |
| 20 | boost::dll::fs::path lib_path(argv[1]); // argv[1] contains path to directory with our plugin library |
| 21 | dll::smart_lib lib(lib_path); // smart library instance |
| 22 | //] |
| 23 | //[smart_lib_size |
| 24 | auto size_f = lib.get_function<std::size_t()>("space::my_plugin::size" ); //get the size function |
| 25 | |
| 26 | auto size = size_f(); // get the size of the class |
| 27 | std::unique_ptr<char[], size> buffer(new char[size]); //allocate a buffer for the import |
| 28 | alias & inst = *reinterpret_cast<alias*>(buffer.get()); //cast it to our alias type. |
| 29 | //] |
| 30 | //[smart_lib_type_alias |
| 31 | lib.add_type_alias("space::my_plugin" ); //add an alias, so i can import a class that is not declared here |
| 32 | //] |
| 33 | //[smart_lib_ctor |
| 34 | auto ctor = lib.get_constructor<alias(const std::string&)>(); //get the constructor |
| 35 | ctor.call_standard(&inst, "MyName" ); //call the non-allocating constructor. The allocating-constructor is a non-portable feature |
| 36 | //] |
| 37 | //[smart_lib_name |
| 38 | auto name_f = lib.get_mem_fn<const alias, std::string()>("name" );//import the name function |
| 39 | std::cout << "Name Call: " << (inst.*name_f)() << std::endl; |
| 40 | //] |
| 41 | //[smart_lib_calculate |
| 42 | //import both calculate functions |
| 43 | auto calc_f = lib.get_mem_fn<alias, float(float, float)>("calculate" ); |
| 44 | auto calc_i = lib.get_mem_fn<alias, int(int, int)> ("calculate" ); |
| 45 | |
| 46 | std::cout << "calc(float): " << (inst.*calc_f)(5., 2.) << std::endl; |
| 47 | std::cout << "calc(int) : " << (inst.*calc_f)(5, 2) << std::endl; |
| 48 | //] |
| 49 | //[smart_lib_var |
| 50 | auto & var = lib.get_variable<int>("space::my_plugin::value" ); |
| 51 | cout << "value " << var << endl; |
| 52 | //] |
| 53 | //[smart_lib_dtor |
| 54 | auto dtor = lib.get_destructor<alias>(); //get the destructor |
| 55 | dtor.call_standard(&inst); |
| 56 | std::cout << "plugin->calculate(1.5, 1.5) call: " << plugin->calculate(1.5, 1.5) << std::endl; |
| 57 | } |
| 58 | //] |
| 59 | |