diff options
Diffstat (limited to 'src/boost/libs/dll/example/mangled')
-rw-r--r-- | src/boost/libs/dll/example/mangled/import_class.cpp | 47 | ||||
-rw-r--r-- | src/boost/libs/dll/example/mangled/my_cpp_plugin.hpp | 43 | ||||
-rw-r--r-- | src/boost/libs/dll/example/mangled/smart_lib.cpp | 58 |
3 files changed, 148 insertions, 0 deletions
diff --git a/src/boost/libs/dll/example/mangled/import_class.cpp b/src/boost/libs/dll/example/mangled/import_class.cpp new file mode 100644 index 00000000..eee4a961 --- /dev/null +++ b/src/boost/libs/dll/example/mangled/import_class.cpp @@ -0,0 +1,47 @@ +// Copyright 2016 Klemens Morgenstern +// +// 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) + +// For more information, see http://www.boost.org + +#include "../b2_workarounds.hpp" // contains dll_test::replace_with_full_path + +//[import_class_setup + +#include <boost/dll/smart_library.hpp> +#include <boost/dll/import_mangled.hpp> +#include <boost/dll/import_class.hpp> + + + +int main(int argc, char* argv[]) { + /*<-*/ b2_workarounds::argv_to_path_guard guard(argc, argv); /*->*/ + boost::dll::fs::path lib_path(argv[1]); // argv[1] contains path to directory with our plugin library + smart_library lib(lib_path);// smart library instance +//] +//[import_class_size + auto size_f = import_mangled<std::size_t()>("space::my_plugin::size"); //get the size function + + auto size = (*size_f)(); // get the size of the class +//] +//[import_class_value + auto value = import_mangled<int>(lib, "space::my_plugin::value"); +//] +//[import_class_ctor + auto cl = import_class<class alias, const std::string&>(lib, "space::my_plugin::some_class", size, "MyName"); +//] +//[import_class_name + auto name = import_mangled<const alias, std::string()>(lib, "name"); + std::cout << "Name: " << (cl->*name)() << std::endl; +//] +//[import_class_calc + auto calc = import_mangled<alias, float(float, float), int(int, int)>(lib, "calculate"); + std::cout << "Calc(float): " (cl->*calc)(5.f, 2.f) << std::endl; + std::cout << "Calc(int): " (cl->*calc)(5, 2) << std::endl; +//] +//[import_class_typeinfo + std::type_info &ti = cl.get_type_info(); +//] +} diff --git a/src/boost/libs/dll/example/mangled/my_cpp_plugin.hpp b/src/boost/libs/dll/example/mangled/my_cpp_plugin.hpp new file mode 100644 index 00000000..3f9e9ee1 --- /dev/null +++ b/src/boost/libs/dll/example/mangled/my_cpp_plugin.hpp @@ -0,0 +1,43 @@ +// Copyright 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) + +#ifndef BOOST_DLL_MY_CPP_PLUGIN_API_HPP +#define BOOST_DLL_MY_CPP_PLUGIN_API_HPP + +//[cppplug +#include <string> + +namespace space { + +class BOOST_SYMBOL_EXPORT my_plugin +{ + std::string _name; +public: + std::string name() const; + float calculate(float x, float y); + int calculate(int, x, int y); + static std::size_t size(); + my_plugin(const std::string & name); + my_plugin(); + ~my_plugin_api(); + static int value; +}; + +} +//] + +std::string space::my_plugin_api::name() const {return _name;} +float space::my_plugin::calculate(float x, float y) {return x/y;} +int space::my_plugin::calculate(int, x, int y) {return x/y;} +std::size_t my_plugin::size() {return sizeof(my_plugin);} +space::my_plugin::my_plugin(const std::string & name) : _name(name) {} +space::my_plugin::my_plugin() : _name("Empty") {} +space::my_plugin::~my_plugin_api() {} +int space::my_plugin::value = 42; + + +#endif // BOOST_DLL_MY_PLUGIN_API_HPP + diff --git a/src/boost/libs/dll/example/mangled/smart_lib.cpp b/src/boost/libs/dll/example/mangled/smart_lib.cpp new file mode 100644 index 00000000..d8cb69c6 --- /dev/null +++ b/src/boost/libs/dll/example/mangled/smart_lib.cpp @@ -0,0 +1,58 @@ +// Copyright 2016 Klemens D. Morgenstern. +// +// 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 "../b2_workarounds.hpp" // contains dll_test::replace_with_full_path + +//[smart_lib_setup +#include <boost/dll/smart_library.hpp> // for import_alias +#include <iostream> +#include <memory> + +namespace dll = boost::dll; + +struct alias; + +int main(int argc, char* argv[]) { + /*<-*/ b2_workarounds::argv_to_path_guard guard(argc, argv); /*->*/ + boost::dll::fs::path lib_path(argv[1]); // argv[1] contains path to directory with our plugin library + dll::smart_lib lib(lib_path); // smart library instance +//] +//[smart_lib_size + auto size_f = lib.get_function<std::size_t()>("space::my_plugin::size"); //get the size function + + auto size = size_f(); // get the size of the class + std::unique_ptr<char[], size> buffer(new char[size]); //allocate a buffer for the import + alias & inst = *reinterpret_cast<alias*>(buffer.get()); //cast it to our alias type. +//] +//[smart_lib_type_alias + lib.add_type_alias("space::my_plugin"); //add an alias, so i can import a class that is not declared here +//] +//[smart_lib_ctor + auto ctor = lib.get_constructor<alias(const std::string&)>(); //get the constructor + ctor.call_standard(&inst, "MyName"); //call the non-allocating constructor. The allocating-constructor is a non-portable feature +//] +//[smart_lib_name + auto name_f = lib.get_mem_fn<const alias, std::string()>("name");//import the name function + std::cout << "Name Call: " << (inst.*name_f)() << std::endl; +//] +//[smart_lib_calculate + //import both calculate functions + auto calc_f = lib.get_mem_fn<alias, float(float, float)>("calculate"); + auto calc_i = lib.get_mem_fn<alias, int(int, int)> ("calculate"); + + std::cout << "calc(float): " << (inst.*calc_f)(5., 2.) << std::endl; + std::cout << "calc(int) : " << (inst.*calc_f)(5, 2) << std::endl; +//] +//[smart_lib_var + auto & var = lib.get_variable<int>("space::my_plugin::value"); + cout << "value " << var << endl; +//] +//[smart_lib_dtor + auto dtor = lib.get_destructor<alias>(); //get the destructor + dtor.call_standard(&inst); + std::cout << "plugin->calculate(1.5, 1.5) call: " << plugin->calculate(1.5, 1.5) << std::endl; +} +//] |