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   @par !example example 29   @par !example example
30   30  
31   31  
32   @see error 32   @see error
33   */ 33   */
34   enum class cond 34   enum class cond
35   { 35   {
36   /** End-of-stream condition. 36   /** End-of-stream condition.
37   37  
38   An `error_code` compares equal to `eof` when the stream 38   An `error_code` compares equal to `eof` when the stream
39   reached its natural end. Examples are a peer sending TCP 39   reached its natural end. Examples are a peer sending TCP
40   FIN, or a file reaching EOF. 40   FIN, or a file reaching EOF.
41   */ 41   */
42   eof = 1, 42   eof = 1,
43   43  
44   /** Operation cancelled condition. 44   /** Operation cancelled condition.
45   45  
46   An `error_code` compares equal to `canceled` when the 46   An `error_code` compares equal to `canceled` when the
47   operation's stop token was activated, the I/O object's 47   operation's stop token was activated, the I/O object's
48   `cancel()` was called, or a platform cancellation error 48   `cancel()` was called, or a platform cancellation error
49   occurred. 49   occurred.
50   */ 50   */
51   canceled = 2, 51   canceled = 2,
52   52  
53   /** Stream truncated condition. 53   /** Stream truncated condition.
54   54  
55   An `error_code` compares equal to `stream_truncated` when 55   An `error_code` compares equal to `stream_truncated` when
56   a TLS peer closed the connection without sending a proper 56   a TLS peer closed the connection without sending a proper
57   shutdown alert. 57   shutdown alert.
58   */ 58   */
59   stream_truncated = 3, 59   stream_truncated = 3,
60   60  
61   /** Operation timed out condition. 61   /** Operation timed out condition.
62   62  
63   An `error_code` compares equal to `timeout` when an 63   An `error_code` compares equal to `timeout` when an
64   operation exceeded its allowed duration. 64   operation exceeded its allowed duration.
65   */ 65   */
66   timeout = 4 66   timeout = 4
67   }; 67   };
68   68  
69   } // capy 69   } // capy
70   } // boost 70   } // boost
71   71  
72   namespace std { 72   namespace std {
73   template<> 73   template<>
74   struct is_error_condition_enum< 74   struct is_error_condition_enum<
75   ::boost::capy::cond> 75   ::boost::capy::cond>
76   : std::true_type {}; 76   : std::true_type {};
77   } // std 77   } // std
78   78  
79   namespace boost { 79   namespace boost {
80   namespace capy { 80   namespace capy {
81   81  
82   namespace detail { 82   namespace detail {
83   83  
84   struct BOOST_CAPY_SYMBOL_VISIBLE 84   struct BOOST_CAPY_SYMBOL_VISIBLE
85   cond_cat_type 85   cond_cat_type
86   : std::error_category 86   : std::error_category
87   { 87   {
88   BOOST_CAPY_DECL const char* name( 88   BOOST_CAPY_DECL const char* name(
89   ) const noexcept override; 89   ) const noexcept override;
90   BOOST_CAPY_DECL std::string message( 90   BOOST_CAPY_DECL std::string message(
91   int) const override; 91   int) const override;
92   BOOST_CAPY_DECL bool equivalent( 92   BOOST_CAPY_DECL bool equivalent(
93   std::error_code const& ec, 93   std::error_code const& ec,
94   int condition) const noexcept override; 94   int condition) const noexcept override;
95   constexpr cond_cat_type() noexcept = default; 95   constexpr cond_cat_type() noexcept = default;
96   }; 96   };
97   97  
98   BOOST_CAPY_DECL extern cond_cat_type cond_cat; 98   BOOST_CAPY_DECL extern cond_cat_type cond_cat;
99   99  
100   } // detail 100   } // detail
101   101  
102   /** Create an error_condition from a cond value. 102   /** Create an error_condition from a cond value.
103   103  
104   @param ev The library condition value. 104   @param ev The library condition value.
105   105  
106   @return An `std::error_condition` holding `ev` in the library's 106   @return An `std::error_condition` holding `ev` in the library's
107   condition category. 107   condition category.
108   */ 108   */
109   inline 109   inline
110   std::error_condition 110   std::error_condition
HITCBC 111   227 make_error_condition( 111   227 make_error_condition(
112   cond ev) noexcept 112   cond ev) noexcept
113   { 113   {
HITCBC 114   227 return std::error_condition{ 114   227 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   227 detail::cond_cat}; 117   227 detail::cond_cat};
118   } 118   }
119   119  
120   } // capy 120   } // capy
121   } // boost 121   } // boost
122   122  
123   #endif 123   #endif