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   428952 bytes_op_base() = default; 39   409247 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   428952 bool await_ready() const noexcept 46   409247 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   428952 return static_cast<bool>(ec_) || token_.stop_requested(); 50   409247 return static_cast<bool>(ec_) || token_.stop_requested();
51   } 51   }
52   52  
HITCBC 53   428940 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept 53   409214 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept
54   { 54   {
HITCBC 55   428940 if (token_.stop_requested()) 55   409214 if (token_.stop_requested())
HITCBC 56   241 return {make_error_code(std::errc::operation_canceled), 0}; 56   241 return {make_error_code(std::errc::operation_canceled), 0};
HITCBC 57   428699 return {ec_, bytes_}; 57   408973 return {ec_, bytes_};
58   } 58   }
59   59  
HITCBC 60   428936 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 60   409231 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
61   -> std::coroutine_handle<> 61   -> std::coroutine_handle<>
62   { 62   {
HITCBC 63   428936 token_ = env->stop_token; 63   409231 token_ = env->stop_token;
HITCBC 64   428936 return static_cast<Derived const*>(this)->dispatch( 64   409231 return static_cast<Derived const*>(this)->dispatch(
HITCBC 65   428936 h, env->executor); 65   409231 h, env->executor);
66   } 66   }
67   }; 67   };
68   68  
69   /* CRTP base for awaitables that return io_result<>. 69   /* CRTP base for awaitables that return io_result<>.
70   70  
71   Derived classes must provide: 71   Derived classes must provide:
72   72  
73   std::coroutine_handle<> dispatch( 73   std::coroutine_handle<> dispatch(
74   std::coroutine_handle<> h, 74   std::coroutine_handle<> h,
75   capy::executor_ref ex) const; 75   capy::executor_ref ex) const;
76   76  
77   which forwards to the backend implementation method, passing 77   which forwards to the backend implementation method, passing
78   token_ and &ec_ as the cancellation/output parameters. 78   token_ and &ec_ as the cancellation/output parameters.
79   */ 79   */
80   template<class Derived> 80   template<class Derived>
81   class void_op_base 81   class void_op_base
82   { 82   {
83   friend Derived; 83   friend Derived;
HITCBC 84   4335 void_op_base() = default; 84   5344 void_op_base() = default;
85   85  
86   public: 86   public:
87   std::stop_token token_; 87   std::stop_token token_;
88   mutable std::error_code ec_; 88   mutable std::error_code ec_;
89   89  
HITCBC 90   4335 bool await_ready() const noexcept 90   5344 bool await_ready() const noexcept
91   { 91   {
92   // A pre-set ec_ means the initiator failed before dispatch 92   // A pre-set ec_ means the initiator failed before dispatch
93   // (e.g. auto-open); complete immediately with that error. 93   // (e.g. auto-open); complete immediately with that error.
HITCBC 94   4335 return static_cast<bool>(ec_) || token_.stop_requested(); 94   5344 return static_cast<bool>(ec_) || token_.stop_requested();
95   } 95   }
96   96  
HITCBC 97   4335 [[nodiscard]] capy::io_result<> await_resume() const noexcept 97   5331 [[nodiscard]] capy::io_result<> await_resume() const noexcept
98   { 98   {
HITCBC 99   4335 if (token_.stop_requested()) 99   5331 if (token_.stop_requested())
HITCBC 100   32 return {make_error_code(std::errc::operation_canceled)}; 100   32 return {make_error_code(std::errc::operation_canceled)};
HITCBC 101   4303 return {ec_}; 101   5299 return {ec_};
102   } 102   }
103   103  
HITCBC 104   4331 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 104   5340 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
105   -> std::coroutine_handle<> 105   -> std::coroutine_handle<>
106   { 106   {
HITCBC 107   4331 token_ = env->stop_token; 107   5340 token_ = env->stop_token;
HITCBC 108   4331 return static_cast<Derived const*>(this)->dispatch( 108   5340 return static_cast<Derived const*>(this)->dispatch(
HITCBC 109   4331 h, env->executor); 109   5340 h, env->executor);
110   } 110   }
111   }; 111   };
112   112  
113   } // namespace boost::corosio::detail 113   } // namespace boost::corosio::detail
114   114  
115   #endif // BOOST_COROSIO_DETAIL_OP_BASE_HPP 115   #endif // BOOST_COROSIO_DETAIL_OP_BASE_HPP