1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin. 2 // Copyright 2015-2019 Antony Polukhin. 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 // MinGW related workaround 9 #define BOOST_DLL_FORCE_ALIAS_INSTANTIATION 10 11 //[plugcpp_on_unload 12 #include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS 13 #include <boost/function.hpp> 14 #include <vector> 15 16 namespace my_namespace { 17 18 struct on_unload { 19 typedef boost::function<void()> callback_t; 20 typedef on_unload this_type; 21 ~on_unloadmy_namespace::on_unload22 ~on_unload() { 23 for (std::size_t i = 0; i < callbacks_.size(); ++i) { 24 callback_t& function = callbacks_[i]; 25 function(); // calling the callback 26 } 27 } 28 29 // not thread safe addmy_namespace::on_unload30 static void add(const callback_t& function) { 31 static this_type instance; 32 instance.callbacks_.push_back(function); 33 } 34 35 private: 36 std::vector<callback_t> callbacks_; on_unloadmy_namespace::on_unload37 on_unload() {} // prohibit construction outside of the `add` function 38 }; 39 40 // Exporting the static "add" function with name "on_unload" 41 BOOST_DLL_ALIAS(my_namespace::on_unload::add, on_unload) 42 43 } // namespace my_namespace 44 45 //] 46 47 48 49