include/boost/capy/ex/immediate.hpp
100.0% Lines (22 / 22)
84.0% Functions (21 / 25)
Functions (25)
Function
Calls
Lines
Blocks
boost::capy::immediate<int>::await_ready() const
:60
3x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, double> >::await_ready() const
:60
2x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, int, int> >::await_ready() const
:60
2x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, unsigned long> >::await_ready() const
:60
9x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code> >::await_ready() const
:60
5x
100.0%
100.0%
boost::capy::immediate<int>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const
:79
1x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, double> >::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const
:79
0
0.0%
0.0%
boost::capy::immediate<std::tuple<std::error_code, int, int, int> >::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const
:79
0
0.0%
0.0%
boost::capy::immediate<std::tuple<std::error_code, unsigned long> >::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const
:79
0
0.0%
0.0%
boost::capy::immediate<std::tuple<std::error_code> >::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const
:79
0
0.0%
0.0%
boost::capy::immediate<boost::capy::immediate_test::testMoveSemantics()::move_only>::await_resume()
:95
1x
100.0%
100.0%
boost::capy::immediate<int>::await_resume()
:95
2x
100.0%
100.0%
boost::capy::immediate<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::await_resume()
:95
1x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, double> >::await_resume()
:95
3x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, int, int> >::await_resume()
:95
3x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, unsigned long> >::await_resume()
:95
9x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code> >::await_resume()
:95
5x
100.0%
100.0%
boost::capy::ready()
:127
3x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, unsigned long> > boost::capy::ready<unsigned long>(unsigned long)
:143
4x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, double> > boost::capy::ready<int, double>(int, double)
:160
2x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, int, int> > boost::capy::ready<int, int, int>(int, int, int)
:178
2x
100.0%
100.0%
boost::capy::ready(std::error_code)
:200
1x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, unsigned long> > boost::capy::ready<unsigned long>(std::error_code, unsigned long)
:217
2x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, double> > boost::capy::ready<int, double>(std::error_code, int, double)
:235
1x
100.0%
100.0%
boost::capy::immediate<std::tuple<std::error_code, int, int, int> > boost::capy::ready<int, int, int>(std::error_code, int, int, int)
:254
1x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // Copyright (c) 2026 Michael Vandeberg | ||
| 4 | // | ||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 7 | // | ||
| 8 | // Official repository: https://github.com/cppalliance/capy | ||
| 9 | // | ||
| 10 | |||
| 11 | #ifndef BOOST_CAPY_EX_IMMEDIATE_HPP | ||
| 12 | #define BOOST_CAPY_EX_IMMEDIATE_HPP | ||
| 13 | |||
| 14 | #include <boost/capy/detail/config.hpp> | ||
| 15 | #include <boost/capy/ex/await_context.hpp> | ||
| 16 | #include <boost/capy/results.hpp> | ||
| 17 | |||
| 18 | #include <coroutine> | ||
| 19 | #include <stop_token> | ||
| 20 | #include <utility> | ||
| 21 | |||
| 22 | namespace boost { | ||
| 23 | namespace capy { | ||
| 24 | |||
| 25 | /** Complete a `co_await` at once with a stored value. | ||
| 26 | |||
| 27 | This awaitable wraps a synchronous result so it can be used in | ||
| 28 | contexts that require an awaitable type. `await_ready()` always | ||
| 29 | returns `true`, so the awaiting coroutine never suspends. | ||
| 30 | |||
| 31 | Use this to adapt synchronous operations to satisfy async concepts | ||
| 32 | like @ref ContextAwaitable without the overhead of a full coroutine frame. | ||
| 33 | |||
| 34 | @par Example | ||
| 35 | @par !example example_1 | ||
| 36 | |||
| 37 | |||
| 38 | @par Building synchronous I/O operations | ||
| 39 | @par !example example_2 | ||
| 40 | |||
| 41 | |||
| 42 | @tparam T The result type to wrap. | ||
| 43 | |||
| 44 | @see @ref ready, @ref results | ||
| 45 | */ | ||
| 46 | template<class T> | ||
| 47 | struct immediate | ||
| 48 | { | ||
| 49 | /** The wrapped value. */ | ||
| 50 | T value_; | ||
| 51 | |||
| 52 | /** Check readiness; always `true`. | ||
| 53 | |||
| 54 | The awaiting coroutine does not suspend, and `await_suspend` is | ||
| 55 | never called. | ||
| 56 | |||
| 57 | @return Always `true`. | ||
| 58 | */ | ||
| 59 | constexpr bool | ||
| 60 | 21x | await_ready() const noexcept | |
| 61 | { | ||
| 62 | 21x | return true; | |
| 63 | } | ||
| 64 | |||
| 65 | /** Satisfy @ref ContextAwaitable; never called, since `await_ready` is `true`. | ||
| 66 | |||
| 67 | This overload allows `immediate` to satisfy the @ref ContextAwaitable | ||
| 68 | concept. Since the result is already available, the environment | ||
| 69 | is unused. If it were called, the returned `std::noop_coroutine()` | ||
| 70 | would leave the awaiting coroutine suspended with nothing to | ||
| 71 | resume it. | ||
| 72 | |||
| 73 | @param h The coroutine handle (unused). | ||
| 74 | @param env The execution environment (unused). | ||
| 75 | |||
| 76 | @return `std::noop_coroutine()`. | ||
| 77 | */ | ||
| 78 | std::coroutine_handle<> | ||
| 79 | 1x | await_suspend( | |
| 80 | std::coroutine_handle<> h, | ||
| 81 | await_context const* env) const noexcept | ||
| 82 | { | ||
| 83 | (void)h; | ||
| 84 | (void)env; | ||
| 85 | 1x | return std::noop_coroutine(); | |
| 86 | } | ||
| 87 | |||
| 88 | /** Return the wrapped value. | ||
| 89 | |||
| 90 | @note A move constructor of `T` that throws calls `std::terminate`. | ||
| 91 | |||
| 92 | @return The stored value, moved out. | ||
| 93 | */ | ||
| 94 | [[nodiscard]] constexpr T | ||
| 95 | 24x | await_resume() noexcept | |
| 96 | { | ||
| 97 | 24x | return std::move(value_); | |
| 98 | } | ||
| 99 | |||
| 100 | /** Return the wrapped value (const overload). | ||
| 101 | |||
| 102 | @return A reference to the stored value. Nothing is moved, so the | ||
| 103 | reference is valid only while the `immediate` is alive. | ||
| 104 | */ | ||
| 105 | [[nodiscard]] constexpr T const& | ||
| 106 | await_resume() const noexcept | ||
| 107 | { | ||
| 108 | return value_; | ||
| 109 | } | ||
| 110 | }; | ||
| 111 | |||
| 112 | /** Create an immediate awaitable for a successful `results`. | ||
| 113 | |||
| 114 | This helper creates an @ref immediate wrapping a @ref results | ||
| 115 | with no error and the provided values. | ||
| 116 | |||
| 117 | @par Example | ||
| 118 | @par !example example_1 | ||
| 119 | |||
| 120 | |||
| 121 | @return An immediate awaitable containing a successful `results`. | ||
| 122 | |||
| 123 | @see @ref immediate, @ref results | ||
| 124 | */ | ||
| 125 | inline | ||
| 126 | immediate<results<>> | ||
| 127 | 3x | ready() noexcept | |
| 128 | { | ||
| 129 | 3x | return {{}}; | |
| 130 | } | ||
| 131 | |||
| 132 | /** Create an immediate awaitable for a successful `results` with one value. | ||
| 133 | |||
| 134 | @par Exception Safety | ||
| 135 | Propagates any exception from copying or moving the arguments. | ||
| 136 | |||
| 137 | @param t1 The result value. | ||
| 138 | |||
| 139 | @return An immediate awaitable containing `results<T1>{std::error_code(), t1}`. | ||
| 140 | */ | ||
| 141 | template<class T1> | ||
| 142 | immediate<results<T1>> | ||
| 143 | 4x | ready(T1 t1) | |
| 144 | { | ||
| 145 | 4x | return {{std::error_code(), std::move(t1)}}; | |
| 146 | } | ||
| 147 | |||
| 148 | /** Create an immediate awaitable for a successful `results` with two values. | ||
| 149 | |||
| 150 | @par Exception Safety | ||
| 151 | Propagates any exception from copying or moving the arguments. | ||
| 152 | |||
| 153 | @param t1 The first result value. | ||
| 154 | @param t2 The second result value. | ||
| 155 | |||
| 156 | @return An immediate awaitable containing `results<T1,T2>{std::error_code(), t1, t2}`. | ||
| 157 | */ | ||
| 158 | template<class T1, class T2> | ||
| 159 | immediate<results<T1, T2>> | ||
| 160 | 2x | ready(T1 t1, T2 t2) | |
| 161 | { | ||
| 162 | 2x | return {{std::error_code(), std::move(t1), std::move(t2)}}; | |
| 163 | } | ||
| 164 | |||
| 165 | /** Create an immediate awaitable for a successful `results` with three values. | ||
| 166 | |||
| 167 | @par Exception Safety | ||
| 168 | Propagates any exception from copying or moving the arguments. | ||
| 169 | |||
| 170 | @param t1 The first result value. | ||
| 171 | @param t2 The second result value. | ||
| 172 | @param t3 The third result value. | ||
| 173 | |||
| 174 | @return An immediate awaitable containing `results<T1,T2,T3>{std::error_code(), t1, t2, t3}`. | ||
| 175 | */ | ||
| 176 | template<class T1, class T2, class T3> | ||
| 177 | immediate<results<T1, T2, T3>> | ||
| 178 | 2x | ready(T1 t1, T2 t2, T3 t3) | |
| 179 | { | ||
| 180 | 2x | return {{std::error_code(), std::move(t1), std::move(t2), std::move(t3)}}; | |
| 181 | } | ||
| 182 | |||
| 183 | /** Create an immediate awaitable for a failed `results`. | ||
| 184 | |||
| 185 | This helper creates an @ref immediate wrapping a @ref results | ||
| 186 | with an error code. | ||
| 187 | |||
| 188 | @par Example | ||
| 189 | @par !example example_2 | ||
| 190 | |||
| 191 | |||
| 192 | @param ec The error code. | ||
| 193 | |||
| 194 | @return An immediate awaitable containing a failed `results`. | ||
| 195 | |||
| 196 | @see @ref immediate, @ref results | ||
| 197 | */ | ||
| 198 | inline | ||
| 199 | immediate<results<>> | ||
| 200 | 1x | ready(std::error_code ec) noexcept | |
| 201 | { | ||
| 202 | 1x | return {{ec}}; | |
| 203 | } | ||
| 204 | |||
| 205 | /** Create an immediate awaitable for a `results` with error and one value. | ||
| 206 | |||
| 207 | @par Exception Safety | ||
| 208 | Propagates any exception from copying or moving the arguments. | ||
| 209 | |||
| 210 | @param ec The error code. | ||
| 211 | @param t1 The result value. | ||
| 212 | |||
| 213 | @return An immediate awaitable containing `results<T1>{ec, t1}`. | ||
| 214 | */ | ||
| 215 | template<class T1> | ||
| 216 | immediate<results<T1>> | ||
| 217 | 2x | ready(std::error_code ec, T1 t1) | |
| 218 | { | ||
| 219 | 2x | return {{ec, std::move(t1)}}; | |
| 220 | } | ||
| 221 | |||
| 222 | /** Create an immediate awaitable for a `results` with error and two values. | ||
| 223 | |||
| 224 | @par Exception Safety | ||
| 225 | Propagates any exception from copying or moving the arguments. | ||
| 226 | |||
| 227 | @param ec The error code. | ||
| 228 | @param t1 The first result value. | ||
| 229 | @param t2 The second result value. | ||
| 230 | |||
| 231 | @return An immediate awaitable containing `results<T1,T2>{ec, t1, t2}`. | ||
| 232 | */ | ||
| 233 | template<class T1, class T2> | ||
| 234 | immediate<results<T1, T2>> | ||
| 235 | 1x | ready(std::error_code ec, T1 t1, T2 t2) | |
| 236 | { | ||
| 237 | 1x | return {{ec, std::move(t1), std::move(t2)}}; | |
| 238 | } | ||
| 239 | |||
| 240 | /** Create an immediate awaitable for a `results` with error and three values. | ||
| 241 | |||
| 242 | @par Exception Safety | ||
| 243 | Propagates any exception from copying or moving the arguments. | ||
| 244 | |||
| 245 | @param ec The error code. | ||
| 246 | @param t1 The first result value. | ||
| 247 | @param t2 The second result value. | ||
| 248 | @param t3 The third result value. | ||
| 249 | |||
| 250 | @return An immediate awaitable containing `results<T1,T2,T3>{ec, t1, t2, t3}`. | ||
| 251 | */ | ||
| 252 | template<class T1, class T2, class T3> | ||
| 253 | immediate<results<T1, T2, T3>> | ||
| 254 | 1x | ready(std::error_code ec, T1 t1, T2 t2, T3 t3) | |
| 255 | { | ||
| 256 | 1x | return {{ec, std::move(t1), std::move(t2), std::move(t3)}}; | |
| 257 | } | ||
| 258 | |||
| 259 | } // namespace capy | ||
| 260 | } // namespace boost | ||
| 261 | |||
| 262 | #endif | ||
| 263 |