source: mainline/uspace/lib/cpp/src/system_error.cpp@ 4fba7ad

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4fba7ad was 4fba7ad, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: moved system_error what logic to runtime_error

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <cstring>
30#include <functional>
31#include <string>
32#include <system_error>
33
34namespace std
35{
36 error_category::~error_category()
37 { /* DUMMY BODY */ }
38
39 error_condition error_category::default_error_condition(int code) const noexcept
40 {
41 return error_condition{code, *this};
42 }
43
44 bool error_category::equivalent(int code, const error_condition& cond) const noexcept
45 {
46 return default_error_condition(code) == cond;
47 }
48
49 bool error_category::equivalent(const error_code& ec, int cond) const noexcept
50 {
51 return *this == ec.category() && ec.value() == cond;
52 }
53
54 bool error_category::operator==(const error_category& rhs) const noexcept
55 {
56 return this == &rhs;
57 }
58
59 bool error_category::operator!=(const error_category& rhs) const noexcept
60 {
61 return !(*this == rhs);
62 }
63
64 bool error_category::operator<(const error_category& rhs) const noexcept
65 {
66 return less<const error_category*>{}(this, &rhs);
67 }
68
69 namespace aux
70 {
71 class generic_category_t: public error_category
72 {
73 public:
74 generic_category_t()
75 : error_category{}
76 { /* DUMMY BODY */ }
77
78 ~generic_category_t() = default;
79
80 const char* name() const noexcept override
81 {
82 return "generic";
83 }
84
85 string message(int ev) const override
86 {
87 return "ev: " + std::to_string(ev);
88 }
89 };
90
91 class system_category_t: public error_category
92 {
93 public:
94 system_category_t()
95 : error_category{}
96 { /* DUMMY BODY */ }
97
98 ~system_category_t() = default;
99
100 const char* name() const noexcept override
101 {
102 return "system";
103 }
104
105 string message(int ev) const override
106 {
107 return "ev: " + std::to_string(ev);
108 }
109 };
110 }
111
112 const error_category& generic_category() noexcept
113 {
114 static aux::generic_category_t instance{};
115
116 return instance;
117 }
118
119 const error_category& system_category() noexcept
120 {
121 static aux::system_category_t instance{};
122
123 return instance;
124 }
125
126 error_code::error_code() noexcept
127 : val_{}, cat_{&system_category()}
128 { /* DUMMY BODY */ }
129
130 error_code::error_code(int val, const error_category& cat) noexcept
131 : val_{val}, cat_{&cat}
132 { /* DUMMY BODY */ }
133
134 void error_code::assign(int val, const error_category& cat) noexcept
135 {
136 val_ = val;
137 cat_ = &cat;
138 }
139
140 void error_code::clear() noexcept
141 {
142 val_ = 0;
143 cat_ = &system_category();
144 }
145
146 int error_code::value() const noexcept
147 {
148 return val_;
149 }
150
151 const error_category& error_code::category() const noexcept
152 {
153 return *cat_;
154 }
155
156 error_condition error_code::default_error_condition() const noexcept
157 {
158 return cat_->default_error_condition(val_);
159 }
160
161 string error_code::message() const
162 {
163 return cat_->message(val_);
164 }
165
166 error_code make_error_code(errc e) noexcept
167 {
168 return error_code{static_cast<int>(e), generic_category()};
169 }
170
171 bool operator<(const error_code& lhs, const error_code& rhs) noexcept
172 {
173 return lhs.category() < rhs.category() ||
174 (lhs.category() == rhs.category() && lhs.value() < rhs.value());
175 }
176
177 error_condition::error_condition() noexcept
178 : val_{}, cat_{&generic_category()}
179 { /* DUMMY BODY */ }
180
181 error_condition::error_condition(int val, const error_category& cat) noexcept
182 : val_{val}, cat_{&cat}
183 { /* DUMMY BODY */ }
184
185 void error_condition::assign(int val, const error_category& cat) noexcept
186 {
187 val_ = val;
188 cat_ = &cat;
189 }
190
191 void error_condition::clear() noexcept
192 {
193 val_ = 0;
194 cat_ = &generic_category();
195 }
196
197 int error_condition::value() const noexcept
198 {
199 return val_;
200 }
201
202 const error_category& error_condition::category() const noexcept
203 {
204 return *cat_;
205 }
206
207 string error_condition::message() const
208 {
209 return cat_->message(val_);
210 }
211
212 error_condition make_error_condition(errc e) noexcept
213 {
214 return error_condition{static_cast<int>(e), generic_category()};
215 }
216
217 bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept
218 {
219 return lhs.category() < rhs.category() ||
220 (lhs.category() == rhs.category() && lhs.value() < rhs.value());
221 }
222
223 bool operator==(const error_code& lhs, const error_code& rhs) noexcept
224 {
225 return lhs.category() == rhs.category() && lhs.value() == rhs.value();
226 }
227
228 bool operator==(const error_code& lhs, const error_condition& rhs) noexcept
229 {
230 return lhs.category().equivalent(lhs.value(), rhs)
231 || rhs.category().equivalent(lhs, rhs.value());
232 }
233
234 bool operator==(const error_condition& lhs, const error_code& rhs) noexcept
235 {
236 return rhs.category().equivalent(rhs.value(), lhs)
237 || lhs.category().equivalent(rhs, lhs.value());
238 }
239
240 bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept
241 {
242 return lhs.category() == rhs.category() && lhs.value() == rhs.value();
243 }
244
245 bool operator!=(const error_code& lhs, const error_code& rhs) noexcept
246 {
247 return !(lhs == rhs);
248 }
249
250 bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept
251 {
252 return !(lhs == rhs);
253 }
254
255 bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept
256 {
257 return !(lhs == rhs);
258 }
259
260 bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept
261 {
262 return !(lhs == rhs);
263 }
264
265 system_error::system_error(error_code ec, const string& what_arg)
266 : runtime_error{what_arg.c_str()}, code_{ec}
267 { /* DUMMY BODY */ }
268
269 system_error::system_error(error_code ec, const char* what_arg)
270 : runtime_error{what_arg}, code_{ec}
271 { /* DUMMY BODY */ }
272
273 system_error::system_error(error_code ec)
274 : runtime_error{"system_error"}, code_{ec}
275 { /* DUMMY BODY */ }
276
277 system_error::system_error(int code, const error_category& cat,
278 const string& what_arg)
279 : runtime_error{what_arg.c_str()}, code_{code, cat}
280 { /* DUMMY BODY */ }
281
282 system_error::system_error(int code, const error_category& cat,
283 const char* what_arg)
284 : runtime_error{what_arg}, code_{code, cat}
285 { /* DUMMY BODY */ }
286
287 system_error::system_error(int code, const error_category& cat)
288 : runtime_error{"system_error"}, code_{code, cat}
289 { /* DUMMY BODY */ }
290
291 const error_code& system_error::code() const noexcept
292 {
293 return code_;
294 }
295}
Note: See TracBrowser for help on using the repository browser.