1 | // |
2 | // async_result.hpp |
3 | // ~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 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 ARCHETYPES_ASYNC_RESULT_HPP |
12 | #define ARCHETYPES_ASYNC_RESULT_HPP |
13 | |
14 | #include <boost/asio/async_result.hpp> |
15 | #include <boost/asio/system_executor.hpp> |
16 | |
17 | namespace archetypes { |
18 | |
19 | struct immediate_handler |
20 | { |
21 | }; |
22 | |
23 | struct lazy_handler |
24 | { |
25 | }; |
26 | |
27 | template <typename Signature> |
28 | struct concrete_handler; |
29 | |
30 | template <typename R, typename Arg1> |
31 | struct concrete_handler<R(Arg1)> |
32 | { |
33 | concrete_handler() |
34 | { |
35 | } |
36 | |
37 | void operator()(typename boost::asio::decay<Arg1>::type) |
38 | { |
39 | } |
40 | |
41 | concrete_handler(concrete_handler&&) {} |
42 | private: |
43 | concrete_handler(const concrete_handler&); |
44 | }; |
45 | |
46 | template <typename R, typename Arg1, typename Arg2> |
47 | struct concrete_handler<R(Arg1, Arg2)> |
48 | { |
49 | concrete_handler() |
50 | { |
51 | } |
52 | |
53 | void operator()(typename boost::asio::decay<Arg1>::type, |
54 | typename boost::asio::decay<Arg2>::type) |
55 | { |
56 | } |
57 | }; |
58 | |
59 | template <typename Signature> |
60 | struct immediate_concrete_handler : concrete_handler<Signature> |
61 | { |
62 | typedef boost::asio::system_executor immediate_executor_type; |
63 | |
64 | immediate_concrete_handler(immediate_handler) |
65 | { |
66 | } |
67 | |
68 | immediate_executor_type get_immediate_executor() const noexcept |
69 | { |
70 | return immediate_executor_type(); |
71 | } |
72 | |
73 | immediate_concrete_handler(immediate_concrete_handler&&) {} |
74 | private: |
75 | immediate_concrete_handler(const immediate_concrete_handler&); |
76 | }; |
77 | |
78 | template <typename Signature> |
79 | struct lazy_concrete_handler : concrete_handler<Signature> |
80 | { |
81 | lazy_concrete_handler(lazy_handler) |
82 | { |
83 | } |
84 | |
85 | lazy_concrete_handler(lazy_concrete_handler&&) {} |
86 | private: |
87 | lazy_concrete_handler(const lazy_concrete_handler&); |
88 | }; |
89 | |
90 | } // namespace archetypes |
91 | |
92 | namespace boost { |
93 | namespace asio { |
94 | |
95 | template <typename Signature> |
96 | class async_result<archetypes::immediate_handler, Signature> |
97 | { |
98 | public: |
99 | // The concrete completion handler type. |
100 | typedef archetypes::immediate_concrete_handler<Signature> |
101 | completion_handler_type; |
102 | |
103 | // The return type of the initiating function. |
104 | typedef void return_type; |
105 | |
106 | // Construct an async_result from a given handler. |
107 | explicit async_result(completion_handler_type&) |
108 | { |
109 | } |
110 | |
111 | // Obtain the value to be returned from the initiating function. |
112 | void get() |
113 | { |
114 | } |
115 | |
116 | private: |
117 | // Disallow copying and assignment. |
118 | async_result(const async_result&) = delete; |
119 | async_result& operator=(const async_result&) = delete; |
120 | }; |
121 | |
122 | template <typename Signature> |
123 | class async_result<archetypes::lazy_handler, Signature> |
124 | { |
125 | public: |
126 | // The concrete completion handler type. |
127 | typedef archetypes::lazy_concrete_handler<Signature> completion_handler_type; |
128 | |
129 | // The return type of the initiating function. |
130 | typedef int return_type; |
131 | |
132 | // Construct an async_result from a given handler. |
133 | explicit async_result(completion_handler_type&) |
134 | { |
135 | } |
136 | |
137 | // Obtain the value to be returned from the initiating function. |
138 | return_type get() |
139 | { |
140 | return 42; |
141 | } |
142 | |
143 | private: |
144 | // Disallow copying and assignment. |
145 | async_result(const async_result&) = delete; |
146 | async_result& operator=(const async_result&) = delete; |
147 | }; |
148 | |
149 | } // namespace asio |
150 | } // namespace boost |
151 | |
152 | #endif // ARCHETYPES_ASYNC_RESULT_HPP |
153 | |