blob: 66bf6d6c4249557571a5dd0737d30aa7ea1caa67 [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
Howard Hinnant8a1df3c2013-08-22 17:41:48 +0000373 _LIBCPP_ALWAYS_INLINE
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700374 _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT;
Marshall Clow5c316a62013-08-21 02:57:19 +0000375#endif
Howard Hinnant9aa4e112012-03-21 16:18:57 +0000376private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 error_category(const error_category&);// = delete;
378 error_category& operator=(const error_category&);// = delete;
379
380public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000381 virtual const char* name() const _NOEXCEPT = 0;
382 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
383 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
384 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 virtual string message(int __ev) const = 0;
386
387 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000388 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389
390 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000391 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000394 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395
Howard Hinnant33be35e2012-09-14 00:39:16 +0000396 friend class _LIBCPP_HIDDEN __do_message;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397};
398
399class _LIBCPP_HIDDEN __do_message
400 : public error_category
401{
402public:
403 virtual string message(int ev) const;
404};
405
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000406_LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
407_LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408
Howard Hinnant83eade62013-03-06 23:30:19 +0000409class _LIBCPP_TYPE_VIS error_condition
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410{
411 int __val_;
412 const error_category* __cat_;
413public:
414 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000415 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416
417 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000418 error_condition(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 : __val_(__val), __cat_(&__cat) {}
420
Howard Hinnant99968442011-11-29 18:15:50 +0000421 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422 _LIBCPP_ALWAYS_INLINE
Howard Hinnant99968442011-11-29 18:15:50 +0000423 error_condition(_Ep __e,
424 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000425 ) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 {*this = make_error_condition(__e);}
427
428 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000429 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 {
431 __val_ = __val;
432 __cat_ = &__cat;
433 }
434
Howard Hinnant99968442011-11-29 18:15:50 +0000435 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436 _LIBCPP_ALWAYS_INLINE
437 typename enable_if
438 <
Howard Hinnant99968442011-11-29 18:15:50 +0000439 is_error_condition_enum<_Ep>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440 error_condition&
441 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000442 operator=(_Ep __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443 {*this = make_error_condition(__e); return *this;}
444
445 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000446 void clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447 {
448 __val_ = 0;
449 __cat_ = &generic_category();
450 }
451
452 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000453 int value() const _NOEXCEPT {return __val_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454
455 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000456 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 string message() const;
458
459 _LIBCPP_ALWAYS_INLINE
Howard Hinnant77861882012-02-21 21:46:43 +0000460 _LIBCPP_EXPLICIT
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000461 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462};
463
464inline _LIBCPP_INLINE_VISIBILITY
465error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000466make_error_condition(errc __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467{
468 return error_condition(static_cast<int>(__e), generic_category());
469}
470
471inline _LIBCPP_INLINE_VISIBILITY
472bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000473operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474{
475 return __x.category() < __y.category()
Howard Hinnantec3773c2011-12-01 20:21:04 +0000476 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477}
478
479// error_code
480
Howard Hinnant83eade62013-03-06 23:30:19 +0000481class _LIBCPP_TYPE_VIS error_code
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482{
483 int __val_;
484 const error_category* __cat_;
485public:
486 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000487 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488
489 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000490 error_code(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 : __val_(__val), __cat_(&__cat) {}
492
Howard Hinnant99968442011-11-29 18:15:50 +0000493 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494 _LIBCPP_ALWAYS_INLINE
Howard Hinnant99968442011-11-29 18:15:50 +0000495 error_code(_Ep __e,
496 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000497 ) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498 {*this = make_error_code(__e);}
499
500 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000501 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 {
503 __val_ = __val;
504 __cat_ = &__cat;
505 }
506
Howard Hinnant99968442011-11-29 18:15:50 +0000507 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 _LIBCPP_ALWAYS_INLINE
509 typename enable_if
510 <
Howard Hinnant99968442011-11-29 18:15:50 +0000511 is_error_code_enum<_Ep>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512 error_code&
513 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000514 operator=(_Ep __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 {*this = make_error_code(__e); return *this;}
516
517 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000518 void clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519 {
520 __val_ = 0;
521 __cat_ = &system_category();
522 }
523
524 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000525 int value() const _NOEXCEPT {return __val_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526
527 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000528 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529
530 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000531 error_condition default_error_condition() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532 {return __cat_->default_error_condition(__val_);}
533
534 string message() const;
535
536 _LIBCPP_ALWAYS_INLINE
Howard Hinnant77861882012-02-21 21:46:43 +0000537 _LIBCPP_EXPLICIT
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000538 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539};
540
541inline _LIBCPP_INLINE_VISIBILITY
542error_code
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000543make_error_code(errc __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544{
545 return error_code(static_cast<int>(__e), generic_category());
546}
547
548inline _LIBCPP_INLINE_VISIBILITY
549bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000550operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551{
552 return __x.category() < __y.category()
Howard Hinnantec3773c2011-12-01 20:21:04 +0000553 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554}
555
556inline _LIBCPP_INLINE_VISIBILITY
557bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000558operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559{
560 return __x.category() == __y.category() && __x.value() == __y.value();
561}
562
563inline _LIBCPP_INLINE_VISIBILITY
564bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000565operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566{
567 return __x.category().equivalent(__x.value(), __y)
568 || __y.category().equivalent(__x, __y.value());
569}
570
571inline _LIBCPP_INLINE_VISIBILITY
572bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000573operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574{
575 return __y == __x;
576}
577
578inline _LIBCPP_INLINE_VISIBILITY
579bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000580operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581{
582 return __x.category() == __y.category() && __x.value() == __y.value();
583}
584
585inline _LIBCPP_INLINE_VISIBILITY
586bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000587operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
588{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589
590inline _LIBCPP_INLINE_VISIBILITY
591bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000592operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
593{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594
595inline _LIBCPP_INLINE_VISIBILITY
596bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000597operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
598{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599
600inline _LIBCPP_INLINE_VISIBILITY
601bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000602operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
603{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604
605template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000606struct _LIBCPP_TYPE_VIS_ONLY hash<error_code>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 : public unary_function<error_code, size_t>
608{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000610 size_t operator()(const error_code& __ec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611 {
612 return static_cast<size_t>(__ec.value());
613 }
614};
615
616// system_error
617
Howard Hinnant83eade62013-03-06 23:30:19 +0000618class _LIBCPP_TYPE_VIS system_error
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619 : public runtime_error
620{
621 error_code __ec_;
622public:
623 system_error(error_code __ec, const string& __what_arg);
624 system_error(error_code __ec, const char* __what_arg);
625 system_error(error_code __ec);
626 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
627 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
628 system_error(int __ev, const error_category& __ecat);
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000629 ~system_error() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630
631 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000632 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633
634private:
635 static string __init(const error_code&, string);
636};
637
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000638_LIBCPP_FUNC_VIS void __throw_system_error(int ev, const char* what_arg);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639
640_LIBCPP_END_NAMESPACE_STD
641
642#endif // _LIBCPP_SYSTEM_ERROR