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