include/boost/url/grammar/impl/error.hpp

100.0% Lines (36 / 36) 100.0% Functions (11 / 11)
error.hpp
f(x) Functions (11)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
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/boostorg/url
9 //
10
11 #ifndef BOOST_URL_GRAMMAR_IMPL_ERROR_HPP
12 #define BOOST_URL_GRAMMAR_IMPL_ERROR_HPP
13
14 #include <type_traits>
15
16 namespace boost {
17 namespace system {
18 template<>
19 struct is_error_code_enum<
20 ::boost::urls::grammar::error>
21 {
22 static bool const value = true;
23 };
24 template<>
25 struct is_error_condition_enum<
26 ::boost::urls::grammar::condition>
27 {
28 static bool const value = true;
29 };
30 } // system
31 } // boost
32
33 namespace boost {
34 namespace urls {
35 namespace grammar {
36
37 namespace detail {
38
39 struct BOOST_SYMBOL_VISIBLE
40 error_cat_type
41 : system::error_category
42 {
43 BOOST_URL_CXX20_CONSTEXPR
44 616x const char* name(
45 ) const noexcept override
46 {
47 616x return "boost.url.grammar";
48 }
49
50 616x std::string message(
51 int code) const override
52 {
53 1232x return message(code, nullptr, 0);
54 }
55
56 BOOST_URL_CXX20_CONSTEXPR
57 616x char const* message(
58 int code,
59 char*,
60 std::size_t) const noexcept override
61 {
62 616x switch(static_cast<error>(code))
63 {
64 1x default:
65 1x case error::need_more: return "need more";
66 63x case error::mismatch: return "mismatch";
67 13x case error::invalid: return "invalid";
68 1x case error::end_of_range: return "end of range";
69 537x case error::leftover: return "leftover";
70 1x case error::out_of_range: return "out of range";
71 }
72 }
73
74 BOOST_URL_CXX20_CONSTEXPR
75 system::error_condition
76 default_error_condition(
77 int ev) const noexcept override;
78
79 78x BOOST_SYSTEM_CONSTEXPR error_cat_type() noexcept
80 78x : error_category(0x0536e50a30f9e9f2)
81 {
82 78x }
83 };
84
85 struct BOOST_SYMBOL_VISIBLE
86 condition_cat_type
87 : system::error_category
88 {
89 BOOST_URL_CXX20_CONSTEXPR
90 5x const char* name(
91 ) const noexcept override
92 {
93 5x return "boost.url.grammar";
94 }
95
96 5x std::string message(
97 int code) const override
98 {
99 10x return message(code, nullptr, 0);
100 }
101
102 BOOST_URL_CXX20_CONSTEXPR
103 5x char const* message(
104 int code,
105 char*,
106 std::size_t) const noexcept override
107 {
108 switch(static_cast<condition>(code))
109 {
110 default:
111 case condition::fatal:
112 5x return "fatal condition";
113 }
114 }
115
116 78x BOOST_SYSTEM_CONSTEXPR condition_cat_type()
117 78x : error_category(0x0536e50a30f9e9f2)
118 {
119 78x }
120 };
121
122 #if defined(BOOST_URL_HAS_CXX20_CONSTEXPR)
123 // Visible so a shared library built as C++20
124 // still exports the symbols that consumers built
125 // as C++17 reference through the externs below
126 BOOST_SYMBOL_VISIBLE
127 inline constexpr error_cat_type error_cat{};
128 BOOST_SYMBOL_VISIBLE
129 inline constexpr condition_cat_type condition_cat{};
130 #else
131 BOOST_URL_DECL extern error_cat_type error_cat;
132 BOOST_URL_DECL extern condition_cat_type condition_cat;
133 #endif
134
135 } // detail
136
137 inline
138 BOOST_SYSTEM_CONSTEXPR
139 system::error_code
140 53401x make_error_code(
141 error ev) noexcept
142 {
143 return system::error_code{
144 static_cast<std::underlying_type<
145 error>::type>(ev),
146 53401x detail::error_cat};
147 }
148
149 inline
150 BOOST_SYSTEM_CONSTEXPR
151 system::error_condition
152 20x make_error_condition(
153 condition c) noexcept
154 {
155 20x return system::error_condition{
156 static_cast<std::underlying_type<
157 condition>::type>(c),
158 20x detail::condition_cat};
159 }
160
161 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
162 system::error_condition
163 6x detail::error_cat_type::
164 default_error_condition(
165 int ev) const noexcept
166 {
167 6x switch(static_cast<error>(ev))
168 {
169 2x case error::invalid:
170 case error::out_of_range:
171 2x return condition::fatal;
172 4x default:
173 4x return {ev, *this};
174 }
175 }
176
177 } // grammar
178 } // urls
179 } // boost
180
181 #endif
182