5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_USE_FUTURE_HPP
12 #define BOOST_ASIO_USE_FUTURE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_STD_FUTURE) \
21 || defined(GENERATING_DOCUMENTATION)
24 #include <boost/asio/detail/type_traits.hpp>
26 #include <boost/asio/detail/push_options.hpp>
32 template <typename Function, typename Allocator>
35 template <typename Function, typename Allocator, typename Result>
36 class packaged_handler;
40 /// Class used to specify that an asynchronous operation should return a future.
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:
47 * @code std::future<std::size_t> my_future
48 * = my_socket.async_read_some(my_buffer, boost::asio::use_future); @endcode
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.
55 template <typename Allocator = std::allocator<void> >
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;
63 /// Construct using default-constructed allocator.
64 BOOST_ASIO_CONSTEXPR use_future_t()
68 /// Construct using specified allocator.
69 explicit use_future_t(const Allocator& allocator)
70 : allocator_(allocator)
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
79 return use_future_t<OtherAllocator>(allocator);
81 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
83 /// Specify an alternate allocator.
84 template <typename OtherAllocator>
85 use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
87 return use_future_t<OtherAllocator>(allocator);
91 allocator_type get_allocator() const
96 /// Wrap a function object in a packaged task.
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.
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)
111 * std::size_t n = fut.get(); @endcode
113 template <typename Function>
114 #if defined(GENERATING_DOCUMENTATION)
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;
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
126 BOOST_ASIO_CONSTEXPR std_allocator_void()
130 operator std::allocator<void>() const
132 return std::allocator<void>();
136 typename conditional<
137 is_same<std::allocator<void>, Allocator>::value,
138 std_allocator_void, Allocator>::type allocator_;
141 /// A special value, similar to std::nothrow.
143 * See the documentation for boost::asio::use_future_t for a usage example.
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;
154 #include <boost/asio/detail/pop_options.hpp>
156 #include <boost/asio/impl/use_future.hpp>
158 #endif // defined(BOOST_ASIO_HAS_STD_FUTURE)
159 // || defined(GENERATING_DOCUMENTATION)
161 #endif // BOOST_ASIO_USE_FUTURE_HPP