Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / impl / use_future.hpp
1 //
2 // impl/use_future.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_IMPL_USE_FUTURE_HPP
12 #define BOOST_ASIO_IMPL_USE_FUTURE_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 <future>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/system/error_code.hpp>
22 #include <boost/asio/handler_type.hpp>
23 #include <boost/system/system_error.hpp>
24 #include <boost/asio/detail/memory.hpp>
25
26 #include <boost/asio/detail/push_options.hpp>
27
28 namespace boost {
29 namespace asio {
30 namespace detail {
31
32   // Completion handler to adapt a promise as a completion handler.
33   template <typename T>
34   class promise_handler
35   {
36   public:
37     // Construct from use_future special value.
38     template <typename Alloc>
39     promise_handler(use_future_t<Alloc> uf)
40       : promise_(std::allocate_shared<std::promise<T> >(
41             BOOST_ASIO_REBIND_ALLOC(Alloc, char)(uf.get_allocator()),
42             std::allocator_arg,
43             BOOST_ASIO_REBIND_ALLOC(Alloc, char)(uf.get_allocator())))
44     {
45     }
46
47     void operator()(T t)
48     {
49       promise_->set_value(t);
50     }
51
52     void operator()(const boost::system::error_code& ec, T t)
53     {
54       if (ec)
55         promise_->set_exception(
56             std::make_exception_ptr(
57               boost::system::system_error(ec)));
58       else
59         promise_->set_value(t);
60     }
61
62   //private:
63     std::shared_ptr<std::promise<T> > promise_;
64   };
65
66   // Completion handler to adapt a void promise as a completion handler.
67   template <>
68   class promise_handler<void>
69   {
70   public:
71     // Construct from use_future special value. Used during rebinding.
72     template <typename Alloc>
73     promise_handler(use_future_t<Alloc> uf)
74       : promise_(std::allocate_shared<std::promise<void> >(
75             BOOST_ASIO_REBIND_ALLOC(Alloc, char)(uf.get_allocator()),
76             std::allocator_arg,
77             BOOST_ASIO_REBIND_ALLOC(Alloc, char)(uf.get_allocator())))
78     {
79     }
80
81     void operator()()
82     {
83       promise_->set_value();
84     }
85
86     void operator()(const boost::system::error_code& ec)
87     {
88       if (ec)
89         promise_->set_exception(
90             std::make_exception_ptr(
91               boost::system::system_error(ec)));
92       else
93         promise_->set_value();
94     }
95
96   //private:
97     std::shared_ptr<std::promise<void> > promise_;
98   };
99
100   // Ensure any exceptions thrown from the handler are propagated back to the
101   // caller via the future.
102   template <typename Function, typename T>
103   void asio_handler_invoke(Function f, promise_handler<T>* h)
104   {
105     std::shared_ptr<std::promise<T> > p(h->promise_);
106     try
107     {
108       f();
109     }
110     catch (...)
111     {
112       p->set_exception(std::current_exception());
113     }
114   }
115
116 } // namespace detail
117
118 #if !defined(GENERATING_DOCUMENTATION)
119
120 // Handler traits specialisation for promise_handler.
121 template <typename T>
122 class async_result<detail::promise_handler<T> >
123 {
124 public:
125   // The initiating function will return a future.
126   typedef std::future<T> type;
127
128   // Constructor creates a new promise for the async operation, and obtains the
129   // corresponding future.
130   explicit async_result(detail::promise_handler<T>& h)
131   {
132     value_ = h.promise_->get_future();
133   }
134
135   // Obtain the future to be returned from the initiating function.
136   type get() { return std::move(value_); }
137
138 private:
139   type value_;
140 };
141
142 // Handler type specialisation for use_future.
143 template <typename Allocator, typename ReturnType>
144 struct handler_type<use_future_t<Allocator>, ReturnType()>
145 {
146   typedef detail::promise_handler<void> type;
147 };
148
149 // Handler type specialisation for use_future.
150 template <typename Allocator, typename ReturnType, typename Arg1>
151 struct handler_type<use_future_t<Allocator>, ReturnType(Arg1)>
152 {
153   typedef detail::promise_handler<Arg1> type;
154 };
155
156 // Handler type specialisation for use_future.
157 template <typename Allocator, typename ReturnType>
158 struct handler_type<use_future_t<Allocator>,
159     ReturnType(boost::system::error_code)>
160 {
161   typedef detail::promise_handler<void> type;
162 };
163
164 // Handler type specialisation for use_future.
165 template <typename Allocator, typename ReturnType, typename Arg2>
166 struct handler_type<use_future_t<Allocator>,
167     ReturnType(boost::system::error_code, Arg2)>
168 {
169   typedef detail::promise_handler<Arg2> type;
170 };
171
172 #endif // !defined(GENERATING_DOCUMENTATION)
173
174 } // namespace asio
175 } // namespace boost
176
177 #include <boost/asio/detail/pop_options.hpp>
178
179 #endif // BOOST_ASIO_IMPL_USE_FUTURE_HPP