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