blob: 5e1e6fe4b296089e22cf5d167b5169e61702355f [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- system_error ----------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_SYSTEM_ERROR
12#define _LIBCPP_SYSTEM_ERROR
13
14/*
15 system_error synopsis
16
17namespace std
18{
19
20class error_category
21{
22public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000023 virtual ~error_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024
25 error_category(const error_category&) = delete;
26 error_category& operator=(const error_category&) = delete;
27
Howard Hinnant1e15fd12011-05-26 19:48:01 +000028 virtual const char* name() const noexcept = 0;
29 virtual error_condition default_error_condition(int ev) const noexcept;
30 virtual bool equivalent(int code, const error_condition& condition) const noexcept;
31 virtual bool equivalent(const error_code& code, int condition) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 virtual string message(int ev) const = 0;
33
Howard Hinnant1e15fd12011-05-26 19:48:01 +000034 bool operator==(const error_category& rhs) const noexcept;
35 bool operator!=(const error_category& rhs) const noexcept;
36 bool operator<(const error_category& rhs) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037};
38
Howard Hinnant1e15fd12011-05-26 19:48:01 +000039const error_category& generic_category() noexcept;
40const error_category& system_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041
42template <class T> struct is_error_code_enum
43 : public false_type {};
44
45template <class T> struct is_error_condition_enum
46 : public false_type {};
47
48class error_code
49{
50public:
51 // constructors:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000052 error_code() noexcept;
53 error_code(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054 template <class ErrorCodeEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000055 error_code(ErrorCodeEnum e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
57 // modifiers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000058 void assign(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059 template <class ErrorCodeEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000060 error_code& operator=(ErrorCodeEnum e) noexcept;
61 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062
63 // observers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000064 int value() const noexcept;
65 const error_category& category() const noexcept;
66 error_condition default_error_condition() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067 string message() const;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000068 explicit operator bool() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71// non-member functions:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000072bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073template <class charT, class traits>
74 basic_ostream<charT,traits>&
75 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
76
77class error_condition
78{
79public:
80 // constructors:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000081 error_condition() noexcept;
82 error_condition(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 template <class ErrorConditionEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000084 error_condition(ErrorConditionEnum e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 // modifiers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000087 void assign(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088 template <class ErrorConditionEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000089 error_condition& operator=(ErrorConditionEnum e) noexcept;
90 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091
92 // observers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000093 int value() const noexcept;
94 const error_category& category() const noexcept;
95 string message() const noexcept;
96 explicit operator bool() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097};
98
Howard Hinnant1e15fd12011-05-26 19:48:01 +000099bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100
101class system_error
102 : public runtime_error
103{
104public:
105 system_error(error_code ec, const string& what_arg);
106 system_error(error_code ec, const char* what_arg);
107 system_error(error_code ec);
108 system_error(int ev, const error_category& ecat, const string& what_arg);
109 system_error(int ev, const error_category& ecat, const char* what_arg);
110 system_error(int ev, const error_category& ecat);
111
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000112 const error_code& code() const noexcept;
113 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114};
115
116enum class errc
117{
118 address_family_not_supported, // EAFNOSUPPORT
119 address_in_use, // EADDRINUSE
120 address_not_available, // EADDRNOTAVAIL
121 already_connected, // EISCONN
122 argument_list_too_long, // E2BIG
123 argument_out_of_domain, // EDOM
124 bad_address, // EFAULT
125 bad_file_descriptor, // EBADF
126 bad_message, // EBADMSG
127 broken_pipe, // EPIPE
128 connection_aborted, // ECONNABORTED
129 connection_already_in_progress, // EALREADY
130 connection_refused, // ECONNREFUSED
131 connection_reset, // ECONNRESET
132 cross_device_link, // EXDEV
133 destination_address_required, // EDESTADDRREQ
134 device_or_resource_busy, // EBUSY
135 directory_not_empty, // ENOTEMPTY
136 executable_format_error, // ENOEXEC
137 file_exists, // EEXIST
138 file_too_large, // EFBIG
139 filename_too_long, // ENAMETOOLONG
140 function_not_supported, // ENOSYS
141 host_unreachable, // EHOSTUNREACH
142 identifier_removed, // EIDRM
143 illegal_byte_sequence, // EILSEQ
144 inappropriate_io_control_operation, // ENOTTY
145 interrupted, // EINTR
146 invalid_argument, // EINVAL
147 invalid_seek, // ESPIPE
148 io_error, // EIO
149 is_a_directory, // EISDIR
150 message_size, // EMSGSIZE
151 network_down, // ENETDOWN
152 network_reset, // ENETRESET
153 network_unreachable, // ENETUNREACH
154 no_buffer_space, // ENOBUFS
155 no_child_process, // ECHILD
156 no_link, // ENOLINK
157 no_lock_available, // ENOLCK
158 no_message_available, // ENODATA
159 no_message, // ENOMSG
160 no_protocol_option, // ENOPROTOOPT
161 no_space_on_device, // ENOSPC
162 no_stream_resources, // ENOSR
163 no_such_device_or_address, // ENXIO
164 no_such_device, // ENODEV
165 no_such_file_or_directory, // ENOENT
166 no_such_process, // ESRCH
167 not_a_directory, // ENOTDIR
168 not_a_socket, // ENOTSOCK
169 not_a_stream, // ENOSTR
170 not_connected, // ENOTCONN
171 not_enough_memory, // ENOMEM
172 not_supported, // ENOTSUP
173 operation_canceled, // ECANCELED
174 operation_in_progress, // EINPROGRESS
175 operation_not_permitted, // EPERM
176 operation_not_supported, // EOPNOTSUPP
177 operation_would_block, // EWOULDBLOCK
178 owner_dead, // EOWNERDEAD
179 permission_denied, // EACCES
180 protocol_error, // EPROTO
181 protocol_not_supported, // EPROTONOSUPPORT
182 read_only_file_system, // EROFS
183 resource_deadlock_would_occur, // EDEADLK
184 resource_unavailable_try_again, // EAGAIN
185 result_out_of_range, // ERANGE
186 state_not_recoverable, // ENOTRECOVERABLE
187 stream_timeout, // ETIME
188 text_file_busy, // ETXTBSY
189 timed_out, // ETIMEDOUT
190 too_many_files_open_in_system, // ENFILE
191 too_many_files_open, // EMFILE
192 too_many_links, // EMLINK
193 too_many_symbolic_link_levels, // ELOOP
194 value_too_large, // EOVERFLOW
195 wrong_protocol_type // EPROTOTYPE
196};
197
198template <> struct is_error_condition_enum<errc>
199 : true_type { }
200
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000201error_code make_error_code(errc e) noexcept;
202error_condition make_error_condition(errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203
204// Comparison operators:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000205bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
206bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
207bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
208bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
209bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
210bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
211bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
212bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213
214template <> struct hash<std::error_code>;
215
216} // std
217
218*/
219
220#include <__config>
221#include <cerrno>
222#include <type_traits>
223#include <stdexcept>
224#include <__functional_base>
225
226#pragma GCC system_header
227
228_LIBCPP_BEGIN_NAMESPACE_STD
229
230// is_error_code_enum
231
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000232template <class _Tp>
233struct _LIBCPP_VISIBLE is_error_code_enum
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000234 : public false_type {};
235
236// is_error_condition_enum
237
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000238template <class _Tp>
239struct _LIBCPP_VISIBLE is_error_condition_enum
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240 : public false_type {};
241
David Chisnall81e68582010-08-11 16:52:41 +0000242// Some error codes are not present on all platforms, so we provide equivalents
243// for them:
244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245//enum class errc
246struct errc
247{
248enum _ {
249 address_family_not_supported = EAFNOSUPPORT,
250 address_in_use = EADDRINUSE,
251 address_not_available = EADDRNOTAVAIL,
252 already_connected = EISCONN,
253 argument_list_too_long = E2BIG,
254 argument_out_of_domain = EDOM,
255 bad_address = EFAULT,
256 bad_file_descriptor = EBADF,
257 bad_message = EBADMSG,
258 broken_pipe = EPIPE,
259 connection_aborted = ECONNABORTED,
260 connection_already_in_progress = EALREADY,
261 connection_refused = ECONNREFUSED,
262 connection_reset = ECONNRESET,
263 cross_device_link = EXDEV,
264 destination_address_required = EDESTADDRREQ,
265 device_or_resource_busy = EBUSY,
266 directory_not_empty = ENOTEMPTY,
267 executable_format_error = ENOEXEC,
268 file_exists = EEXIST,
269 file_too_large = EFBIG,
270 filename_too_long = ENAMETOOLONG,
271 function_not_supported = ENOSYS,
272 host_unreachable = EHOSTUNREACH,
273 identifier_removed = EIDRM,
274 illegal_byte_sequence = EILSEQ,
275 inappropriate_io_control_operation = ENOTTY,
276 interrupted = EINTR,
277 invalid_argument = EINVAL,
278 invalid_seek = ESPIPE,
279 io_error = EIO,
280 is_a_directory = EISDIR,
281 message_size = EMSGSIZE,
282 network_down = ENETDOWN,
283 network_reset = ENETRESET,
284 network_unreachable = ENETUNREACH,
285 no_buffer_space = ENOBUFS,
286 no_child_process = ECHILD,
287 no_link = ENOLINK,
288 no_lock_available = ENOLCK,
David Chisnall81e68582010-08-11 16:52:41 +0000289#ifdef ENODATA
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290 no_message_available = ENODATA,
David Chisnall81e68582010-08-11 16:52:41 +0000291#else
292 no_message_available = ENOMSG,
293#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 no_message = ENOMSG,
295 no_protocol_option = ENOPROTOOPT,
296 no_space_on_device = ENOSPC,
David Chisnall81e68582010-08-11 16:52:41 +0000297#ifdef ENOSR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 no_stream_resources = ENOSR,
David Chisnall81e68582010-08-11 16:52:41 +0000299#else
300 no_stream_resources = ENOMEM,
301#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 no_such_device_or_address = ENXIO,
303 no_such_device = ENODEV,
304 no_such_file_or_directory = ENOENT,
305 no_such_process = ESRCH,
306 not_a_directory = ENOTDIR,
307 not_a_socket = ENOTSOCK,
David Chisnall81e68582010-08-11 16:52:41 +0000308#ifdef ENOSTR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 not_a_stream = ENOSTR,
David Chisnall81e68582010-08-11 16:52:41 +0000310#else
311 not_a_stream = EINVAL,
312#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 not_connected = ENOTCONN,
314 not_enough_memory = ENOMEM,
315 not_supported = ENOTSUP,
316 operation_canceled = ECANCELED,
317 operation_in_progress = EINPROGRESS,
318 operation_not_permitted = EPERM,
319 operation_not_supported = EOPNOTSUPP,
320 operation_would_block = EWOULDBLOCK,
321 owner_dead = EOWNERDEAD,
322 permission_denied = EACCES,
323 protocol_error = EPROTO,
324 protocol_not_supported = EPROTONOSUPPORT,
325 read_only_file_system = EROFS,
326 resource_deadlock_would_occur = EDEADLK,
327 resource_unavailable_try_again = EAGAIN,
328 result_out_of_range = ERANGE,
329 state_not_recoverable = ENOTRECOVERABLE,
David Chisnall81e68582010-08-11 16:52:41 +0000330#ifdef ETIME
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331 stream_timeout = ETIME,
David Chisnall81e68582010-08-11 16:52:41 +0000332#else
333 stream_timeout = ETIMEDOUT,
334#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 text_file_busy = ETXTBSY,
336 timed_out = ETIMEDOUT,
337 too_many_files_open_in_system = ENFILE,
338 too_many_files_open = EMFILE,
339 too_many_links = EMLINK,
340 too_many_symbolic_link_levels = ELOOP,
341 value_too_large = EOVERFLOW,
342 wrong_protocol_type = EPROTOTYPE
343};
344
345 _ __v_;
346
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000347 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348 errc(_ __v) : __v_(__v) {}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000349 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350 operator int() const {return __v_;}
351
352};
353
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000354template <>
355struct _LIBCPP_VISIBLE is_error_condition_enum<errc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356 : true_type { };
357
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000358template <>
359struct _LIBCPP_VISIBLE is_error_condition_enum<errc::_>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360 : true_type { };
361
362class error_condition;
363class error_code;
364
365// class error_category
366
367class __do_message;
368
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000369class _LIBCPP_VISIBLE error_category
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370{
371public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000372 virtual ~error_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373
374private:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000375 error_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376 error_category(const error_category&);// = delete;
377 error_category& operator=(const error_category&);// = delete;
378
379public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000380 virtual const char* name() const _NOEXCEPT = 0;
381 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
382 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
383 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 virtual string message(int __ev) const = 0;
385
386 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000387 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388
389 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000390 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391
392 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000393 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394
395 friend class __do_message;
396};
397
398class _LIBCPP_HIDDEN __do_message
399 : public error_category
400{
401public:
402 virtual string message(int ev) const;
403};
404
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000405const error_category& generic_category() _NOEXCEPT;
406const error_category& system_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000408class _LIBCPP_VISIBLE error_condition
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409{
410 int __val_;
411 const error_category* __cat_;
412public:
413 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000414 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415
416 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000417 error_condition(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418 : __val_(__val), __cat_(&__cat) {}
419
420 template <class _E>
421 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000422 error_condition(_E __e,
423 typename enable_if<is_error_condition_enum<_E>::value>::type* = 0
424 ) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 {*this = make_error_condition(__e);}
426
427 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000428 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 {
430 __val_ = __val;
431 __cat_ = &__cat;
432 }
433
434 template <class _E>
435 _LIBCPP_ALWAYS_INLINE
436 typename enable_if
437 <
438 is_error_condition_enum<_E>::value,
439 error_condition&
440 >::type
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000441 operator=(_E __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 {*this = make_error_condition(__e); return *this;}
443
444 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000445 void clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446 {
447 __val_ = 0;
448 __cat_ = &generic_category();
449 }
450
451 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000452 int value() const _NOEXCEPT {return __val_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453
454 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000455 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456 string message() const;
457
458 _LIBCPP_ALWAYS_INLINE
459 //explicit
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000460 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461};
462
463inline _LIBCPP_INLINE_VISIBILITY
464error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000465make_error_condition(errc __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466{
467 return error_condition(static_cast<int>(__e), generic_category());
468}
469
470inline _LIBCPP_INLINE_VISIBILITY
471bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000472operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473{
474 return __x.category() < __y.category()
475 || __x.category() == __y.category() && __x.value() < __y.value();
476}
477
478// error_code
479
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000480class _LIBCPP_VISIBLE error_code
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481{
482 int __val_;
483 const error_category* __cat_;
484public:
485 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000486 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487
488 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000489 error_code(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490 : __val_(__val), __cat_(&__cat) {}
491
492 template <class _E>
493 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000494 error_code(_E __e,
495 typename enable_if<is_error_code_enum<_E>::value>::type* = 0
496 ) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497 {*this = make_error_code(__e);}
498
499 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000500 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501 {
502 __val_ = __val;
503 __cat_ = &__cat;
504 }
505
506 template <class _E>
507 _LIBCPP_ALWAYS_INLINE
508 typename enable_if
509 <
510 is_error_code_enum<_E>::value,
511 error_code&
512 >::type
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000513 operator=(_E __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514 {*this = make_error_code(__e); return *this;}
515
516 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000517 void clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518 {
519 __val_ = 0;
520 __cat_ = &system_category();
521 }
522
523 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000524 int value() const _NOEXCEPT {return __val_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525
526 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000527 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528
529 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000530 error_condition default_error_condition() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531 {return __cat_->default_error_condition(__val_);}
532
533 string message() const;
534
535 _LIBCPP_ALWAYS_INLINE
536 //explicit
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000537 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538};
539
540inline _LIBCPP_INLINE_VISIBILITY
541error_code
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000542make_error_code(errc __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543{
544 return error_code(static_cast<int>(__e), generic_category());
545}
546
547inline _LIBCPP_INLINE_VISIBILITY
548bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000549operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550{
551 return __x.category() < __y.category()
552 || __x.category() == __y.category() && __x.value() < __y.value();
553}
554
555inline _LIBCPP_INLINE_VISIBILITY
556bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000557operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558{
559 return __x.category() == __y.category() && __x.value() == __y.value();
560}
561
562inline _LIBCPP_INLINE_VISIBILITY
563bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000564operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565{
566 return __x.category().equivalent(__x.value(), __y)
567 || __y.category().equivalent(__x, __y.value());
568}
569
570inline _LIBCPP_INLINE_VISIBILITY
571bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000572operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573{
574 return __y == __x;
575}
576
577inline _LIBCPP_INLINE_VISIBILITY
578bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000579operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580{
581 return __x.category() == __y.category() && __x.value() == __y.value();
582}
583
584inline _LIBCPP_INLINE_VISIBILITY
585bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000586operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
587{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588
589inline _LIBCPP_INLINE_VISIBILITY
590bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000591operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
592{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593
594inline _LIBCPP_INLINE_VISIBILITY
595bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000596operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
597{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
599inline _LIBCPP_INLINE_VISIBILITY
600bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000601operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
602{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603
604template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000605struct _LIBCPP_VISIBLE hash<error_code>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606 : public unary_function<error_code, size_t>
607{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000609 size_t operator()(const error_code& __ec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 {
611 return static_cast<size_t>(__ec.value());
612 }
613};
614
615// system_error
616
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000617class _LIBCPP_VISIBLE system_error
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 : public runtime_error
619{
620 error_code __ec_;
621public:
622 system_error(error_code __ec, const string& __what_arg);
623 system_error(error_code __ec, const char* __what_arg);
624 system_error(error_code __ec);
625 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
626 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
627 system_error(int __ev, const error_category& __ecat);
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000628 ~system_error() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629
630 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000631 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632
633private:
634 static string __init(const error_code&, string);
635};
636
637void __throw_system_error(int ev, const char* what_arg);
638
639_LIBCPP_END_NAMESPACE_STD
640
641#endif // _LIBCPP_SYSTEM_ERROR