source: mainline/uspace/lib/cpp/src/system_error.cpp@ a1c35cc

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

cpp: added system_error

  • Property mode set to 100644
File size: 8.3 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 <functional>
30#include <string>
31#include <system_error>
32
33namespace std
34{
35 error_category::~error_category()
36 { /* DUMMY BODY */ }
37
38 error_condition error_category::default_error_condition(int code) const noexcept
39 {
40 return error_condition{code, *this};
41 }
42
43 bool error_category::equivalent(int code, const error_condition& cond) const noexcept
44 {
45 return default_error_condition(code) == cond;
46 }
47
48 bool error_category::equivalent(const error_code& ec, int cond) const noexcept
49 {
50 return *this == ec.category() && ec.value() == cond;
51 }
52
53 bool error_category::operator==(const error_category& rhs) const noexcept
54 {
55 return this == &rhs;
56 }
57
58 bool error_category::operator!=(const error_category& rhs) const noexcept
59 {
60 return !(*this == rhs);
61 }
62
63 bool error_category::operator<(const error_category& rhs) const noexcept
64 {
65 return less<const error_category*>{}(this, &rhs);
66 }
67
68 namespace aux
69 {
70 class generic_category_t: public error_category
71 {
72 public:
73 generic_category_t()
74 : error_category{}
75 { /* DUMMY BODY */ }
76
77 ~generic_category_t() = default;
78
79 const char* name() const noexcept override
80 {
81 return "generic";
82 }
83
84 string message(int ev) const override
85 {
86 return "ev: " + std::to_string(ev);
87 }
88 };
89
90 class system_category_t: public error_category
91 {
92 public:
93 system_category_t()
94 : error_category{}
95 { /* DUMMY BODY */ }
96
97 ~system_category_t() = default;
98
99 const char* name() const noexcept override
100 {
101 return "system";
102 }
103
104 string message(int ev) const override
105 {
106 return "ev: " + std::to_string(ev);
107 }
108 };
109 }
110
111 const error_category& generic_category() noexcept
112 {
113 static aux::generic_category_t instance{};
114
115 return instance;
116 }
117
118 const error_category& system_category() noexcept
119 {
120 static aux::system_category_t instance{};
121
122 return instance;
123 }
124
125 error_code::error_code() noexcept
126 : val_{}, cat_{&system_category()}
127 { /* DUMMY BODY */ }
128
129 error_code::error_code(int val, const error_category& cat) noexcept
130 : val_{val}, cat_{&cat}
131 { /* DUMMY BODY */ }
132
133 void error_code::assign(int val, const error_category& cat) noexcept
134 {
135 val_ = val;
136 cat_ = &cat;
137 }
138
139 void error_code::clear() noexcept
140 {
141 val_ = 0;
142 cat_ = &system_category();
143 }
144
145 int error_code::value() const noexcept
146 {
147 return val_;
148 }
149
150 const error_category& error_code::category() const noexcept
151 {
152 return *cat_;
153 }
154
155 error_condition error_code::default_error_condition() const noexcept
156 {
157 return cat_->default_error_condition(val_);
158 }
159
160 string error_code::message() const
161 {
162 return cat_->message(val_);
163 }
164
165 error_code make_error_code(errc e) noexcept
166 {
167 return error_code{static_cast<int>(e), generic_category()};
168 }
169
170 bool operator<(const error_code& lhs, const error_code& rhs) noexcept
171 {
172 return lhs.category() < rhs.category() ||
173 (lhs.category() == rhs.category() && lhs.value() < rhs.value());
174 }
175
176 error_condition::error_condition() noexcept
177 : val_{}, cat_{&generic_category()}
178 { /* DUMMY BODY */ }
179
180 error_condition::error_condition(int val, const error_category& cat) noexcept
181 : val_{val}, cat_{&cat}
182 { /* DUMMY BODY */ }
183
184 void error_condition::assign(int val, const error_category& cat) noexcept
185 {
186 val_ = val;
187 cat_ = &cat;
188 }
189
190 void error_condition::clear() noexcept
191 {
192 val_ = 0;
193 cat_ = &generic_category();
194 }
195
196 int error_condition::value() const noexcept
197 {
198 return val_;
199 }
200
201 const error_category& error_condition::category() const noexcept
202 {
203 return *cat_;
204 }
205
206 string error_condition::message() const
207 {
208 return cat_->message(val_);
209 }
210
211 error_condition make_error_condition(errc e) noexcept
212 {
213 return error_condition{static_cast<int>(e), generic_category()};
214 }
215
216 bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept
217 {
218 return lhs.category() < rhs.category() ||
219 (lhs.category() == rhs.category() && lhs.value() < rhs.value());
220 }
221
222 bool operator==(const error_code& lhs, const error_code& rhs) noexcept
223 {
224 return lhs.category() == rhs.category() && lhs.value() == rhs.value();
225 }
226
227 bool operator==(const error_code& lhs, const error_condition& rhs) noexcept
228 {
229 return lhs.category().equivalent(lhs.value(), rhs)
230 || rhs.category().equivalent(lhs, rhs.value());
231 }
232
233 bool operator==(const error_condition& lhs, const error_code& rhs) noexcept
234 {
235 return rhs.category().equivalent(rhs.value(), lhs)
236 || lhs.category().equivalent(rhs, lhs.value());
237 }
238
239 bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept
240 {
241 return lhs.category() == rhs.category() && lhs.value() == rhs.value();
242 }
243
244 bool operator!=(const error_code& lhs, const error_code& rhs) noexcept
245 {
246 return !(lhs == rhs);
247 }
248
249 bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept
250 {
251 return !(lhs == rhs);
252 }
253
254 bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept
255 {
256 return !(lhs == rhs);
257 }
258
259 bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept
260 {
261 return !(lhs == rhs);
262 }
263
264 system_error::system_error(error_code ec, const string& what_arg)
265 : code_{ec}, what_{what_arg}
266 { /* DUMMY BODY */ }
267
268 system_error::system_error(error_code ec, const char* what_arg)
269 : code_{ec}, what_{what_arg}
270 { /* DUMMY BODY */ }
271
272 system_error::system_error(error_code ec)
273 : code_{ec}, what_{}
274 { /* DUMMY BODY */ }
275
276 system_error::system_error(int code, const error_category& cat,
277 const string& what_arg)
278 : code_{code, cat}, what_{what_arg}
279 { /* DUMMY BODY */ }
280
281 system_error::system_error(int code, const error_category& cat,
282 const char* what_arg)
283 : code_{code, cat}, what_{what_arg}
284 { /* DUMMY BODY */ }
285
286 system_error::system_error(int code, const error_category& cat)
287 : code_{code, cat}, what_{}
288 { /* DUMMY BODY */ }
289
290 const error_code& system_error::code() const noexcept
291 {
292 return code_;
293 }
294
295 const char* system_error::what() const noexcept
296 {
297 return what_.c_str();
298 }
299}
Note: See TracBrowser for help on using the repository browser.