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