blob: 12745525f57ffbc3882e9664e23b1002f8139658 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- system_error ----------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-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 Hinnant3e519522010-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 Hinnanta62f2892011-05-26 19:48:01 +000023 virtual ~error_category() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000024
Marshall Clowa86d5162013-08-21 02:57:19 +000025 constexpr error_category();
Howard Hinnant3e519522010-05-11 19:42:16 +000026 error_category(const error_category&) = delete;
27 error_category& operator=(const error_category&) = delete;
28
Howard Hinnanta62f2892011-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 Hinnant3e519522010-05-11 19:42:16 +000033 virtual string message(int ev) const = 0;
34
Howard Hinnanta62f2892011-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 Hinnant3e519522010-05-11 19:42:16 +000038};
39
Howard Hinnanta62f2892011-05-26 19:48:01 +000040const error_category& generic_category() noexcept;
41const error_category& system_category() noexcept;
Howard Hinnant3e519522010-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
Marshall Clowe69a08b2016-09-24 17:36:14 +000049template <class _Tp>
50constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
51
52template <class _Tp>
53constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
54
Howard Hinnant3e519522010-05-11 19:42:16 +000055class error_code
56{
57public:
58 // constructors:
Howard Hinnanta62f2892011-05-26 19:48:01 +000059 error_code() noexcept;
60 error_code(int val, const error_category& cat) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000061 template <class ErrorCodeEnum>
Howard Hinnanta62f2892011-05-26 19:48:01 +000062 error_code(ErrorCodeEnum e) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000063
64 // modifiers:
Howard Hinnanta62f2892011-05-26 19:48:01 +000065 void assign(int val, const error_category& cat) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000066 template <class ErrorCodeEnum>
Howard Hinnanta62f2892011-05-26 19:48:01 +000067 error_code& operator=(ErrorCodeEnum e) noexcept;
68 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000069
70 // observers:
Howard Hinnanta62f2892011-05-26 19:48:01 +000071 int value() const noexcept;
72 const error_category& category() const noexcept;
73 error_condition default_error_condition() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000074 string message() const;
Howard Hinnanta62f2892011-05-26 19:48:01 +000075 explicit operator bool() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000076};
77
78// non-member functions:
Howard Hinnanta62f2892011-05-26 19:48:01 +000079bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000080template <class charT, class traits>
81 basic_ostream<charT,traits>&
82 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
83
84class error_condition
85{
86public:
87 // constructors:
Howard Hinnanta62f2892011-05-26 19:48:01 +000088 error_condition() noexcept;
89 error_condition(int val, const error_category& cat) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000090 template <class ErrorConditionEnum>
Howard Hinnanta62f2892011-05-26 19:48:01 +000091 error_condition(ErrorConditionEnum e) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000092
93 // modifiers:
Howard Hinnanta62f2892011-05-26 19:48:01 +000094 void assign(int val, const error_category& cat) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000095 template <class ErrorConditionEnum>
Howard Hinnanta62f2892011-05-26 19:48:01 +000096 error_condition& operator=(ErrorConditionEnum e) noexcept;
97 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000098
99 // observers:
Howard Hinnanta62f2892011-05-26 19:48:01 +0000100 int value() const noexcept;
101 const error_category& category() const noexcept;
102 string message() const noexcept;
103 explicit operator bool() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000104};
105
Howard Hinnanta62f2892011-05-26 19:48:01 +0000106bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000107
108class system_error
109 : public runtime_error
110{
111public:
112 system_error(error_code ec, const string& what_arg);
113 system_error(error_code ec, const char* what_arg);
114 system_error(error_code ec);
115 system_error(int ev, const error_category& ecat, const string& what_arg);
116 system_error(int ev, const error_category& ecat, const char* what_arg);
117 system_error(int ev, const error_category& ecat);
118
Howard Hinnanta62f2892011-05-26 19:48:01 +0000119 const error_code& code() const noexcept;
120 const char* what() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000121};
122
123enum class errc
124{
125 address_family_not_supported, // EAFNOSUPPORT
126 address_in_use, // EADDRINUSE
127 address_not_available, // EADDRNOTAVAIL
128 already_connected, // EISCONN
129 argument_list_too_long, // E2BIG
130 argument_out_of_domain, // EDOM
131 bad_address, // EFAULT
132 bad_file_descriptor, // EBADF
133 bad_message, // EBADMSG
134 broken_pipe, // EPIPE
135 connection_aborted, // ECONNABORTED
136 connection_already_in_progress, // EALREADY
137 connection_refused, // ECONNREFUSED
138 connection_reset, // ECONNRESET
139 cross_device_link, // EXDEV
140 destination_address_required, // EDESTADDRREQ
141 device_or_resource_busy, // EBUSY
142 directory_not_empty, // ENOTEMPTY
143 executable_format_error, // ENOEXEC
144 file_exists, // EEXIST
145 file_too_large, // EFBIG
146 filename_too_long, // ENAMETOOLONG
147 function_not_supported, // ENOSYS
148 host_unreachable, // EHOSTUNREACH
149 identifier_removed, // EIDRM
150 illegal_byte_sequence, // EILSEQ
151 inappropriate_io_control_operation, // ENOTTY
152 interrupted, // EINTR
153 invalid_argument, // EINVAL
154 invalid_seek, // ESPIPE
155 io_error, // EIO
156 is_a_directory, // EISDIR
157 message_size, // EMSGSIZE
158 network_down, // ENETDOWN
159 network_reset, // ENETRESET
160 network_unreachable, // ENETUNREACH
161 no_buffer_space, // ENOBUFS
162 no_child_process, // ECHILD
163 no_link, // ENOLINK
164 no_lock_available, // ENOLCK
165 no_message_available, // ENODATA
166 no_message, // ENOMSG
167 no_protocol_option, // ENOPROTOOPT
168 no_space_on_device, // ENOSPC
169 no_stream_resources, // ENOSR
170 no_such_device_or_address, // ENXIO
171 no_such_device, // ENODEV
172 no_such_file_or_directory, // ENOENT
173 no_such_process, // ESRCH
174 not_a_directory, // ENOTDIR
175 not_a_socket, // ENOTSOCK
176 not_a_stream, // ENOSTR
177 not_connected, // ENOTCONN
178 not_enough_memory, // ENOMEM
179 not_supported, // ENOTSUP
180 operation_canceled, // ECANCELED
181 operation_in_progress, // EINPROGRESS
182 operation_not_permitted, // EPERM
183 operation_not_supported, // EOPNOTSUPP
184 operation_would_block, // EWOULDBLOCK
185 owner_dead, // EOWNERDEAD
186 permission_denied, // EACCES
187 protocol_error, // EPROTO
188 protocol_not_supported, // EPROTONOSUPPORT
189 read_only_file_system, // EROFS
190 resource_deadlock_would_occur, // EDEADLK
191 resource_unavailable_try_again, // EAGAIN
192 result_out_of_range, // ERANGE
193 state_not_recoverable, // ENOTRECOVERABLE
194 stream_timeout, // ETIME
195 text_file_busy, // ETXTBSY
196 timed_out, // ETIMEDOUT
197 too_many_files_open_in_system, // ENFILE
198 too_many_files_open, // EMFILE
199 too_many_links, // EMLINK
200 too_many_symbolic_link_levels, // ELOOP
201 value_too_large, // EOVERFLOW
202 wrong_protocol_type // EPROTOTYPE
203};
204
205template <> struct is_error_condition_enum<errc>
206 : true_type { }
207
Howard Hinnanta62f2892011-05-26 19:48:01 +0000208error_code make_error_code(errc e) noexcept;
209error_condition make_error_condition(errc e) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000210
211// Comparison operators:
Howard Hinnanta62f2892011-05-26 19:48:01 +0000212bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
213bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
214bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
215bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
216bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
217bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
218bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
219bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000220
221template <> struct hash<std::error_code>;
Marshall Clow1c7fe122016-11-14 18:22:19 +0000222template <> struct hash<std::error_condition>;
Howard Hinnant3e519522010-05-11 19:42:16 +0000223
224} // std
225
226*/
227
228#include <__config>
229#include <cerrno>
230#include <type_traits>
231#include <stdexcept>
232#include <__functional_base>
Marshall Clowbc455472017-09-11 16:05:42 +0000233#include <string>
Howard Hinnant3e519522010-05-11 19:42:16 +0000234
Howard Hinnant073458b2011-10-17 20:05:10 +0000235#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000236#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000237#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000238
239_LIBCPP_BEGIN_NAMESPACE_STD
240
241// is_error_code_enum
242
Howard Hinnante0601332010-09-23 17:31:07 +0000243template <class _Tp>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000244struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
Howard Hinnant3e519522010-05-11 19:42:16 +0000245 : public false_type {};
246
Marshall Clowe69a08b2016-09-24 17:36:14 +0000247#if _LIBCPP_STD_VER > 14
248template <class _Tp>
249constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
250#endif
251
Howard Hinnant3e519522010-05-11 19:42:16 +0000252// is_error_condition_enum
253
Howard Hinnante0601332010-09-23 17:31:07 +0000254template <class _Tp>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000255struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
Howard Hinnant3e519522010-05-11 19:42:16 +0000256 : public false_type {};
257
Marshall Clowe69a08b2016-09-24 17:36:14 +0000258#if _LIBCPP_STD_VER > 14
259template <class _Tp>
260constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
261#endif
262
David Chisnall37aab762010-08-11 16:52:41 +0000263// Some error codes are not present on all platforms, so we provide equivalents
264// for them:
265
Howard Hinnant3e519522010-05-11 19:42:16 +0000266//enum class errc
Howard Hinnant75689c12011-12-02 19:36:40 +0000267_LIBCPP_DECLARE_STRONG_ENUM(errc)
Howard Hinnant3e519522010-05-11 19:42:16 +0000268{
Howard Hinnant3e519522010-05-11 19:42:16 +0000269 address_family_not_supported = EAFNOSUPPORT,
270 address_in_use = EADDRINUSE,
271 address_not_available = EADDRNOTAVAIL,
272 already_connected = EISCONN,
273 argument_list_too_long = E2BIG,
274 argument_out_of_domain = EDOM,
275 bad_address = EFAULT,
276 bad_file_descriptor = EBADF,
277 bad_message = EBADMSG,
278 broken_pipe = EPIPE,
279 connection_aborted = ECONNABORTED,
280 connection_already_in_progress = EALREADY,
281 connection_refused = ECONNREFUSED,
282 connection_reset = ECONNRESET,
283 cross_device_link = EXDEV,
284 destination_address_required = EDESTADDRREQ,
285 device_or_resource_busy = EBUSY,
286 directory_not_empty = ENOTEMPTY,
287 executable_format_error = ENOEXEC,
288 file_exists = EEXIST,
289 file_too_large = EFBIG,
290 filename_too_long = ENAMETOOLONG,
291 function_not_supported = ENOSYS,
292 host_unreachable = EHOSTUNREACH,
293 identifier_removed = EIDRM,
294 illegal_byte_sequence = EILSEQ,
295 inappropriate_io_control_operation = ENOTTY,
296 interrupted = EINTR,
297 invalid_argument = EINVAL,
298 invalid_seek = ESPIPE,
299 io_error = EIO,
300 is_a_directory = EISDIR,
301 message_size = EMSGSIZE,
302 network_down = ENETDOWN,
303 network_reset = ENETRESET,
304 network_unreachable = ENETUNREACH,
305 no_buffer_space = ENOBUFS,
306 no_child_process = ECHILD,
307 no_link = ENOLINK,
308 no_lock_available = ENOLCK,
David Chisnall37aab762010-08-11 16:52:41 +0000309#ifdef ENODATA
Howard Hinnant3e519522010-05-11 19:42:16 +0000310 no_message_available = ENODATA,
David Chisnall37aab762010-08-11 16:52:41 +0000311#else
312 no_message_available = ENOMSG,
313#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000314 no_message = ENOMSG,
315 no_protocol_option = ENOPROTOOPT,
316 no_space_on_device = ENOSPC,
David Chisnall37aab762010-08-11 16:52:41 +0000317#ifdef ENOSR
Howard Hinnant3e519522010-05-11 19:42:16 +0000318 no_stream_resources = ENOSR,
David Chisnall37aab762010-08-11 16:52:41 +0000319#else
320 no_stream_resources = ENOMEM,
321#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000322 no_such_device_or_address = ENXIO,
323 no_such_device = ENODEV,
324 no_such_file_or_directory = ENOENT,
325 no_such_process = ESRCH,
326 not_a_directory = ENOTDIR,
327 not_a_socket = ENOTSOCK,
David Chisnall37aab762010-08-11 16:52:41 +0000328#ifdef ENOSTR
Howard Hinnant3e519522010-05-11 19:42:16 +0000329 not_a_stream = ENOSTR,
David Chisnall37aab762010-08-11 16:52:41 +0000330#else
331 not_a_stream = EINVAL,
332#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000333 not_connected = ENOTCONN,
334 not_enough_memory = ENOMEM,
335 not_supported = ENOTSUP,
336 operation_canceled = ECANCELED,
337 operation_in_progress = EINPROGRESS,
338 operation_not_permitted = EPERM,
339 operation_not_supported = EOPNOTSUPP,
340 operation_would_block = EWOULDBLOCK,
341 owner_dead = EOWNERDEAD,
342 permission_denied = EACCES,
343 protocol_error = EPROTO,
344 protocol_not_supported = EPROTONOSUPPORT,
345 read_only_file_system = EROFS,
346 resource_deadlock_would_occur = EDEADLK,
347 resource_unavailable_try_again = EAGAIN,
348 result_out_of_range = ERANGE,
349 state_not_recoverable = ENOTRECOVERABLE,
David Chisnall37aab762010-08-11 16:52:41 +0000350#ifdef ETIME
Howard Hinnant3e519522010-05-11 19:42:16 +0000351 stream_timeout = ETIME,
David Chisnall37aab762010-08-11 16:52:41 +0000352#else
353 stream_timeout = ETIMEDOUT,
354#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000355 text_file_busy = ETXTBSY,
356 timed_out = ETIMEDOUT,
357 too_many_files_open_in_system = ENFILE,
358 too_many_files_open = EMFILE,
359 too_many_links = EMLINK,
360 too_many_symbolic_link_levels = ELOOP,
361 value_too_large = EOVERFLOW,
362 wrong_protocol_type = EPROTOTYPE
363};
Howard Hinnant75689c12011-12-02 19:36:40 +0000364_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc)
Howard Hinnant3e519522010-05-11 19:42:16 +0000365
Howard Hinnante0601332010-09-23 17:31:07 +0000366template <>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000367struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
Howard Hinnant3e519522010-05-11 19:42:16 +0000368 : true_type { };
369
Howard Hinnant75689c12011-12-02 19:36:40 +0000370#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnante0601332010-09-23 17:31:07 +0000371template <>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000372struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
Howard Hinnant3e519522010-05-11 19:42:16 +0000373 : true_type { };
Howard Hinnant75689c12011-12-02 19:36:40 +0000374#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000375
Howard Hinnant6e412562013-03-06 23:30:19 +0000376class _LIBCPP_TYPE_VIS error_condition;
377class _LIBCPP_TYPE_VIS error_code;
Howard Hinnant3e519522010-05-11 19:42:16 +0000378
379// class error_category
380
Howard Hinnantaeb85682012-09-14 00:39:16 +0000381class _LIBCPP_HIDDEN __do_message;
Howard Hinnant3e519522010-05-11 19:42:16 +0000382
Howard Hinnant6e412562013-03-06 23:30:19 +0000383class _LIBCPP_TYPE_VIS error_category
Howard Hinnant3e519522010-05-11 19:42:16 +0000384{
385public:
Howard Hinnanta62f2892011-05-26 19:48:01 +0000386 virtual ~error_category() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000387
Eric Fiselier9aca97d2017-01-02 22:17:51 +0000388#if defined(_LIBCPP_BUILDING_SYSTEM_ERROR) && \
Eric Fiselier11f60452017-01-17 03:16:26 +0000389 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Howard Hinnanta62f2892011-05-26 19:48:01 +0000390 error_category() _NOEXCEPT;
Marshall Clowa86d5162013-08-21 02:57:19 +0000391#else
Howard Hinnantd8bf8502013-08-22 17:41:48 +0000392 _LIBCPP_ALWAYS_INLINE
Eric Fiseliere57e3ae2015-08-28 07:02:42 +0000393 _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT
Marshall Clowa86d5162013-08-21 02:57:19 +0000394#endif
Howard Hinnantcb16c682012-03-21 16:18:57 +0000395private:
Howard Hinnant3e519522010-05-11 19:42:16 +0000396 error_category(const error_category&);// = delete;
397 error_category& operator=(const error_category&);// = delete;
398
399public:
Howard Hinnanta62f2892011-05-26 19:48:01 +0000400 virtual const char* name() const _NOEXCEPT = 0;
401 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
402 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
403 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000404 virtual string message(int __ev) const = 0;
405
406 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000407 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000408
409 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000410 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000411
412 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000413 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000414
Howard Hinnantaeb85682012-09-14 00:39:16 +0000415 friend class _LIBCPP_HIDDEN __do_message;
Howard Hinnant3e519522010-05-11 19:42:16 +0000416};
417
418class _LIBCPP_HIDDEN __do_message
419 : public error_category
420{
421public:
422 virtual string message(int ev) const;
423};
424
Howard Hinnantf0544c22013-08-12 18:38:34 +0000425_LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
426_LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000427
Howard Hinnant6e412562013-03-06 23:30:19 +0000428class _LIBCPP_TYPE_VIS error_condition
Howard Hinnant3e519522010-05-11 19:42:16 +0000429{
430 int __val_;
431 const error_category* __cat_;
432public:
433 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000434 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000435
436 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000437 error_condition(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000438 : __val_(__val), __cat_(&__cat) {}
439
Howard Hinnantc003db12011-11-29 18:15:50 +0000440 template <class _Ep>
Howard Hinnant3e519522010-05-11 19:42:16 +0000441 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc003db12011-11-29 18:15:50 +0000442 error_condition(_Ep __e,
443 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
Howard Hinnanta62f2892011-05-26 19:48:01 +0000444 ) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000445 {*this = make_error_condition(__e);}
446
447 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000448 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000449 {
450 __val_ = __val;
451 __cat_ = &__cat;
452 }
453
Howard Hinnantc003db12011-11-29 18:15:50 +0000454 template <class _Ep>
Howard Hinnant3e519522010-05-11 19:42:16 +0000455 _LIBCPP_ALWAYS_INLINE
456 typename enable_if
457 <
Howard Hinnantc003db12011-11-29 18:15:50 +0000458 is_error_condition_enum<_Ep>::value,
Howard Hinnant3e519522010-05-11 19:42:16 +0000459 error_condition&
460 >::type
Howard Hinnantc003db12011-11-29 18:15:50 +0000461 operator=(_Ep __e) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000462 {*this = make_error_condition(__e); return *this;}
463
464 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000465 void clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000466 {
467 __val_ = 0;
468 __cat_ = &generic_category();
469 }
470
471 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000472 int value() const _NOEXCEPT {return __val_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000473
474 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000475 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000476 string message() const;
477
478 _LIBCPP_ALWAYS_INLINE
Howard Hinnantf2f2d8b2012-02-21 21:46:43 +0000479 _LIBCPP_EXPLICIT
Howard Hinnanta62f2892011-05-26 19:48:01 +0000480 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000481};
482
483inline _LIBCPP_INLINE_VISIBILITY
484error_condition
Howard Hinnanta62f2892011-05-26 19:48:01 +0000485make_error_condition(errc __e) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000486{
487 return error_condition(static_cast<int>(__e), generic_category());
488}
489
490inline _LIBCPP_INLINE_VISIBILITY
491bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000492operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000493{
494 return __x.category() < __y.category()
Howard Hinnantc2063662011-12-01 20:21:04 +0000495 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnant3e519522010-05-11 19:42:16 +0000496}
497
498// error_code
499
Howard Hinnant6e412562013-03-06 23:30:19 +0000500class _LIBCPP_TYPE_VIS error_code
Howard Hinnant3e519522010-05-11 19:42:16 +0000501{
502 int __val_;
503 const error_category* __cat_;
504public:
505 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000506 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000507
508 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000509 error_code(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000510 : __val_(__val), __cat_(&__cat) {}
511
Howard Hinnantc003db12011-11-29 18:15:50 +0000512 template <class _Ep>
Howard Hinnant3e519522010-05-11 19:42:16 +0000513 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc003db12011-11-29 18:15:50 +0000514 error_code(_Ep __e,
515 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
Howard Hinnanta62f2892011-05-26 19:48:01 +0000516 ) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000517 {*this = make_error_code(__e);}
518
519 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000520 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000521 {
522 __val_ = __val;
523 __cat_ = &__cat;
524 }
525
Howard Hinnantc003db12011-11-29 18:15:50 +0000526 template <class _Ep>
Howard Hinnant3e519522010-05-11 19:42:16 +0000527 _LIBCPP_ALWAYS_INLINE
528 typename enable_if
529 <
Howard Hinnantc003db12011-11-29 18:15:50 +0000530 is_error_code_enum<_Ep>::value,
Howard Hinnant3e519522010-05-11 19:42:16 +0000531 error_code&
532 >::type
Howard Hinnantc003db12011-11-29 18:15:50 +0000533 operator=(_Ep __e) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000534 {*this = make_error_code(__e); return *this;}
535
536 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000537 void clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000538 {
539 __val_ = 0;
540 __cat_ = &system_category();
541 }
542
543 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000544 int value() const _NOEXCEPT {return __val_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000545
546 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000547 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000548
549 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000550 error_condition default_error_condition() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000551 {return __cat_->default_error_condition(__val_);}
552
553 string message() const;
554
555 _LIBCPP_ALWAYS_INLINE
Howard Hinnantf2f2d8b2012-02-21 21:46:43 +0000556 _LIBCPP_EXPLICIT
Howard Hinnanta62f2892011-05-26 19:48:01 +0000557 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000558};
559
560inline _LIBCPP_INLINE_VISIBILITY
561error_code
Howard Hinnanta62f2892011-05-26 19:48:01 +0000562make_error_code(errc __e) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000563{
564 return error_code(static_cast<int>(__e), generic_category());
565}
566
567inline _LIBCPP_INLINE_VISIBILITY
568bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000569operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000570{
571 return __x.category() < __y.category()
Howard Hinnantc2063662011-12-01 20:21:04 +0000572 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnant3e519522010-05-11 19:42:16 +0000573}
574
575inline _LIBCPP_INLINE_VISIBILITY
576bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000577operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000578{
579 return __x.category() == __y.category() && __x.value() == __y.value();
580}
581
582inline _LIBCPP_INLINE_VISIBILITY
583bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000584operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000585{
586 return __x.category().equivalent(__x.value(), __y)
587 || __y.category().equivalent(__x, __y.value());
588}
589
590inline _LIBCPP_INLINE_VISIBILITY
591bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000592operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000593{
594 return __y == __x;
595}
596
597inline _LIBCPP_INLINE_VISIBILITY
598bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000599operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000600{
601 return __x.category() == __y.category() && __x.value() == __y.value();
602}
603
604inline _LIBCPP_INLINE_VISIBILITY
605bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000606operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
607{return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000608
609inline _LIBCPP_INLINE_VISIBILITY
610bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000611operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
612{return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000613
614inline _LIBCPP_INLINE_VISIBILITY
615bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000616operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
617{return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000618
619inline _LIBCPP_INLINE_VISIBILITY
620bool
Howard Hinnanta62f2892011-05-26 19:48:01 +0000621operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
622{return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000623
624template <>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000625struct _LIBCPP_TEMPLATE_VIS hash<error_code>
Howard Hinnant3e519522010-05-11 19:42:16 +0000626 : public unary_function<error_code, size_t>
627{
Howard Hinnante0601332010-09-23 17:31:07 +0000628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta62f2892011-05-26 19:48:01 +0000629 size_t operator()(const error_code& __ec) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000630 {
631 return static_cast<size_t>(__ec.value());
632 }
633};
634
Marshall Clow1c7fe122016-11-14 18:22:19 +0000635template <>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000636struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
Marshall Clow1c7fe122016-11-14 18:22:19 +0000637 : public unary_function<error_condition, size_t>
638{
639 _LIBCPP_INLINE_VISIBILITY
640 size_t operator()(const error_condition& __ec) const _NOEXCEPT
641 {
642 return static_cast<size_t>(__ec.value());
643 }
644};
645
Howard Hinnant3e519522010-05-11 19:42:16 +0000646// system_error
647
Howard Hinnant6e412562013-03-06 23:30:19 +0000648class _LIBCPP_TYPE_VIS system_error
Howard Hinnant3e519522010-05-11 19:42:16 +0000649 : public runtime_error
650{
651 error_code __ec_;
652public:
653 system_error(error_code __ec, const string& __what_arg);
654 system_error(error_code __ec, const char* __what_arg);
655 system_error(error_code __ec);
656 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
657 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
658 system_error(int __ev, const error_category& __ecat);
Howard Hinnanta62f2892011-05-26 19:48:01 +0000659 ~system_error() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000660
661 _LIBCPP_ALWAYS_INLINE
Howard Hinnanta62f2892011-05-26 19:48:01 +0000662 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000663
664private:
665 static string __init(const error_code&, string);
666};
667
Aditya Kumard51f2a22016-08-27 02:26:42 +0000668_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
669void __throw_system_error(int ev, const char* what_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +0000670
671_LIBCPP_END_NAMESPACE_STD
672
673#endif // _LIBCPP_SYSTEM_ERROR