100.00% Lines (3/3) 100.00% Functions (1/1)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // Copyright (c) 2026 Michael Vandeberg 3   // Copyright (c) 2026 Michael Vandeberg
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/cppalliance/capy 8   // Official repository: https://github.com/cppalliance/capy
9   // 9   //
10   10  
11   #ifndef BOOST_CAPY_COND_HPP 11   #ifndef BOOST_CAPY_COND_HPP
12   #define BOOST_CAPY_COND_HPP 12   #define BOOST_CAPY_COND_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <system_error> 15   #include <system_error>
16   16  
17   namespace boost { 17   namespace boost {
18   namespace capy { 18   namespace capy {
19   19  
20   /** Portable error conditions for capy I/O operations. 20   /** Portable error conditions for capy I/O operations.
21   21  
22   These are the conditions callers should compare against when 22   These are the conditions callers should compare against when
23   handling errors from capy operations. The @ref error enum values 23   handling errors from capy operations. The @ref error enum values
24   map to these conditions, as do platform-specific error codes 24   map to these conditions, as do platform-specific error codes
25   (e.g., `ECANCELED`, SSL EOF errors). 25   (e.g., `ECANCELED`, SSL EOF errors).
26   26  
27   @par Example 27   @par Example
28   28  
29 - @code 29 + @par !example example
30 - auto [ec, n] = co_await stream.read_some( bufs ); 30 +
31 - if( ec == cond::canceled )  
32 - {  
33 - // handle cancellation  
34 - }  
35 - else if( ec == cond::eof )  
36 - {  
37 - // handle end of stream  
38 - }  
39 - else if( ec )  
40 - {  
41 - // handle other errors  
42 - }  
43 - @endcode  
44   31  
45   @see error 32   @see error
46   */ 33   */
47   enum class cond 34   enum class cond
48   { 35   {
49   /** End-of-stream condition. 36   /** End-of-stream condition.
50   37  
51   An `error_code` compares equal to `eof` when the stream 38   An `error_code` compares equal to `eof` when the stream
52   reached its natural end. Examples are a peer sending TCP 39   reached its natural end. Examples are a peer sending TCP
53   FIN, or a file reaching EOF. 40   FIN, or a file reaching EOF.
54   */ 41   */
55   eof = 1, 42   eof = 1,
56   43  
57   /** Operation cancelled condition. 44   /** Operation cancelled condition.
58   45  
59   An `error_code` compares equal to `canceled` when the 46   An `error_code` compares equal to `canceled` when the
60   operation's stop token was activated, the I/O object's 47   operation's stop token was activated, the I/O object's
61   `cancel()` was called, or a platform cancellation error 48   `cancel()` was called, or a platform cancellation error
62   occurred. 49   occurred.
63   */ 50   */
64   canceled = 2, 51   canceled = 2,
65   52  
66   /** Stream truncated condition. 53   /** Stream truncated condition.
67   54  
68   An `error_code` compares equal to `stream_truncated` when 55   An `error_code` compares equal to `stream_truncated` when
69   a TLS peer closed the connection without sending a proper 56   a TLS peer closed the connection without sending a proper
70   shutdown alert. 57   shutdown alert.
71   */ 58   */
72   stream_truncated = 3, 59   stream_truncated = 3,
73   60  
74   /** Operation timed out condition. 61   /** Operation timed out condition.
75   62  
76   An `error_code` compares equal to `timeout` when an 63   An `error_code` compares equal to `timeout` when an
77   operation exceeded its allowed duration. 64   operation exceeded its allowed duration.
78   */ 65   */
79   timeout = 4 66   timeout = 4
80   }; 67   };
81   68  
82   } // capy 69   } // capy
83   } // boost 70   } // boost
84   71  
85   namespace std { 72   namespace std {
86   template<> 73   template<>
87   struct is_error_condition_enum< 74   struct is_error_condition_enum<
88   ::boost::capy::cond> 75   ::boost::capy::cond>
89   : std::true_type {}; 76   : std::true_type {};
90   } // std 77   } // std
91   78  
92   namespace boost { 79   namespace boost {
93   namespace capy { 80   namespace capy {
94   81  
95   namespace detail { 82   namespace detail {
96   83  
97   struct BOOST_CAPY_SYMBOL_VISIBLE 84   struct BOOST_CAPY_SYMBOL_VISIBLE
98   cond_cat_type 85   cond_cat_type
99   : std::error_category 86   : std::error_category
100   { 87   {
101   BOOST_CAPY_DECL const char* name( 88   BOOST_CAPY_DECL const char* name(
102   ) const noexcept override; 89   ) const noexcept override;
103   BOOST_CAPY_DECL std::string message( 90   BOOST_CAPY_DECL std::string message(
104   int) const override; 91   int) const override;
105   BOOST_CAPY_DECL bool equivalent( 92   BOOST_CAPY_DECL bool equivalent(
106   std::error_code const& ec, 93   std::error_code const& ec,
107   int condition) const noexcept override; 94   int condition) const noexcept override;
108   constexpr cond_cat_type() noexcept = default; 95   constexpr cond_cat_type() noexcept = default;
109   }; 96   };
110   97  
111   BOOST_CAPY_DECL extern cond_cat_type cond_cat; 98   BOOST_CAPY_DECL extern cond_cat_type cond_cat;
112   99  
113   } // detail 100   } // detail
114   101  
115   /** Create an error_condition from a cond value. 102   /** Create an error_condition from a cond value.
116   103  
117   @param ev The library condition value. 104   @param ev The library condition value.
118   105  
119   @return An `std::error_condition` holding `ev` in the library's 106   @return An `std::error_condition` holding `ev` in the library's
120   condition category. 107   condition category.
121   */ 108   */
122   inline 109   inline
123   std::error_condition 110   std::error_condition
HITCBC 124   227 make_error_condition( 111   227 make_error_condition(
125   cond ev) noexcept 112   cond ev) noexcept
126   { 113   {
HITCBC 127   227 return std::error_condition{ 114   227 return std::error_condition{
128   static_cast<std::underlying_type< 115   static_cast<std::underlying_type<
129   cond>::type>(ev), 116   cond>::type>(ev),
HITCBC 130   227 detail::cond_cat}; 117   227 detail::cond_cat};
131   } 118   }
132   119  
133   } // capy 120   } // capy
134   } // boost 121   } // boost
135   122  
136   #endif 123   #endif