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 #if defined(BOOST_NO_CXX17_STD_APPLY)
17 #include <boost/context/detail/apply.hpp>
19 #include <boost/core/pointer_traits.hpp>
21 #include <boost/fiber/detail/config.hpp>
22 #include <boost/fiber/future/detail/task_base.hpp>
24 #ifdef BOOST_HAS_ABI_HEADERS
25 # include BOOST_ABI_PREFIX
32 template< typename Fn, typename Allocator, typename R, typename ... Args >
33 class task_object : public task_base< R, Args ... > {
35 typedef task_base< R, Args ... > base_type;
36 typedef std::allocator_traits< Allocator > allocator_traits;
39 typedef typename allocator_traits::template rebind_alloc<
43 task_object( allocator_type const& alloc, Fn const& fn) :
49 task_object( allocator_type const& alloc, Fn && fn) :
51 fn_{ std::move( fn) },
55 void run( Args && ... args) override final {
58 #if defined(BOOST_NO_CXX17_STD_APPLY)
59 boost::context::detail::apply(
60 fn_, std::make_tuple( std::forward< Args >( args) ... ) )
63 fn_, std::make_tuple( std::forward< Args >( args) ... ) )
67 this->set_exception( std::current_exception() );
71 typename base_type::ptr_type reset() override final {
72 typedef std::allocator_traits< allocator_type > traity_type;
73 typedef pointer_traits< typename traity_type::pointer> ptrait_type;
75 typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) };
76 typename ptrait_type::element_type* p = boost::to_address(ptr);
78 traity_type::construct( alloc_, p, alloc_, std::move( fn_) );
80 traity_type::deallocate( alloc_, ptr, 1);
87 void deallocate_future() noexcept override final {
88 destroy_( alloc_, this);
93 allocator_type alloc_;
95 static void destroy_( allocator_type const& alloc, task_object * p) noexcept {
96 allocator_type a{ alloc };
97 typedef std::allocator_traits< allocator_type > traity_type;
98 traity_type::destroy( a, p);
99 traity_type::deallocate( a, p, 1);
103 template< typename Fn, typename Allocator, typename ... Args >
104 class task_object< Fn, Allocator, void, Args ... > : public task_base< void, Args ... > {
106 typedef task_base< void, Args ... > base_type;
107 typedef std::allocator_traits< Allocator > allocator_traits;
110 typedef typename allocator_traits::template rebind_alloc<
111 task_object< Fn, Allocator, void, Args ... >
114 task_object( allocator_type const& alloc, Fn const& fn) :
120 task_object( allocator_type const& alloc, Fn && fn) :
122 fn_{ std::move( fn) },
126 void run( Args && ... args) override final {
128 #if defined(BOOST_NO_CXX17_STD_APPLY)
129 boost::context::detail::apply(
130 fn_, std::make_tuple( std::forward< Args >( args) ... ) );
133 fn_, std::make_tuple( std::forward< Args >( args) ... ) );
137 this->set_exception( std::current_exception() );
141 typename base_type::ptr_type reset() override final {
142 typedef std::allocator_traits< allocator_type > traity_type;
143 typedef pointer_traits< typename traity_type::pointer> ptrait_type;
145 typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) };
146 typename ptrait_type::element_type* p = boost::to_address(ptr);
148 traity_type::construct( alloc_, p, alloc_, std::move( fn_) );
150 traity_type::deallocate( alloc_, ptr, 1);
157 void deallocate_future() noexcept override final {
158 destroy_( alloc_, this);
163 allocator_type alloc_;
165 static void destroy_( allocator_type const& alloc, task_object * p) noexcept {
166 allocator_type a{ alloc };
167 typedef std::allocator_traits< allocator_type > traity_type;
168 traity_type::destroy( a, p);
169 traity_type::deallocate( a, p, 1);
175 #ifdef BOOST_HAS_ABI_HEADERS
176 # include BOOST_ABI_SUFFIX
179 #endif // BOOST_FIBERS_DETAIL_TASK_OBJECT_H