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