2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_FIBERS_DETAIL_TASK_OBJECT_H
8 #define BOOST_FIBERS_DETAIL_TASK_OBJECT_H
15 #include <boost/config.hpp>
16 #include <boost/context/detail/config.hpp>
17 #if defined(BOOST_NO_CXX17_STD_APPLY)
18 #include <boost/context/detail/apply.hpp>
20 #include <boost/core/pointer_traits.hpp>
22 #include <boost/fiber/detail/config.hpp>
23 #include <boost/fiber/future/detail/task_base.hpp>
25 #ifdef BOOST_HAS_ABI_HEADERS
26 # include BOOST_ABI_PREFIX
33 template< typename Fn, typename Allocator, typename R, typename ... Args >
34 class task_object : public task_base< R, Args ... > {
36 typedef task_base< R, Args ... > base_type;
37 typedef std::allocator_traits< Allocator > allocator_traits;
40 typedef typename allocator_traits::template rebind_alloc<
44 task_object( allocator_type const& alloc, Fn const& fn) :
50 task_object( allocator_type const& alloc, Fn && fn) :
52 fn_{ std::move( fn) },
56 void run( Args && ... args) override final {
59 #if defined(BOOST_NO_CXX17_STD_APPLY)
60 boost::context::detail::apply(
61 fn_, std::make_tuple( std::forward< Args >( args) ... ) )
64 fn_, std::make_tuple( std::forward< Args >( args) ... ) )
67 #if defined(BOOST_CONTEXT_HAS_CXXABI_H)
68 } catch ( abi::__forced_unwind const&) {
72 this->set_exception( std::current_exception() );
76 typename base_type::ptr_type reset() override final {
77 typedef std::allocator_traits< allocator_type > traity_type;
78 typedef pointer_traits< typename traity_type::pointer> ptrait_type;
80 typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) };
81 typename ptrait_type::element_type* p = boost::to_address(ptr);
83 traity_type::construct( alloc_, p, alloc_, std::move( fn_) );
85 traity_type::deallocate( alloc_, ptr, 1);
92 void deallocate_future() noexcept override final {
93 destroy_( alloc_, this);
98 allocator_type alloc_;
100 static void destroy_( allocator_type const& alloc, task_object * p) noexcept {
101 allocator_type a{ alloc };
102 typedef std::allocator_traits< allocator_type > traity_type;
103 traity_type::destroy( a, p);
104 traity_type::deallocate( a, p, 1);
108 template< typename Fn, typename Allocator, typename ... Args >
109 class task_object< Fn, Allocator, void, Args ... > : public task_base< void, Args ... > {
111 typedef task_base< void, Args ... > base_type;
112 typedef std::allocator_traits< Allocator > allocator_traits;
115 typedef typename allocator_traits::template rebind_alloc<
116 task_object< Fn, Allocator, void, Args ... >
119 task_object( allocator_type const& alloc, Fn const& fn) :
125 task_object( allocator_type const& alloc, Fn && fn) :
127 fn_{ std::move( fn) },
131 void run( Args && ... args) override final {
133 #if defined(BOOST_NO_CXX17_STD_APPLY)
134 boost::context::detail::apply(
135 fn_, std::make_tuple( std::forward< Args >( args) ... ) );
138 fn_, std::make_tuple( std::forward< Args >( args) ... ) );
141 #if defined(BOOST_CONTEXT_HAS_CXXABI_H)
142 } catch ( abi::__forced_unwind const&) {
146 this->set_exception( std::current_exception() );
150 typename base_type::ptr_type reset() override final {
151 typedef std::allocator_traits< allocator_type > traity_type;
152 typedef pointer_traits< typename traity_type::pointer> ptrait_type;
154 typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) };
155 typename ptrait_type::element_type* p = boost::to_address(ptr);
157 traity_type::construct( alloc_, p, alloc_, std::move( fn_) );
159 traity_type::deallocate( alloc_, ptr, 1);
166 void deallocate_future() noexcept override final {
167 destroy_( alloc_, this);
172 allocator_type alloc_;
174 static void destroy_( allocator_type const& alloc, task_object * p) noexcept {
175 allocator_type a{ alloc };
176 typedef std::allocator_traits< allocator_type > traity_type;
177 traity_type::destroy( a, p);
178 traity_type::deallocate( a, p, 1);
184 #ifdef BOOST_HAS_ABI_HEADERS
185 # include BOOST_ABI_SUFFIX
188 #endif // BOOST_FIBERS_DETAIL_TASK_OBJECT_H