100.00% Lines (22/22) 100.00% Functions (8/8)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Michael Vandeberg 2   // Copyright (c) 2026 Michael Vandeberg
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_DETAIL_OP_BASE_HPP 10   #ifndef BOOST_COROSIO_DETAIL_OP_BASE_HPP
11   #define BOOST_COROSIO_DETAIL_OP_BASE_HPP 11   #define BOOST_COROSIO_DETAIL_OP_BASE_HPP
12   12  
13   #include <boost/capy/io_result.hpp> 13   #include <boost/capy/io_result.hpp>
14   #include <boost/capy/ex/executor_ref.hpp> 14   #include <boost/capy/ex/executor_ref.hpp>
15   #include <boost/capy/ex/io_env.hpp> 15   #include <boost/capy/ex/io_env.hpp>
16   16  
17   #include <coroutine> 17   #include <coroutine>
18   #include <cstddef> 18   #include <cstddef>
19   #include <stop_token> 19   #include <stop_token>
20   #include <system_error> 20   #include <system_error>
21   21  
22   namespace boost::corosio::detail { 22   namespace boost::corosio::detail {
23   23  
24   /* CRTP base for awaitables that return io_result<std::size_t>. 24   /* CRTP base for awaitables that return io_result<std::size_t>.
25   25  
26   Derived classes must provide: 26   Derived classes must provide:
27   27  
28   std::coroutine_handle<> dispatch( 28   std::coroutine_handle<> dispatch(
29   std::coroutine_handle<> h, 29   std::coroutine_handle<> h,
30   capy::executor_ref ex) const; 30   capy::executor_ref ex) const;
31   31  
32   which forwards to the backend implementation method, passing 32   which forwards to the backend implementation method, passing
33   token_, &ec_, and &bytes_ as the cancellation/output parameters. 33   token_, &ec_, and &bytes_ as the cancellation/output parameters.
34   */ 34   */
35   template<class Derived> 35   template<class Derived>
36   class bytes_op_base 36   class bytes_op_base
37   { 37   {
38   friend Derived; 38   friend Derived;
HITCBC 39   394966 bytes_op_base() = default; 39   394212 bytes_op_base() = default;
40   40  
41   public: 41   public:
42   std::stop_token token_; 42   std::stop_token token_;
43   mutable std::error_code ec_; 43   mutable std::error_code ec_;
44   mutable std::size_t bytes_ = 0; 44   mutable std::size_t bytes_ = 0;
45   45  
HITCBC 46   394966 bool await_ready() const noexcept 46   394212 bool await_ready() const noexcept
47   { 47   {
48   // A pre-set ec_ means the initiator failed before dispatch 48   // A pre-set ec_ means the initiator failed before dispatch
49   // (e.g. a closed object); complete immediately with that error. 49   // (e.g. a closed object); complete immediately with that error.
HITCBC 50   394966 return static_cast<bool>(ec_) || token_.stop_requested(); 50   394212 return static_cast<bool>(ec_) || token_.stop_requested();
51   } 51   }
52   52  
HITCBC 53   394933 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept 53   394179 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept
54   { 54   {
HITCBC 55   394933 if (token_.stop_requested()) 55   394179 if (token_.stop_requested())
HITCBC 56   245 return {make_error_code(std::errc::operation_canceled), 0}; 56   243 return {make_error_code(std::errc::operation_canceled), 0};
HITCBC 57   394688 return {ec_, bytes_}; 57   393936 return {ec_, bytes_};
58   } 58   }
59   59  
HITCBC 60   394950 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 60   394196 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
61   -> std::coroutine_handle<> 61   -> std::coroutine_handle<>
62   { 62   {
HITCBC 63   394950 token_ = env->stop_token; 63   394196 token_ = env->stop_token;
HITCBC 64   394950 return static_cast<Derived const*>(this)->dispatch( 64   394196 return static_cast<Derived const*>(this)->dispatch(
HITCBC 65   394950 h, env->executor); 65   394196 h, env->executor);
66   } 66   }
67   }; 67   };
68 - /* CRTP base for awaitables that return io_result<Value> for a  
69 - moved-out result object (e.g. the resolver's result lists).  
70 -  
71 - Derived classes must provide:  
72 -  
73 - std::coroutine_handle<> dispatch(  
74 - std::coroutine_handle<> h,  
75 - capy::executor_ref ex) const;  
76 -  
77 - which forwards to the backend implementation method, passing  
78 - token_, &ec_, and &value_ as the cancellation/output parameters.  
79 - */  
80 - template<class Derived, class Value>  
81 - class value_op_base  
82 - {  
83 - friend Derived;  
84 - value_op_base() = default;  
DCB 85 - 50  
86 - public:  
87 - std::stop_token token_;  
88 - mutable std::error_code ec_;  
89 - mutable Value value_{};  
90 -  
91 - bool await_ready() const noexcept  
DCB 92 - 50 {  
93 - // A pre-set ec_ means the initiator failed before dispatch;  
94 - // complete immediately with that error.  
95 - return static_cast<bool>(ec_) || token_.stop_requested();  
DCB 96 - 50 }  
97 -  
98 - [[nodiscard]] capy::io_result<Value> await_resume() const noexcept  
DCB 99 - 48 {  
100 - if (token_.stop_requested())  
DCB 101 - 48 return {make_error_code(std::errc::operation_canceled), {}};  
DCB 102 - 2 return {ec_, std::move(value_)};  
DCB 103 - 46 }  
104 -  
105 - auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)  
DCB 106 - 50 -> std::coroutine_handle<>  
107 - {  
108 - token_ = env->stop_token;  
DCB 109 - 50 return static_cast<Derived const*>(this)->dispatch(  
DCB 110 - 50 h, env->executor);  
DCB 111 - 50 }  
112 - };  
113 -  
114   68  
115   /* CRTP base for awaitables that return io_result<>. 69   /* CRTP base for awaitables that return io_result<>.
116   70  
117   Derived classes must provide: 71   Derived classes must provide:
118   72  
119   std::coroutine_handle<> dispatch( 73   std::coroutine_handle<> dispatch(
120   std::coroutine_handle<> h, 74   std::coroutine_handle<> h,
121   capy::executor_ref ex) const; 75   capy::executor_ref ex) const;
122   76  
123   which forwards to the backend implementation method, passing 77   which forwards to the backend implementation method, passing
124   token_ and &ec_ as the cancellation/output parameters. 78   token_ and &ec_ as the cancellation/output parameters.
125   */ 79   */
126   template<class Derived> 80   template<class Derived>
127   class void_op_base 81   class void_op_base
128   { 82   {
129   friend Derived; 83   friend Derived;
HITCBC 130   6646 void_op_base() = default; 84   6576 void_op_base() = default;
131   85  
132   public: 86   public:
133   std::stop_token token_; 87   std::stop_token token_;
134   mutable std::error_code ec_; 88   mutable std::error_code ec_;
135   89  
HITCBC 136   6646 bool await_ready() const noexcept 90   6576 bool await_ready() const noexcept
137   { 91   {
138   // A pre-set ec_ means the initiator failed before dispatch 92   // A pre-set ec_ means the initiator failed before dispatch
139   // (e.g. auto-open); complete immediately with that error. 93   // (e.g. auto-open); complete immediately with that error.
HITCBC 140   6646 return static_cast<bool>(ec_) || token_.stop_requested(); 94   6576 return static_cast<bool>(ec_) || token_.stop_requested();
141   } 95   }
142   96  
HITCBC 143   6633 [[nodiscard]] capy::io_result<> await_resume() const noexcept 97   6563 [[nodiscard]] capy::io_result<> await_resume() const noexcept
144   { 98   {
HITCBC 145   6633 if (token_.stop_requested()) 99   6563 if (token_.stop_requested())
HITCBC 146   32 return {make_error_code(std::errc::operation_canceled)}; 100   32 return {make_error_code(std::errc::operation_canceled)};
HITCBC 147   6601 return {ec_}; 101   6531 return {ec_};
148   } 102   }
149   103  
HITCBC 150   6642 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 104   6572 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
151   -> std::coroutine_handle<> 105   -> std::coroutine_handle<>
152   { 106   {
HITCBC 153   6642 token_ = env->stop_token; 107   6572 token_ = env->stop_token;
HITCBC 154   6642 return static_cast<Derived const*>(this)->dispatch( 108   6572 return static_cast<Derived const*>(this)->dispatch(
HITCBC 155   6642 h, env->executor); 109   6572 h, env->executor);
156   } 110   }
157   }; 111   };
158   112  
159   } // namespace boost::corosio::detail 113   } // namespace boost::corosio::detail
160   114  
161   #endif // BOOST_COROSIO_DETAIL_OP_BASE_HPP 115   #endif // BOOST_COROSIO_DETAIL_OP_BASE_HPP