]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_ASYNC_COMPLETION_HPP | |
9 | #define BEAST_ASYNC_COMPLETION_HPP | |
10 | ||
11 | #include <beast/config.hpp> | |
12 | #include <beast/core/handler_concepts.hpp> | |
13 | #include <boost/asio/async_result.hpp> | |
14 | #include <boost/asio/handler_type.hpp> | |
15 | #include <type_traits> | |
16 | #include <utility> | |
17 | ||
18 | namespace beast { | |
19 | ||
20 | /** Helper for customizing the return type of asynchronous initiation functions. | |
21 | ||
22 | This class template is used to transform caller-provided completion | |
23 | handlers in calls to asynchronous initiation functions. The transformation | |
24 | allows customization of the return type of the initiating function, and the | |
25 | function signature of the final handler. | |
26 | ||
27 | @tparam CompletionHandler A completion handler, or a user defined type | |
28 | with specializations for customizing the return type (for example, | |
29 | `boost::asio::use_future` or `boost::asio::yield_context`). | |
30 | ||
31 | @tparam Signature The callable signature of the final completion handler. | |
32 | ||
33 | Example: | |
34 | @code | |
35 | ... | |
36 | template<class CompletionHandler> | |
37 | typename async_completion<CompletionHandler, | |
38 | void(error_code)>::result_type | |
39 | async_initfn(..., CompletionHandler&& handler) | |
40 | { | |
41 | async_completion<CompletionHandler, | |
42 | void(error_code)> completion{handler}; | |
43 | ... | |
44 | return completion.result.get(); | |
45 | } | |
46 | @endcode | |
47 | ||
48 | @note See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3896.pdf"> | |
49 | Library Foundations For Asynchronous Operations</a> | |
50 | */ | |
51 | template<class CompletionHandler, class Signature> | |
52 | struct async_completion | |
53 | { | |
54 | /** The type of the final handler called by the asynchronous initiation function. | |
55 | ||
56 | Objects of this type will be callable with the specified signature. | |
57 | */ | |
58 | using handler_type = | |
59 | typename boost::asio::handler_type< | |
60 | CompletionHandler, Signature>::type; | |
61 | ||
62 | /// The type of the value returned by the asynchronous initiation function. | |
63 | using result_type = typename | |
64 | boost::asio::async_result<handler_type>::type; | |
65 | ||
66 | /** Construct the helper. | |
67 | ||
68 | @param token The completion handler. Copies will be made as | |
69 | required. If `CompletionHandler` is movable, it may also be moved. | |
70 | */ | |
71 | async_completion(typename std::remove_reference<CompletionHandler>::type& token) | |
72 | : handler(std::forward<CompletionHandler>(token)) | |
73 | , result(handler) | |
74 | { | |
75 | static_assert(is_CompletionHandler<handler_type, Signature>::value, | |
76 | "Handler requirements not met"); | |
77 | } | |
78 | ||
79 | /// The final completion handler, callable with the specified signature. | |
80 | handler_type handler; | |
81 | ||
82 | /// The return value of the asynchronous initiation function. | |
83 | boost::asio::async_result<handler_type> result; | |
84 | }; | |
85 | ||
86 | } // beast | |
87 | ||
88 | #endif |