]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/use_future.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / use_future.hpp
1 //
2 // 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_USE_FUTURE_HPP
12 #define BOOST_ASIO_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
20 #if defined(BOOST_ASIO_HAS_STD_FUTURE) \
21 || defined(GENERATING_DOCUMENTATION)
22
23 #include <memory>
24 #include <boost/asio/detail/type_traits.hpp>
25
26 #include <boost/asio/detail/push_options.hpp>
27
28 namespace boost {
29 namespace asio {
30 namespace detail {
31
32 template <typename Function, typename Allocator>
33 class packaged_token;
34
35 template <typename Function, typename Allocator, typename Result>
36 class packaged_handler;
37
38 } // namespace detail
39
40 /// Class used to specify that an asynchronous operation should return a future.
41 /**
42 * The use_future_t class is used to indicate that an asynchronous operation
43 * should return a std::future object. A use_future_t object may be passed as a
44 * handler to an asynchronous operation, typically using the special value @c
45 * boost::asio::use_future. For example:
46 *
47 * @code std::future<std::size_t> my_future
48 * = my_socket.async_read_some(my_buffer, boost::asio::use_future); @endcode
49 *
50 * The initiating function (async_read_some in the above example) returns a
51 * future that will receive the result of the operation. If the operation
52 * completes with an error_code indicating failure, it is converted into a
53 * system_error and passed back to the caller via the future.
54 */
55 template <typename Allocator = std::allocator<void> >
56 class use_future_t
57 {
58 public:
59 /// The allocator type. The allocator is used when constructing the
60 /// @c std::promise object for a given asynchronous operation.
61 typedef Allocator allocator_type;
62
63 /// Construct using default-constructed allocator.
64 BOOST_ASIO_CONSTEXPR use_future_t()
65 {
66 }
67
68 /// Construct using specified allocator.
69 explicit use_future_t(const Allocator& allocator)
70 : allocator_(allocator)
71 {
72 }
73
74 #if !defined(BOOST_ASIO_NO_DEPRECATED)
75 /// (Deprecated: Use rebind().) Specify an alternate allocator.
76 template <typename OtherAllocator>
77 use_future_t<OtherAllocator> operator[](const OtherAllocator& allocator) const
78 {
79 return use_future_t<OtherAllocator>(allocator);
80 }
81 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
82
83 /// Specify an alternate allocator.
84 template <typename OtherAllocator>
85 use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
86 {
87 return use_future_t<OtherAllocator>(allocator);
88 }
89
90 /// Obtain allocator.
91 allocator_type get_allocator() const
92 {
93 return allocator_;
94 }
95
96 /// Wrap a function object in a packaged task.
97 /**
98 * The @c package function is used to adapt a function object as a packaged
99 * task. When this adapter is passed as a completion token to an asynchronous
100 * operation, the result of the function object is retuned via a std::future.
101 *
102 * @par Example
103 *
104 * @code std::future<std::size_t> fut =
105 * my_socket.async_read_some(buffer,
106 * use_future([](boost::system::error_code ec, std::size_t n)
107 * {
108 * return ec ? 0 : n;
109 * }));
110 * ...
111 * std::size_t n = fut.get(); @endcode
112 */
113 template <typename Function>
114 #if defined(GENERATING_DOCUMENTATION)
115 unspecified
116 #else // defined(GENERATING_DOCUMENTATION)
117 detail::packaged_token<typename decay<Function>::type, Allocator>
118 #endif // defined(GENERATING_DOCUMENTATION)
119 operator()(BOOST_ASIO_MOVE_ARG(Function) f) const;
120
121 private:
122 // Helper type to ensure that use_future can be constexpr default-constructed
123 // even when std::allocator<void> can't be.
124 struct std_allocator_void
125 {
126 BOOST_ASIO_CONSTEXPR std_allocator_void()
127 {
128 }
129
130 operator std::allocator<void>() const
131 {
132 return std::allocator<void>();
133 }
134 };
135
136 typename conditional<
137 is_same<std::allocator<void>, Allocator>::value,
138 std_allocator_void, Allocator>::type allocator_;
139 };
140
141 /// A special value, similar to std::nothrow.
142 /**
143 * See the documentation for boost::asio::use_future_t for a usage example.
144 */
145 #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
146 constexpr use_future_t<> use_future;
147 #elif defined(BOOST_ASIO_MSVC)
148 __declspec(selectany) use_future_t<> use_future;
149 #endif
150
151 } // namespace asio
152 } // namespace boost
153
154 #include <boost/asio/detail/pop_options.hpp>
155
156 #include <boost/asio/impl/use_future.hpp>
157
158 #endif // defined(BOOST_ASIO_HAS_STD_FUTURE)
159 // || defined(GENERATING_DOCUMENTATION)
160
161 #endif // BOOST_ASIO_USE_FUTURE_HPP