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_PACKAGED_TASK_HPP
8 #define BOOST_FIBERS_PACKAGED_TASK_HPP
12 #include <type_traits>
15 #include <boost/config.hpp>
17 #include <boost/fiber/detail/disable_overload.hpp>
18 #include <boost/fiber/exceptions.hpp>
19 #include <boost/fiber/future/detail/task_base.hpp>
20 #include <boost/fiber/future/detail/task_object.hpp>
21 #include <boost/fiber/future/future.hpp>
26 template< typename Signature >
29 template< typename R, typename ... Args >
30 class packaged_task< R( Args ... ) > {
32 typedef typename detail::task_base< R, Args ... >::ptr_type ptr_type;
34 bool obtained_{ false };
38 packaged_task() = default;
40 template< typename Fn,
41 typename = detail::disable_overload< packaged_task, Fn >
43 explicit packaged_task( Fn && fn) :
44 packaged_task{ std::allocator_arg,
45 std::allocator< packaged_task >{},
46 std::forward< Fn >( fn) } {
49 template< typename Fn,
52 explicit packaged_task( std::allocator_arg_t, Allocator const& alloc, Fn && fn) {
53 typedef detail::task_object<
54 typename std::decay< Fn >::type, Allocator, R, Args ...
56 typedef std::allocator_traits<
57 typename object_type::allocator_type
59 typedef pointer_traits< typename traits_type::pointer > ptrait_type;
61 typename object_type::allocator_type a{ alloc };
62 typename traits_type::pointer ptr{ traits_type::allocate( a, 1) };
63 typename ptrait_type::element_type* p = boost::to_address(ptr);
65 traits_type::construct( a, p, a, std::forward< Fn >( fn) );
67 traits_type::deallocate( a, ptr, 1);
75 task_->owner_destroyed();
79 packaged_task( packaged_task const&) = delete;
80 packaged_task & operator=( packaged_task const&) = delete;
82 packaged_task( packaged_task && other) noexcept :
83 obtained_{ other.obtained_ },
84 task_{ std::move( other.task_) } {
85 other.obtained_ = false;
88 packaged_task & operator=( packaged_task && other) noexcept {
89 if ( BOOST_LIKELY( this != & other) ) {
90 packaged_task tmp{ std::move( other) };
96 void swap( packaged_task & other) noexcept {
97 std::swap( obtained_, other.obtained_);
98 task_.swap( other.task_);
101 bool valid() const noexcept {
102 return nullptr != task_.get();
105 future< R > get_future() {
107 throw future_already_retrieved{};
109 if ( BOOST_UNLIKELY( ! valid() ) ) {
110 throw packaged_task_uninitialized{};
114 boost::static_pointer_cast< detail::shared_state< R > >( task_) };
117 void operator()( Args ... args) {
118 if ( BOOST_UNLIKELY( ! valid() ) ) {
119 throw packaged_task_uninitialized{};
121 task_->run( std::forward< Args >( args) ... );
125 if ( BOOST_UNLIKELY( ! valid() ) ) {
126 throw packaged_task_uninitialized{};
130 task_ = tmp.task_->reset();
135 template< typename Signature >
136 void swap( packaged_task< Signature > & l, packaged_task< Signature > & r) noexcept {
142 #endif // BOOST_FIBERS_PACKAGED_TASK_HPP