blob: df668cc64c64dfaa38e149a5e9fbeef546de7e1b [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
Marshall Clow5c316a62013-08-21 02:57:19 +000025 constexpr error_category();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026 error_category(const error_category&) = delete;
27 error_category& operator=(const error_category&) = delete;
28
Howard Hinnant1e15fd12011-05-26 19:48:01 +000029 virtual const char* name() const noexcept = 0;
30 virtual error_condition default_error_condition(int ev) const noexcept;
31 virtual bool equivalent(int code, const error_condition& condition) const noexcept;
32 virtual bool equivalent(const error_code& code, int condition) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033 virtual string message(int ev) const = 0;
34
Howard Hinnant1e15fd12011-05-26 19:48:01 +000035 bool operator==(const error_category& rhs) const noexcept;
36 bool operator!=(const error_category& rhs) const noexcept;
37 bool operator<(const error_category& rhs) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038};
39
Howard Hinnant1e15fd12011-05-26 19:48:01 +000040const error_category& generic_category() noexcept;
41const error_category& system_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042
43template <class T> struct is_error_code_enum
44 : public false_type {};
45
46template <class T> struct is_error_condition_enum
47 : public false_type {};
48
49class error_code
50{
51public:
52 // constructors:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000053 error_code() noexcept;
54 error_code(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055 template <class ErrorCodeEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000056 error_code(ErrorCodeEnum e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057
58 // modifiers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000059 void assign(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 template <class ErrorCodeEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000061 error_code& operator=(ErrorCodeEnum e) noexcept;
62 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
64 // observers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000065 int value() const noexcept;
66 const error_category& category() const noexcept;
67 error_condition default_error_condition() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068 string message() const;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000069 explicit operator bool() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070};
71
72// non-member functions:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000073bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074template <class charT, class traits>
75 basic_ostream<charT,traits>&
76 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
77
78class error_condition
79{
80public:
81 // constructors:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000082 error_condition() noexcept;
83 error_condition(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084 template <class ErrorConditionEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000085 error_condition(ErrorConditionEnum e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 // modifiers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000088 void assign(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089 template <class ErrorConditionEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000090 error_condition& operator=(ErrorConditionEnum e) noexcept;
91 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092
93 // observers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000094 int value() const noexcept;
95 const error_category& category() const noexcept;
96 string message() const noexcept;
97 explicit operator bool() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098};
99
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000100bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101
102class system_error
103 : public runtime_error
104{
105public:
106 system_error(error_code ec, const string& what_arg);
107 system_error(error_code ec, const char* what_arg);
108 system_error(error_code ec);
109 system_error(int ev, const error_category& ecat, const string& what_arg);
110 system_error(int ev, const error_category& ecat, const char* what_arg);
111 system_error(int ev, const error_category& ecat);
112
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000113 const error_code& code() const noexcept;
114 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115};
116
117enum class errc
118{
119 address_family_not_supported, // EAFNOSUPPORT
120 address_in_use, // EADDRINUSE
121 address_not_available, // EADDRNOTAVAIL
122 already_connected, // EISCONN
123 argument_list_too_long, // E2BIG
124 argument_out_of_domain, // EDOM
125 bad_address, // EFAULT
126 bad_file_descriptor, // EBADF
127 bad_message, // EBADMSG
128 broken_pipe, // EPIPE
129 connection_aborted, // ECONNABORTED
130 connection_already_in_progress, // EALREADY
131 connection_refused, // ECONNREFUSED
132 connection_reset, // ECONNRESET
133 cross_device_link, // EXDEV
134 destination_address_required, // EDESTADDRREQ
135 device_or_resource_busy, // EBUSY
136 directory_not_empty, // ENOTEMPTY
137 executable_format_error, // ENOEXEC
138 file_exists, // EEXIST
139 file_too_large, // EFBIG
140 filename_too_long, // ENAMETOOLONG
141 function_not_supported, // ENOSYS
142 host_unreachable, // EHOSTUNREACH
143 identifier_removed, // EIDRM
144 illegal_byte_sequence, // EILSEQ
145 inappropriate_io_control_operation, // ENOTTY
146 interrupted, // EINTR
147 invalid_argument, // EINVAL
148 invalid_seek, // ESPIPE
149 io_error, // EIO
150 is_a_directory, // EISDIR
151 message_size, // EMSGSIZE
152 network_down, // ENETDOWN
153 network_reset, // ENETRESET
154 network_unreachable, // ENETUNREACH
155 no_buffer_space, // ENOBUFS
156 no_child_process, // ECHILD
157 no_link, // ENOLINK
158 no_lock_available, // ENOLCK
159 no_message_available, // ENODATA
160 no_message, // ENOMSG
161 no_protocol_option, // ENOPROTOOPT
162 no_space_on_device, // ENOSPC
163 no_stream_resources, // ENOSR
164 no_such_device_or_address, // ENXIO
165 no_such_device, // ENODEV
166 no_such_file_or_directory, // ENOENT
167 no_such_process, // ESRCH
168 not_a_directory, // ENOTDIR
169 not_a_socket, // ENOTSOCK
170 not_a_stream, // ENOSTR
171 not_connected, // ENOTCONN
172 not_enough_memory, // ENOMEM
173 not_supported, // ENOTSUP
174 operation_canceled, // ECANCELED
175 operation_in_progress, // EINPROGRESS
176 operation_not_permitted, // EPERM
177 operation_not_supported, // EOPNOTSUPP
178 operation_would_block, // EWOULDBLOCK
179 owner_dead, // EOWNERDEAD
180 permission_denied, // EACCES
181 protocol_error, // EPROTO
182 protocol_not_supported, // EPROTONOSUPPORT
183 read_only_file_system, // EROFS
184 resource_deadlock_would_occur, // EDEADLK
185 resource_unavailable_try_again, // EAGAIN
186 result_out_of_range, // ERANGE
187 state_not_recoverable, // ENOTRECOVERABLE
188 stream_timeout, // ETIME
189 text_file_busy, // ETXTBSY
190 timed_out, // ETIMEDOUT
191 too_many_files_open_in_system, // ENFILE
192 too_many_files_open, // EMFILE
193 too_many_links, // EMLINK
194 too_many_symbolic_link_levels, // ELOOP
195 value_too_large, // EOVERFLOW
196 wrong_protocol_type // EPROTOTYPE
197};
198
199template <> struct is_error_condition_enum<errc>
200 : true_type { }
201
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000202error_code make_error_code(errc e) noexcept;
203error_condition make_error_condition(errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204
205// Comparison operators:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000206bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
207bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
208bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
209bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
210bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
211bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
212bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
213bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214
215template <> struct hash<std::error_code>;
216
217} // std
218
219*/
220
221#include <__config>
222#include <cerrno>
223#include <type_traits>
224#include <stdexcept>
225#include <__functional_base>
226
Howard Hinnant08e17472011-10-17 20:05:10 +0000227#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000229#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230
231_LIBCPP_BEGIN_NAMESPACE_STD
232
233// is_error_code_enum
234
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000235template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000236struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237 : public false_type {};
238
239// is_error_condition_enum
240
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000241template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000242struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 : public false_type {};
244
David Chisnall81e68582010-08-11 16:52:41 +0000245// Some error codes are not present on all platforms, so we provide equivalents
246// for them:
247
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248//enum class errc
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000249_LIBCPP_DECLARE_STRONG_ENUM(errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251 address_family_not_supported = EAFNOSUPPORT,
252 address_in_use = EADDRINUSE,
253 address_not_available = EADDRNOTAVAIL,
254 already_connected = EISCONN,
255 argument_list_too_long = E2BIG,
256 argument_out_of_domain = EDOM,
257 bad_address = EFAULT,
258 bad_file_descriptor = EBADF,
259 bad_message = EBADMSG,
260 broken_pipe = EPIPE,
261 connection_aborted = ECONNABORTED,
262 connection_already_in_progress = EALREADY,
263 connection_refused = ECONNREFUSED,
264 connection_reset = ECONNRESET,
265 cross_device_link = EXDEV,
266 destination_address_required = EDESTADDRREQ,
267 device_or_resource_busy = EBUSY,
268 directory_not_empty = ENOTEMPTY,
269 executable_format_error = ENOEXEC,
270 file_exists = EEXIST,
271 file_too_large = EFBIG,
272 filename_too_long = ENAMETOOLONG,
273 function_not_supported = ENOSYS,
274 host_unreachable = EHOSTUNREACH,
275 identifier_removed = EIDRM,
276 illegal_byte_sequence = EILSEQ,
277 inappropriate_io_control_operation = ENOTTY,
278 interrupted = EINTR,
279 invalid_argument = EINVAL,
280 invalid_seek = ESPIPE,
281 io_error = EIO,
282 is_a_directory = EISDIR,
283 message_size = EMSGSIZE,
284 network_down = ENETDOWN,
285 network_reset = ENETRESET,
286 network_unreachable = ENETUNREACH,
287 no_buffer_space = ENOBUFS,
288 no_child_process = ECHILD,
289 no_link = ENOLINK,
290 no_lock_available = ENOLCK,
David Chisnall81e68582010-08-11 16:52:41 +0000291#ifdef ENODATA
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 no_message_available = ENODATA,
David Chisnall81e68582010-08-11 16:52:41 +0000293#else
294 no_message_available = ENOMSG,
295#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296 no_message = ENOMSG,
297 no_protocol_option = ENOPROTOOPT,
298 no_space_on_device = ENOSPC,
David Chisnall81e68582010-08-11 16:52:41 +0000299#ifdef ENOSR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300 no_stream_resources = ENOSR,
David Chisnall81e68582010-08-11 16:52:41 +0000301#else
302 no_stream_resources = ENOMEM,
303#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304 no_such_device_or_address = ENXIO,
305 no_such_device = ENODEV,
306 no_such_file_or_directory = ENOENT,
307 no_such_process = ESRCH,
308 not_a_directory = ENOTDIR,
309 not_a_socket = ENOTSOCK,
David Chisnall81e68582010-08-11 16:52:41 +0000310#ifdef ENOSTR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 not_a_stream = ENOSTR,
David Chisnall81e68582010-08-11 16:52:41 +0000312#else
313 not_a_stream = EINVAL,
314#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000315 not_connected = ENOTCONN,
316 not_enough_memory = ENOMEM,
317 not_supported = ENOTSUP,
318 operation_canceled = ECANCELED,
319 operation_in_progress = EINPROGRESS,
320 operation_not_permitted = EPERM,
321 operation_not_supported = EOPNOTSUPP,
322 operation_would_block = EWOULDBLOCK,
323 owner_dead = EOWNERDEAD,
324 permission_denied = EACCES,
325 protocol_error = EPROTO,
326 protocol_not_supported = EPROTONOSUPPORT,
327 read_only_file_system = EROFS,
328 resource_deadlock_would_occur = EDEADLK,
329 resource_unavailable_try_again = EAGAIN,
330 result_out_of_range = ERANGE,
331 state_not_recoverable = ENOTRECOVERABLE,
David Chisnall81e68582010-08-11 16:52:41 +0000332#ifdef ETIME
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333 stream_timeout = ETIME,
David Chisnall81e68582010-08-11 16:52:41 +0000334#else
335 stream_timeout = ETIMEDOUT,
336#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 text_file_busy = ETXTBSY,
338 timed_out = ETIMEDOUT,
339 too_many_files_open_in_system = ENFILE,
340 too_many_files_open = EMFILE,
341 too_many_links = EMLINK,
342 too_many_symbolic_link_levels = ELOOP,
343 value_too_large = EOVERFLOW,
344 wrong_protocol_type = EPROTOTYPE
345};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000346_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000348template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000349struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum<errc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350 : true_type { };
351
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000352#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000353template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000354struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum<errc::__lx>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355 : true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000356#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
Howard Hinnant83eade62013-03-06 23:30:19 +0000358class _LIBCPP_TYPE_VIS error_condition;
359class _LIBCPP_TYPE_VIS error_code;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360
361// class error_category
362
Howard Hinnant33be35e2012-09-14 00:39:16 +0000363class _LIBCPP_HIDDEN __do_message;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
Howard Hinnant83eade62013-03-06 23:30:19 +0000365class _LIBCPP_TYPE_VIS error_category
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366{
367public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000368 virtual ~error_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369
Marshall Clow5c316a62013-08-21 02:57:19 +0000370#ifdef _LIBCPP_BUILDING_SYSTEM_ERROR
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000371 error_category() _NOEXCEPT;
Marshall Clow5c316a62013-08-21 02:57:19 +0000372#else
373 _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT {}
374#endif
Howard Hinnant9aa4e112012-03-21 16:18:57 +0000375private:
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
Howard Hinnant33be35e2012-09-14 00:39:16 +0000395 friend class _LIBCPP_HIDDEN __do_message;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396};
397
398class _LIBCPP_HIDDEN __do_message
399 : public error_category
400{
401public:
402 virtual string message(int ev) const;
403};
404
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000405_LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
406_LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407
Howard Hinnant83eade62013-03-06 23:30:19 +0000408class _LIBCPP_TYPE_VIS 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
Howard Hinnant99968442011-11-29 18:15:50 +0000420 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 _LIBCPP_ALWAYS_INLINE
Howard Hinnant99968442011-11-29 18:15:50 +0000422 error_condition(_Ep __e,
423 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000424 ) _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
Howard Hinnant99968442011-11-29 18:15:50 +0000434 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 _LIBCPP_ALWAYS_INLINE
436 typename enable_if
437 <
Howard Hinnant99968442011-11-29 18:15:50 +0000438 is_error_condition_enum<_Ep>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439 error_condition&
440 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000441 operator=(_Ep __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
Howard Hinnant77861882012-02-21 21:46:43 +0000459 _LIBCPP_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()
Howard Hinnantec3773c2011-12-01 20:21:04 +0000475 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476}
477
478// error_code
479
Howard Hinnant83eade62013-03-06 23:30:19 +0000480class _LIBCPP_TYPE_VIS 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
Howard Hinnant99968442011-11-29 18:15:50 +0000492 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493 _LIBCPP_ALWAYS_INLINE
Howard Hinnant99968442011-11-29 18:15:50 +0000494 error_code(_Ep __e,
495 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000496 ) _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
Howard Hinnant99968442011-11-29 18:15:50 +0000506 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 _LIBCPP_ALWAYS_INLINE
508 typename enable_if
509 <
Howard Hinnant99968442011-11-29 18:15:50 +0000510 is_error_code_enum<_Ep>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511 error_code&
512 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000513 operator=(_Ep __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
Howard Hinnant77861882012-02-21 21:46:43 +0000536 _LIBCPP_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()
Howard Hinnantec3773c2011-12-01 20:21:04 +0000552 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553}
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 Hinnant0f678bd2013-08-12 18:38:34 +0000605struct _LIBCPP_TYPE_VIS_ONLY 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 Hinnant83eade62013-03-06 23:30:19 +0000617class _LIBCPP_TYPE_VIS 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
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000637_LIBCPP_FUNC_VIS void __throw_system_error(int ev, const char* what_arg);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
639_LIBCPP_END_NAMESPACE_STD
640
641#endif // _LIBCPP_SYSTEM_ERROR