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;
38 typedef typename std::allocator_traits< Allocator >::template rebind_alloc<
42 task_object( allocator_type const& alloc, Fn const& fn) :
48 task_object( allocator_type const& alloc, Fn && fn) :
50 fn_{ std::move( fn) },
54 void run( Args && ... args) override final {
57 #if defined(BOOST_NO_CXX17_STD_APPLY)
58 boost::context::detail::apply(
59 fn_, std::make_tuple( std::forward< Args >( args) ... ) )
62 fn_, std::make_tuple( std::forward< Args >( args) ... ) )
66 this->set_exception( std::current_exception() );
70 typename base_type::ptr_type reset() override final {
71 typedef std::allocator_traits< allocator_type > traity_type;
72 typedef pointer_traits< typename traity_type::pointer> ptrait_type;
74 typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) };
75 typename ptrait_type::element_type* p = boost::to_address(ptr);
77 traity_type::construct( alloc_, p, alloc_, std::move( fn_) );
79 traity_type::deallocate( alloc_, ptr, 1);
86 void deallocate_future() noexcept override final {
87 destroy_( alloc_, this);
92 allocator_type alloc_;
94 static void destroy_( allocator_type const& alloc, task_object * p) noexcept {
95 allocator_type a{ alloc };
101 template< typename Fn, typename Allocator, typename ... Args >
102 class task_object< Fn, Allocator, void, Args ... > : public task_base< void, Args ... > {
104 typedef task_base< void, Args ... > base_type;
107 typedef typename Allocator::template rebind<
108 task_object< Fn, Allocator, void, Args ... >
109 >::other allocator_type;
111 task_object( allocator_type const& alloc, Fn const& fn) :
117 task_object( allocator_type const& alloc, Fn && fn) :
119 fn_{ std::move( fn) },
123 void run( Args && ... args) override final {
125 #if defined(BOOST_NO_CXX17_STD_APPLY)
126 boost::context::detail::apply(
127 fn_, std::make_tuple( std::forward< Args >( args) ... ) );
130 fn_, std::make_tuple( std::forward< Args >( args) ... ) );
134 this->set_exception( std::current_exception() );
138 typename base_type::ptr_type reset() override final {
139 typedef std::allocator_traits< allocator_type > traity_type;
140 typedef pointer_traits< typename traity_type::pointer> ptrait_type;
142 typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) };
143 typename ptrait_type::element_type* p = boost::to_address(ptr);
145 traity_type::construct( alloc_, p, alloc_, std::move( fn_) );
147 traity_type::deallocate( alloc_, ptr, 1);
154 void deallocate_future() noexcept override final {
155 destroy_( alloc_, this);
160 allocator_type alloc_;
162 static void destroy_( allocator_type const& alloc, task_object * p) noexcept {
163 allocator_type a{ alloc };
171 #ifdef BOOST_HAS_ABI_HEADERS
172 # include BOOST_ABI_SUFFIX
175 #endif // BOOST_FIBERS_DETAIL_TASK_OBJECT_H