Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
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 : #include <boost/capy/cond.hpp>
11 : #include <boost/capy/error.hpp>
12 : #include <boost/system/errc.hpp>
13 : #include <system_error>
14 :
15 : namespace boost {
16 : namespace capy {
17 :
18 : namespace detail {
19 :
20 : const char*
21 1 : cond_cat_type::
22 : name() const noexcept
23 : {
24 1 : return "boost.capy";
25 : }
26 :
27 : std::string
28 2 : cond_cat_type::
29 : message(int code) const
30 : {
31 4 : return message(code, nullptr, 0);
32 : }
33 :
34 : char const*
35 2 : cond_cat_type::
36 : message(
37 : int code,
38 : char*,
39 : std::size_t) const noexcept
40 : {
41 2 : switch(static_cast<cond>(code))
42 : {
43 1 : case cond::eof: return "end of file";
44 1 : case cond::canceled: return "operation canceled";
45 0 : case cond::stream_truncated: return "stream truncated";
46 0 : default:
47 0 : return "unknown";
48 : }
49 : }
50 :
51 : bool
52 8 : cond_cat_type::
53 : equivalent(
54 : system::error_code const& ec,
55 : int condition) const noexcept
56 : {
57 8 : switch(static_cast<cond>(condition))
58 : {
59 4 : case cond::eof:
60 4 : return ec == capy::error::eof;
61 :
62 4 : case cond::canceled:
63 : // Check capy::error::canceled
64 4 : if(ec == capy::error::canceled)
65 0 : return true;
66 : // Check boost::system::errc
67 4 : if(ec == boost::system::errc::operation_canceled)
68 2 : return true;
69 : // Check std::errc
70 2 : if(ec == std::errc::operation_canceled)
71 0 : return true;
72 2 : return false;
73 :
74 0 : case cond::stream_truncated:
75 0 : return ec == capy::error::stream_truncated;
76 :
77 0 : default:
78 0 : return false;
79 : }
80 : }
81 :
82 : //-----------------------------------------------
83 :
84 : // msvc 14.0 has a bug that warns about inability
85 : // to use constexpr construction here, even though
86 : // there's no constexpr construction
87 : #if defined(_MSC_VER) && _MSC_VER <= 1900
88 : # pragma warning( push )
89 : # pragma warning( disable : 4592 )
90 : #endif
91 :
92 : #if defined(__cpp_constinit) && __cpp_constinit >= 201907L
93 : constinit cond_cat_type cond_cat;
94 : #else
95 : cond_cat_type cond_cat;
96 : #endif
97 :
98 : #if defined(_MSC_VER) && _MSC_VER <= 1900
99 : # pragma warning( pop )
100 : #endif
101 :
102 : } // detail
103 :
104 : } // capy
105 : } // boost
|