1 // Copyright (C) 2013 Vicente J. Botet Escriba
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)
6 // 2013/09 Vicente J. Botet Escriba
7 // Adapt to boost from CCIA C++11 implementation
8 // Make use of Boost.Move
10 #ifndef BOOST_THREAD_DETAIL_NULLARY_FUNCTION_HPP
11 #define BOOST_THREAD_DETAIL_NULLARY_FUNCTION_HPP
13 #include <boost/config.hpp>
14 #include <boost/thread/detail/memory.hpp>
15 #include <boost/thread/detail/move.hpp>
16 #include <boost/thread/csbl/memory/shared_ptr.hpp>
17 #include <boost/type_traits/decay.hpp>
25 class nullary_function;
27 class nullary_function<void()>
31 virtual void call()=0;
36 csbl::shared_ptr<impl_base> impl;
38 struct impl_type: impl_base
41 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
46 impl_type(BOOST_THREAD_RV_REF(F) f_)
55 struct impl_type_ptr: impl_base
58 impl_type_ptr(void (*f_)())
67 BOOST_THREAD_COPYABLE_AND_MOVABLE(nullary_function)
69 explicit nullary_function(void (*f)()):
70 impl(new impl_type_ptr(f))
73 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
75 explicit nullary_function(F& f):
76 impl(new impl_type<F>(f))
80 nullary_function(BOOST_THREAD_RV_REF(F) f):
81 impl(new impl_type<typename decay<F>::type>(thread_detail::decay_copy(boost::forward<F>(f))))
88 nullary_function(nullary_function const& other) BOOST_NOEXCEPT :
92 nullary_function(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT :
93 #if defined BOOST_NO_CXX11_SMART_PTR
94 impl(BOOST_THREAD_RV(other).impl)
96 BOOST_THREAD_RV(other).impl.reset();
99 impl(boost::move(other.impl))
107 nullary_function& operator=(BOOST_THREAD_COPY_ASSIGN_REF(nullary_function) other) BOOST_NOEXCEPT
112 nullary_function& operator=(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT
114 #if defined BOOST_NO_CXX11_SMART_PTR
115 impl=BOOST_THREAD_RV(other).impl;
116 BOOST_THREAD_RV(other).impl.reset();
118 impl = boost::move(other.impl);
125 { if (impl) impl->call();}
129 template <typename R>
130 class nullary_function<R()>
139 csbl::shared_ptr<impl_base> impl;
140 template <typename F>
141 struct impl_type: impl_base
144 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
149 impl_type(BOOST_THREAD_RV_REF(F) f_)
158 struct impl_type_ptr: impl_base
161 impl_type_ptr(R (*f_)())
171 BOOST_THREAD_COPYABLE_AND_MOVABLE(nullary_function)
173 nullary_function(R (*f)()):
174 impl(new impl_type_ptr(f))
176 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
178 nullary_function(F& f):
179 impl(new impl_type<F>(f))
183 nullary_function(BOOST_THREAD_RV_REF(F) f):
184 impl(new impl_type<typename decay<F>::type>(thread_detail::decay_copy(boost::forward<F>(f))))
187 nullary_function(nullary_function const& other) BOOST_NOEXCEPT :
191 nullary_function(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT :
192 #if defined BOOST_NO_CXX11_SMART_PTR
193 impl(BOOST_THREAD_RV(other).impl)
195 BOOST_THREAD_RV(other).impl.reset();
198 impl(boost::move(other.impl))
210 nullary_function& operator=(BOOST_THREAD_COPY_ASSIGN_REF(nullary_function) other) BOOST_NOEXCEPT
215 nullary_function& operator=(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT
217 #if defined BOOST_NO_CXX11_SMART_PTR
218 impl=BOOST_THREAD_RV(other).impl;
219 BOOST_THREAD_RV(other).impl.reset();
221 impl = boost::move(other.impl);
227 { if (impl) return impl->call(); else return R();}
231 BOOST_THREAD_DCL_MOVABLE_BEG(F) detail::nullary_function<F> BOOST_THREAD_DCL_MOVABLE_END