From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- .../libs/dll/example/tutorial8/refcounting_api.hpp | 77 ++++++++++++++++++++++ .../dll/example/tutorial8/refcounting_plugin.cpp | 47 +++++++++++++ .../dll/example/tutorial8/refcounting_plugin.hpp | 17 +++++ src/boost/libs/dll/example/tutorial8/tutorial8.cpp | 25 +++++++ .../dll/example/tutorial8/tutorial8_static.cpp | 23 +++++++ 5 files changed, 189 insertions(+) create mode 100644 src/boost/libs/dll/example/tutorial8/refcounting_api.hpp create mode 100644 src/boost/libs/dll/example/tutorial8/refcounting_plugin.cpp create mode 100644 src/boost/libs/dll/example/tutorial8/refcounting_plugin.hpp create mode 100644 src/boost/libs/dll/example/tutorial8/tutorial8.cpp create mode 100644 src/boost/libs/dll/example/tutorial8/tutorial8_static.cpp (limited to 'src/boost/libs/dll/example/tutorial8') diff --git a/src/boost/libs/dll/example/tutorial8/refcounting_api.hpp b/src/boost/libs/dll/example/tutorial8/refcounting_api.hpp new file mode 100644 index 00000000..c9178110 --- /dev/null +++ b/src/boost/libs/dll/example/tutorial8/refcounting_api.hpp @@ -0,0 +1,77 @@ +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015-2019 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) + +//[plugcpp_my_plugin_refcounting_api +#include "../tutorial_common/my_plugin_api.hpp" +#include + +class my_refcounting_api: public my_plugin_api { +public: + // Returns path to shared object that holds a plugin. + // Must be instantiated in plugin. + virtual boost::dll::fs::path location() const = 0; +}; +//] + + +//[plugcpp_library_holding_deleter_api_bind +#include +#include +#include + +struct library_holding_deleter { + boost::shared_ptr lib_; + + void operator()(my_refcounting_api* p) const { + delete p; + } +}; + +inline boost::shared_ptr bind(my_refcounting_api* plugin) { + // getting location of the shared library that holds the plugin + boost::dll::fs::path location = plugin->location(); + + // `make_shared` is an efficient way to create a shared pointer + boost::shared_ptr lib + = boost::make_shared(location); + + library_holding_deleter deleter; + deleter.lib_ = lib; + + return boost::shared_ptr( + plugin, deleter + ); +} +//] + +//[plugcpp_get_plugin_refcounting +#include +#include +inline boost::shared_ptr get_plugin( + boost::dll::fs::path path, const char* func_name) +{ + typedef my_refcounting_api*(func_t)(); + boost::function creator = boost::dll::import_alias( + path, + func_name, + boost::dll::load_mode::append_decorations // will be ignored for executable + ); + + // `plugin` does not hold a reference to shared library. If `creator` will go out of scope, + // then `plugin` can not be used. + my_refcounting_api* plugin = creator(); + + // Returned variable holds a reference to + // shared_library and it is safe to use it. + return bind( plugin ); + + // `creator` goes out of scope here and will be destroyed. +} + +//] + + diff --git a/src/boost/libs/dll/example/tutorial8/refcounting_plugin.cpp b/src/boost/libs/dll/example/tutorial8/refcounting_plugin.cpp new file mode 100644 index 00000000..bb52cdfa --- /dev/null +++ b/src/boost/libs/dll/example/tutorial8/refcounting_plugin.cpp @@ -0,0 +1,47 @@ +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015-2019 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) + +// MinGW related workaround +#define BOOST_DLL_FORCE_ALIAS_INSTANTIATION + +//[plugcpp_my_plugin_refcounting +#include "refcounting_plugin.hpp" +#include // for this_line_location() + +namespace my_namespace { + +class my_plugin_refcounting : public my_refcounting_api { +public: + // Must be instantiated in plugin + boost::dll::fs::path location() const { + return boost::dll::this_line_location(); // location of this plugin + } + + std::string name() const { + return "refcounting"; + } + + // ... + //<- + // This block is invisible for Quickbook documentation + float calculate(float /*x*/, float /*y*/) { + return 0; + } + //-> +}; + +} // namespace my_namespace + +// Factory method. Returns *simple pointer*! +my_refcounting_api* create() { + return new my_namespace::my_plugin_refcounting(); +} + +//] + + + diff --git a/src/boost/libs/dll/example/tutorial8/refcounting_plugin.hpp b/src/boost/libs/dll/example/tutorial8/refcounting_plugin.hpp new file mode 100644 index 00000000..990bd214 --- /dev/null +++ b/src/boost/libs/dll/example/tutorial8/refcounting_plugin.hpp @@ -0,0 +1,17 @@ +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015-2019 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) + +//[plugcpp_my_plugin_refcounting_hpp +#include "refcounting_api.hpp" +#include // for BOOST_DLL_ALIAS + +my_refcounting_api* create(); // defined in plugin +BOOST_DLL_ALIAS(create, create_refc_plugin) +//] + + + diff --git a/src/boost/libs/dll/example/tutorial8/tutorial8.cpp b/src/boost/libs/dll/example/tutorial8/tutorial8.cpp new file mode 100644 index 00000000..ef169a99 --- /dev/null +++ b/src/boost/libs/dll/example/tutorial8/tutorial8.cpp @@ -0,0 +1,25 @@ +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015-2019 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 "../b2_workarounds.hpp" + +//[callplugcpp_tutorial8 +#include +#include "refcounting_api.hpp" + +int main(int argc, char* argv[]) { + /*<-*/ b2_workarounds::argv_to_path_guard guard(argc, argv); /*->*/ + boost::shared_ptr plugin = get_plugin( + boost::dll::fs::path(argv[1]) / "refcounting_plugin", + "create_refc_plugin" + ); + + std::cout << "Plugin name: " << plugin->name() + << ", \nlocation: " << plugin->location() + << std::endl; +} +//] diff --git a/src/boost/libs/dll/example/tutorial8/tutorial8_static.cpp b/src/boost/libs/dll/example/tutorial8/tutorial8_static.cpp new file mode 100644 index 00000000..810d322f --- /dev/null +++ b/src/boost/libs/dll/example/tutorial8/tutorial8_static.cpp @@ -0,0 +1,23 @@ +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015-2019 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) + +//[callplugcpp_tutorial8_static +#include // program_location() +#include +#include "refcounting_plugin.hpp" + +int main() { + boost::shared_ptr plugin = get_plugin( + boost::dll::program_location(), + "create_refc_plugin" + ); + + std::cout << "Plugin name: " << plugin->name() + << ", \nlocation: " << plugin->location() + << std::endl; +} +//] -- cgit v1.2.3