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,2018 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>
18 #include <boost/type_traits/is_same.hpp>
26 class nullary_function;
28 class nullary_function<void()>
32 virtual void call()=0;
37 csbl::shared_ptr<impl_base> impl;
39 struct impl_type: impl_base
42 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
47 impl_type(BOOST_THREAD_RV_REF(F) f_)
56 struct impl_type_ptr: impl_base
59 impl_type_ptr(void (*f_)())
68 BOOST_THREAD_COPYABLE_AND_MOVABLE(nullary_function)
70 explicit nullary_function(void (*f)()):
71 impl(new impl_type_ptr(f))
74 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
76 explicit nullary_function(F& f
77 , typename disable_if<is_same<typename decay<F>::type, nullary_function>, int* >::type=0
79 impl(new impl_type<F>(f))
83 nullary_function(BOOST_THREAD_RV_REF(F) f
84 , typename disable_if<is_same<typename decay<F>::type, nullary_function>, int* >::type=0
86 impl(new impl_type<typename decay<F>::type>(thread_detail::decay_copy(boost::forward<F>(f))))
93 nullary_function(nullary_function const& other) BOOST_NOEXCEPT :
97 nullary_function(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT :
98 #if defined BOOST_NO_CXX11_SMART_PTR
99 impl(BOOST_THREAD_RV(other).impl)
101 BOOST_THREAD_RV(other).impl.reset();
104 impl(boost::move(other.impl))
112 nullary_function& operator=(BOOST_THREAD_COPY_ASSIGN_REF(nullary_function) other) BOOST_NOEXCEPT
117 nullary_function& operator=(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT
119 #if defined BOOST_NO_CXX11_SMART_PTR
120 impl=BOOST_THREAD_RV(other).impl;
121 BOOST_THREAD_RV(other).impl.reset();
123 impl = boost::move(other.impl);
130 { if (impl) impl->call();}
134 template <typename R>
135 class nullary_function<R()>
144 csbl::shared_ptr<impl_base> impl;
145 template <typename F>
146 struct impl_type: impl_base
149 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
154 impl_type(BOOST_THREAD_RV_REF(F) f_)
163 struct impl_type_ptr: impl_base
166 impl_type_ptr(R (*f_)())
176 BOOST_THREAD_COPYABLE_AND_MOVABLE(nullary_function)
178 nullary_function(R (*f)()):
179 impl(new impl_type_ptr(f))
181 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
183 nullary_function(F& f):
184 impl(new impl_type<F>(f))
188 nullary_function(BOOST_THREAD_RV_REF(F) f):
189 impl(new impl_type<typename decay<F>::type>(thread_detail::decay_copy(boost::forward<F>(f))))
192 nullary_function(nullary_function const& other) BOOST_NOEXCEPT :
196 nullary_function(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT :
197 #if defined BOOST_NO_CXX11_SMART_PTR
198 impl(BOOST_THREAD_RV(other).impl)
200 BOOST_THREAD_RV(other).impl.reset();
203 impl(boost::move(other.impl))
215 nullary_function& operator=(BOOST_THREAD_COPY_ASSIGN_REF(nullary_function) other) BOOST_NOEXCEPT
220 nullary_function& operator=(BOOST_THREAD_RV_REF(nullary_function) other) BOOST_NOEXCEPT
222 #if defined BOOST_NO_CXX11_SMART_PTR
223 impl=BOOST_THREAD_RV(other).impl;
224 BOOST_THREAD_RV(other).impl.reset();
226 impl = boost::move(other.impl);
232 { if (impl) return impl->call(); else return R();}
236 BOOST_THREAD_DCL_MOVABLE_BEG(F) detail::nullary_function<F> BOOST_THREAD_DCL_MOVABLE_END