1 // Copyright (C) 2013 Vicente J. Botet Escriba 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 // 6 // 2013/09 Vicente J. Botet Escriba 7 // Adapt to boost from CCIA C++11 implementation 8 // Make use of Boost.Move 9 10 #ifndef BOOST_THREAD_DETAIL_FUNCTION_WRAPPER_HPP 11 #define BOOST_THREAD_DETAIL_FUNCTION_WRAPPER_HPP 12 13 #include <boost/config.hpp> 14 #include <boost/thread/detail/memory.hpp> 15 #include <boost/thread/detail/move.hpp> 16 17 #include <boost/thread/csbl/memory/unique_ptr.hpp> 18 19 #include <memory> 20 #include <functional> 21 22 namespace boost 23 { 24 namespace detail 25 { 26 class function_wrapper 27 { 28 struct impl_base 29 { 30 virtual void call()=0; ~impl_baseboost::detail::function_wrapper::impl_base31 virtual ~impl_base() 32 { 33 } 34 }; 35 typedef boost::csbl::unique_ptr<impl_base> impl_base_type; 36 impl_base_type impl; 37 template <typename F> 38 struct impl_type: impl_base 39 { 40 F f; impl_typeboost::detail::function_wrapper::impl_type41 impl_type(F const &f_) 42 : f(f_) 43 {} impl_typeboost::detail::function_wrapper::impl_type44 impl_type(BOOST_THREAD_RV_REF(F) f_) 45 : f(boost::move(f_)) 46 {} 47 callboost::detail::function_wrapper::impl_type48 void call() 49 { 50 if (impl) f(); 51 } 52 }; 53 public: 54 BOOST_THREAD_MOVABLE_ONLY(function_wrapper) 55 56 //#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES 57 template<typename F> function_wrapper(F const & f)58 function_wrapper(F const& f): 59 impl(new impl_type<F>(f)) 60 {} 61 //#endif 62 template<typename F> function_wrapper(BOOST_THREAD_RV_REF (F)f)63 function_wrapper(BOOST_THREAD_RV_REF(F) f): 64 impl(new impl_type<F>(boost::forward<F>(f))) 65 {} function_wrapper(BOOST_THREAD_RV_REF (function_wrapper)other)66 function_wrapper(BOOST_THREAD_RV_REF(function_wrapper) other) BOOST_NOEXCEPT : 67 impl(other.impl) 68 { 69 other.impl = 0; 70 } function_wrapper()71 function_wrapper() 72 : impl(0) 73 { 74 } ~function_wrapper()75 ~function_wrapper() 76 { 77 } 78 operator =(BOOST_THREAD_RV_REF (function_wrapper)other)79 function_wrapper& operator=(BOOST_THREAD_RV_REF(function_wrapper) other) BOOST_NOEXCEPT 80 { 81 impl=other.impl; 82 other.impl=0; 83 return *this; 84 } 85 operator ()()86 void operator()() 87 { impl->call();} 88 89 }; 90 } 91 } 92 93 #endif // header 94