include/boost/capy/cond.hpp

100.0% Lines (3 / 3) 100.0% Functions (1 / 1)
cond.hpp
f(x) Functions (1)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Michael Vandeberg
4 //
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)
7 //
8 // Official repository: https://github.com/cppalliance/capy
9 //
10
11 #ifndef BOOST_CAPY_COND_HPP
12 #define BOOST_CAPY_COND_HPP
13
14 #include <boost/capy/detail/config.hpp>
15 #include <system_error>
16
17 namespace boost {
18 namespace capy {
19
20 /** Group error codes from capy operations into portable conditions.
21
22 These are the conditions callers should compare against when
23 handling errors from capy operations. Capy @ref error values map
24 to these conditions. So does any code equivalent to
25 `std::errc::operation_canceled` (for example `ECANCELED`) or
26 `std::errc::timed_out`.
27
28 @par Example
29
30 @par !example example
31
32
33 @see @ref error
34 */
35 enum class cond
36 {
37 /** Match codes that report the end of a stream.
38
39 An `error_code` compares equal to `eof` when the stream
40 reached its natural end. Examples are a peer sending TCP
41 FIN, or a file reaching EOF.
42 */
43 eof = 1,
44
45 /** Match codes that report a canceled operation.
46
47 An `error_code` compares equal to `canceled` when the
48 operation's stop token was activated, the I/O object's
49 `cancel()` was called, or a platform cancellation error
50 occurred.
51 */
52 canceled = 2,
53
54 /** Match codes that report a truncated TLS stream.
55
56 An `error_code` compares equal to `stream_truncated` when
57 a TLS peer closed the connection without sending a proper
58 shutdown alert.
59 */
60 stream_truncated = 3,
61
62 /** Match codes that report a timed-out operation.
63
64 An `error_code` compares equal to `timeout` when an
65 operation exceeded its allowed duration, or when it is
66 equivalent to `std::errc::timed_out`.
67 */
68 timeout = 4
69 };
70
71 } // capy
72 } // boost
73
74 namespace std {
75 template<>
76 struct is_error_condition_enum<
77 ::boost::capy::cond>
78 : std::true_type {};
79 } // std
80
81 namespace boost {
82 namespace capy {
83
84 namespace detail {
85
86 struct BOOST_CAPY_SYMBOL_VISIBLE
87 cond_cat_type
88 : std::error_category
89 {
90 BOOST_CAPY_DECL const char* name(
91 ) const noexcept override;
92 BOOST_CAPY_DECL std::string message(
93 int) const override;
94 BOOST_CAPY_DECL bool equivalent(
95 std::error_code const& ec,
96 int condition) const noexcept override;
97 constexpr cond_cat_type() noexcept = default;
98 };
99
100 BOOST_CAPY_DECL extern cond_cat_type cond_cat;
101
102 } // detail
103
104 /** Create an error_condition from a cond value.
105
106 @param ev The library condition value.
107
108 @return An `std::error_condition` holding `ev` in the library's
109 condition category.
110 */
111 inline
112 std::error_condition
113 799x make_error_condition(
114 cond ev) noexcept
115 {
116 799x return std::error_condition{
117 static_cast<std::underlying_type<
118 cond>::type>(ev),
119 799x detail::cond_cat};
120 }
121
122 } // capy
123 } // boost
124
125 #endif
126