1 //
2 // packaged_task.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef BOOST_ASIO_PACKAGED_TASK_HPP
12 #define BOOST_ASIO_PACKAGED_TASK_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/future.hpp>
20 
21 #if defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) \
22   || defined(GENERATING_DOCUMENTATION)
23 
24 #include <boost/asio/async_result.hpp>
25 #include <boost/asio/detail/type_traits.hpp>
26 #include <boost/asio/detail/variadic_templates.hpp>
27 
28 #include <boost/asio/detail/push_options.hpp>
29 
30 namespace boost {
31 namespace asio {
32 
33 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) \
34   || defined(GENERATING_DOCUMENTATION)
35 
36 /// Partial specialisation of @c async_result for @c std::packaged_task.
37 template <typename Result, typename... Args, typename Signature>
38 class async_result<std::packaged_task<Result(Args...)>, Signature>
39 {
40 public:
41   /// The packaged task is the concrete completion handler type.
42   typedef std::packaged_task<Result(Args...)> completion_handler_type;
43 
44   /// The return type of the initiating function is the future obtained from
45   /// the packaged task.
46   typedef std::future<Result> return_type;
47 
48   /// The constructor extracts the future from the packaged task.
async_result(completion_handler_type & h)49   explicit async_result(completion_handler_type& h)
50     : future_(h.get_future())
51   {
52   }
53 
54   /// Returns the packaged task's future.
get()55   return_type get()
56   {
57     return std::move(future_);
58   }
59 
60 private:
61   return_type future_;
62 };
63 
64 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
65       //   || defined(GENERATING_DOCUMENTATION)
66 
67 template <typename Result, typename Signature>
68 struct async_result<std::packaged_task<Result()>, Signature>
69 {
70   typedef std::packaged_task<Result()> completion_handler_type;
71   typedef std::future<Result> return_type;
72 
73   explicit async_result(completion_handler_type& h)
74     : future_(h.get_future())
75   {
76   }
77 
78   return_type get()
79   {
80     return std::move(future_);
81   }
82 
83 private:
84   return_type future_;
85 };
86 
87 #define BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
88   template <typename Result, \
89     BOOST_ASIO_VARIADIC_TPARAMS(n), typename Signature> \
90   class async_result< \
91     std::packaged_task<Result(BOOST_ASIO_VARIADIC_TARGS(n))>, Signature> \
92   { \
93   public: \
94     typedef std::packaged_task< \
95       Result(BOOST_ASIO_VARIADIC_TARGS(n))> \
96         completion_handler_type; \
97   \
98     typedef std::future<Result> return_type; \
99   \
100     explicit async_result(completion_handler_type& h) \
101       : future_(h.get_future()) \
102     { \
103     } \
104   \
105     return_type get() \
106     { \
107       return std::move(future_); \
108     } \
109   \
110   private: \
111     return_type future_; \
112   }; \
113   /**/
114   BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF)
115 #undef BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF
116 
117 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
118        //   || defined(GENERATING_DOCUMENTATION)
119 
120 } // namespace asio
121 } // namespace boost
122 
123 #include <boost/asio/detail/pop_options.hpp>
124 
125 #endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS)
126        //   || defined(GENERATING_DOCUMENTATION)
127 
128 #endif // BOOST_ASIO_PACKAGED_TASK_HPP
129