source: mainline/uspace/lib/cpp/include/impl/system_error.hpp@ 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: 10.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#ifndef LIBCPP_SYSTEM_ERROR
30#define LIBCPP_SYSTEM_ERROR
31
32#include <internal/aux.hpp>
33#include <internal/stringfwd.hpp>
34#include <stdexcept>
35
36namespace std
37{
38 class error_condition;
39 class error_code;
40
41 enum class errc
42 { // TODO: add matching values
43 address_family_not_supported,
44 address_in_use,
45 address_not_available,
46 already_connected,
47 argument_list_too_long,
48 argument_out_of_domain,
49 bad_address,
50 bad_file_descriptor,
51 bad_message,
52 broken_pipe,
53 connection_aborted,
54 connection_already_in_progress,
55 connection_refused,
56 connection_reset,
57 cross_device_link,
58 destination_address_required,
59 device_or_resource_busy,
60 directory_not_empty,
61 executable_format_error,
62 file_exists,
63 file_too_large,
64 filename_too_long,
65 function_not_supported,
66 host_unreachable,
67 identifier_removed,
68 illegal_byte_sequence,
69 inappropriate_io_control_operation,
70 interrupted,
71 invalid_argument,
72 invalid_seek,
73 io_error,
74 is_a_directory,
75 message_size,
76 network_down,
77 network_reset,
78 network_unreachable,
79 no_buffer_space,
80 no_child_process,
81 no_link,
82 no_lock_available,
83 no_message_available,
84 no_message,
85 no_protocol_option,
86 no_space_on_device,
87 no_stream_resources,
88 no_such_device_or_address,
89 no_such_device,
90 no_such_file_or_directory,
91 no_such_process,
92 not_a_directory,
93 not_a_socket,
94 not_a_stream,
95 not_connected,
96 not_enough_memory,
97 not_supported,
98 operation_canceled,
99 operation_in_progress,
100 operation_not_permitted,
101 operation_not_supported,
102 operation_would_block,
103 owner_dead,
104 permission_denied,
105 protocol_error,
106 protocol_not_supported,
107 read_only_file_system,
108 resource_deadlock_would_occur,
109 resource_unavailable_try_again,
110 result_out_of_range,
111 state_not_recoverable,
112 stream_timeout,
113 text_file_busy,
114 timed_out,
115 too_many_files_open_in_system,
116 too_many_files_open,
117 too_many_links,
118 too_many_symbolic_link_levels,
119 value_too_large,
120 wrong_protocol_type
121 };
122
123 template<class>
124 struct is_error_code_enum: false_type
125 { /* DUMMY BODY */ };
126
127 template<>
128 struct is_error_code_enum<errc>: true_type
129 { /* DUMMY BODY */ };
130
131 template<class T>
132 inline constexpr bool is_error_code_enum_v = is_error_code_enum<T>::value;
133
134 template<class>
135 struct is_error_condition_enum: false_type
136 { /* DUMMY BODY */ };
137
138 template<>
139 struct is_error_condition_enum<errc>: true_type
140 { /* DUMMY BODY */ };
141
142 template<class T>
143 inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<T>::value;
144
145 /**
146 * 19.5.1, class error_category:
147 */
148
149 class error_category
150 {
151 public:
152 constexpr error_category() noexcept = default;
153 virtual ~error_category();
154
155 error_category(const error_category&) = delete;
156 error_category& operator=(const error_category&) = delete;
157
158 virtual const char* name() const noexcept = 0;
159 virtual error_condition default_error_condition(int) const noexcept;
160 virtual bool equivalent(int, const error_condition&) const noexcept;
161 virtual bool equivalent(const error_code&, int) const noexcept;
162 virtual string message(int) const = 0;
163
164 bool operator==(const error_category&) const noexcept;
165 bool operator!=(const error_category&) const noexcept;
166 bool operator<(const error_category&) const noexcept;
167 };
168
169 const error_category& generic_category() noexcept;
170 const error_category& system_category() noexcept;
171
172 /**
173 * 19.5.2, class error_code:
174 */
175
176 class error_code
177 {
178 public:
179 /**
180 * 19.5.2.2, constructors:
181 */
182
183 error_code() noexcept;
184 error_code(int, const error_category&) noexcept;
185
186 template<class ErrorCodeEnum>
187 error_code(
188 enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
189 ) noexcept
190 {
191 val_ = static_cast<int>(e);
192 cat_ = &generic_category();
193 }
194
195 /**
196 * 19.5.2.3, modifiers:
197 */
198
199 void assign(int, const error_category&) noexcept;
200
201 template<class ErrorCodeEnum>
202 error_code& operator=(
203 enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
204 ) noexcept
205 {
206 val_ = static_cast<int>(e);
207 cat_ = &generic_category();
208
209 return *this;
210 }
211
212 void clear() noexcept;
213
214 /**
215 * 19.5.2.4, observers:
216 */
217
218 int value() const noexcept;
219 const error_category& category() const noexcept;
220 error_condition default_error_condition() const noexcept;
221 string message() const;
222
223 explicit operator bool() const noexcept
224 {
225 return val_ != 0;
226 }
227
228 private:
229 int val_;
230 const error_category* cat_;
231 };
232
233 /**
234 * 19.5.2.5, non-member functions:
235 */
236
237 error_code make_error_code(errc e) noexcept;
238 bool operator<(const error_code&, const error_code&) noexcept;
239
240 template<class Char, class Traits>
241 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os,
242 const error_code& ec)
243 {
244 return os << ec.category().name() << ": " << ec.value();
245 }
246
247 /**
248 * 19.5.3, class error_condition:
249 */
250
251 class error_condition
252 {
253 public:
254 /**
255 * 19.5.3.2, constructors:
256 */
257
258 error_condition() noexcept;
259 error_condition(int, const error_category&) noexcept;
260
261 template<class ErrorCodeEnum>
262 error_condition(
263 enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
264 ) noexcept
265 {
266 val_ = static_cast<int>(e);
267 cat_ = &generic_category();
268 }
269
270 /**
271 * 19.5.3.3, modifiers:
272 */
273
274 void assign(int, const error_category&) noexcept;
275
276 template<class ErrorCodeEnum>
277 error_condition& operator=(
278 enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
279 ) noexcept
280 {
281 val_ = static_cast<int>(e);
282 cat_ = &generic_category();
283
284 return *this;
285 }
286
287 void clear() noexcept;
288
289 /**
290 * 19.5.3.4, observers:
291 */
292
293 int value() const noexcept;
294 const error_category& category() const noexcept;
295 string message() const;
296
297 explicit operator bool() const noexcept
298 {
299 return val_ != 0;
300 }
301
302 private:
303 int val_;
304 const error_category* cat_;
305 };
306
307 /**
308 * 19.5.3.4, non-member functions:
309 */
310
311 error_condition make_error_condition(errc e) noexcept;
312 bool operator<(const error_condition&, const error_condition&) noexcept;
313
314 /**
315 * 19.5.4, comparison operators:
316 */
317
318 bool operator==(const error_code&, const error_code&) noexcept;
319 bool operator==(const error_code&, const error_condition&) noexcept;
320 bool operator==(const error_condition&, const error_code&) noexcept;
321 bool operator==(const error_condition&, const error_condition&) noexcept;
322 bool operator!=(const error_code&, const error_code&) noexcept;
323 bool operator!=(const error_code&, const error_condition&) noexcept;
324 bool operator!=(const error_condition&, const error_code&) noexcept;
325 bool operator!=(const error_condition&, const error_condition&) noexcept;
326
327 /**
328 * 19.5.6, class system_error:
329 */
330
331 class system_error: public runtime_error
332 {
333 public:
334 system_error(error_code, const string&);
335 system_error(error_code, const char*);
336 system_error(error_code);
337 system_error(int, const error_category&, const string&);
338 system_error(int, const error_category&, const char*);
339 system_error(int, const error_category&);
340
341 const error_code& code() const noexcept;
342
343 private:
344 error_code code_;
345 };
346
347 /**
348 * 19.5.5, hash support:
349 */
350
351 template<class>
352 struct hash;
353
354 template<>
355 struct hash<error_code>
356 {
357 size_t operator()(const error_code& ec) const noexcept
358 {
359 return static_cast<size_t>(ec.value());
360 }
361
362 using result_type = size_t;
363 using argument_type = error_code;
364 };
365}
366
367#endif
Note: See TracBrowser for help on using the repository browser.