use_awaitable.hpp
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// use_awaitable.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_USE_AWAITABLE_HPP
#define ASIO_USE_AWAITABLE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#if defined(ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
#include "asio/awaitable.hpp"
#include "asio/detail/handler_tracking.hpp"
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
# if defined(ASIO_HAS_SOURCE_LOCATION)
# include "asio/detail/source_location.hpp"
# endif // defined(ASIO_HAS_SOURCE_LOCATION)
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
#include "asio/detail/push_options.hpp"
namespace asio {
/// A completion token that represents the currently executing coroutine.
/**
* The @c use_awaitable_t class, with its value @c use_awaitable, is used to
* represent the currently executing coroutine. This completion token may be
* passed as a handler to an asynchronous operation. For example:
*
* @code awaitable<void> my_coroutine()
* {
* std::size_t n = co_await my_socket.async_read_some(buffer, use_awaitable);
* ...
* } @endcode
*
* When used with co_await, the initiating function (@c async_read_some in the
* above example) suspends the current coroutine. The coroutine is resumed when
* the asynchronous operation completes, and the result of the operation is
* returned.
*/
template <typename Executor = any_io_executor>
struct use_awaitable_t
{
/// Default constructor.
ASIO_CONSTEXPR use_awaitable_t(
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
# if defined(ASIO_HAS_SOURCE_LOCATION)
detail::source_location location = detail::source_location::current()
# endif // defined(ASIO_HAS_SOURCE_LOCATION)
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
)
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
# if defined(ASIO_HAS_SOURCE_LOCATION)
: file_name_(location.file_name()),
line_(location.line()),
function_name_(location.function_name())
# else // defined(ASIO_HAS_SOURCE_LOCATION)
: file_name_(0),
line_(0),
function_name_(0)
# endif // defined(ASIO_HAS_SOURCE_LOCATION)
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
{
}
/// Constructor used to specify file name, line, and function name.
ASIO_CONSTEXPR use_awaitable_t(const char* file_name,
int line, const char* function_name)
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
: file_name_(file_name),
line_(line),
function_name_(function_name)
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
{
#if !defined(ASIO_ENABLE_HANDLER_TRACKING)
(void)file_name;
(void)line;
(void)function_name;
#endif // !defined(ASIO_ENABLE_HANDLER_TRACKING)
}
/// Adapts an executor to add the @c use_awaitable_t completion token as the
/// default.
template <typename InnerExecutor>
struct executor_with_default : InnerExecutor
{
/// Specify @c use_awaitable_t as the default completion token type.
typedef use_awaitable_t default_completion_token_type;
/// Construct the adapted executor from the inner executor type.
executor_with_default(const InnerExecutor& ex) ASIO_NOEXCEPT
: InnerExecutor(ex)
{
}
/// Convert the specified executor to the inner executor type, then use
/// that to construct the adapted executor.
template <typename OtherExecutor>
executor_with_default(const OtherExecutor& ex,
typename enable_if<
is_convertible<OtherExecutor, InnerExecutor>::value
>::type* = 0) ASIO_NOEXCEPT
: InnerExecutor(ex)
{
}
};
/// Type alias to adapt an I/O object to use @c use_awaitable_t as its
/// default completion token type.
#if defined(ASIO_HAS_ALIAS_TEMPLATES) \
|| defined(GENERATING_DOCUMENTATION)
template <typename T>
using as_default_on_t = typename T::template rebind_executor<
executor_with_default<typename T::executor_type> >::other;
#endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
// || defined(GENERATING_DOCUMENTATION)
/// Function helper to adapt an I/O object to use @c use_awaitable_t as its
/// default completion token type.
template <typename T>
static typename decay<T>::type::template rebind_executor<
executor_with_default<typename decay<T>::type::executor_type>
>::other
as_default_on(ASIO_MOVE_ARG(T) object)
{
return typename decay<T>::type::template rebind_executor<
executor_with_default<typename decay<T>::type::executor_type>
>::other(ASIO_MOVE_CAST(T)(object));
}
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
const char* file_name_;
int line_;
const char* function_name_;
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
};
/// A completion token object that represents the currently executing coroutine.
/**
* See the documentation for asio::use_awaitable_t for a usage example.
*/
#if defined(GENERATING_DOCUMENTATION)
constexpr use_awaitable_t<> use_awaitable;
#elif defined(ASIO_HAS_CONSTEXPR)
constexpr use_awaitable_t<> use_awaitable(0, 0, 0);
#elif defined(ASIO_MSVC)
__declspec(selectany) use_awaitable_t<> use_awaitable(0, 0, 0);
#endif
} // namespace asio
#include "asio/detail/pop_options.hpp"
#include "asio/impl/use_awaitable.hpp"
#endif // defined(ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
#endif // ASIO_USE_AWAITABLE_HPP