blob: c7e73d4ab82b0012f89e02e77d2b78a31fa431e1 [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
Marshall Clowdb866842016-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 Hinnantbc8d3f92010-05-11 19:42:16 +000055class error_code
56{
57public:
58 // constructors:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000059 error_code() noexcept;
60 error_code(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061 template <class ErrorCodeEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000062 error_code(ErrorCodeEnum e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
64 // modifiers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000065 void assign(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 template <class ErrorCodeEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000067 error_code& operator=(ErrorCodeEnum e) noexcept;
68 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069
70 // observers:
Howard Hinnant1e15fd12011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000074 string message() const;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000075 explicit operator bool() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076};
77
78// non-member functions:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000079bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
Howard Hinnantbc8d3f92010-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 Hinnant1e15fd12011-05-26 19:48:01 +000088 error_condition() noexcept;
89 error_condition(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000090 template <class ErrorConditionEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000091 error_condition(ErrorConditionEnum e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092
93 // modifiers:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000094 void assign(int val, const error_category& cat) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095 template <class ErrorConditionEnum>
Howard Hinnant1e15fd12011-05-26 19:48:01 +000096 error_condition& operator=(ErrorConditionEnum e) noexcept;
97 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99 // observers:
Howard Hinnant1e15fd12011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000104};
105
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000106bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
Howard Hinnantbc8d3f92010-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 Hinnant1e15fd12011-05-26 19:48:01 +0000119 const error_code& code() const noexcept;
120 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-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 Hinnant1e15fd12011-05-26 19:48:01 +0000208error_code make_error_code(errc e) noexcept;
209error_condition make_error_condition(errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210
211// Comparison operators:
Howard Hinnant1e15fd12011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000220
221template <> struct hash<std::error_code>;
Marshall Clowdb7fa112016-11-14 18:22:19 +0000222template <> struct hash<std::error_condition>;
Howard Hinnantbc8d3f92010-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>
233
Howard Hinnant08e17472011-10-17 20:05:10 +0000234#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000236#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237
238_LIBCPP_BEGIN_NAMESPACE_STD
239
240// is_error_code_enum
241
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000242template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000243struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244 : public false_type {};
245
Marshall Clowdb866842016-09-24 17:36:14 +0000246#if _LIBCPP_STD_VER > 14
247template <class _Tp>
248constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
249#endif
250
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251// is_error_condition_enum
252
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000253template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000254struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255 : public false_type {};
256
Marshall Clowdb866842016-09-24 17:36:14 +0000257#if _LIBCPP_STD_VER > 14
258template <class _Tp>
259constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
260#endif
261
David Chisnall81e68582010-08-11 16:52:41 +0000262// Some error codes are not present on all platforms, so we provide equivalents
263// for them:
264
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265//enum class errc
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000266_LIBCPP_DECLARE_STRONG_ENUM(errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 address_family_not_supported = EAFNOSUPPORT,
269 address_in_use = EADDRINUSE,
270 address_not_available = EADDRNOTAVAIL,
271 already_connected = EISCONN,
272 argument_list_too_long = E2BIG,
273 argument_out_of_domain = EDOM,
274 bad_address = EFAULT,
275 bad_file_descriptor = EBADF,
276 bad_message = EBADMSG,
277 broken_pipe = EPIPE,
278 connection_aborted = ECONNABORTED,
279 connection_already_in_progress = EALREADY,
280 connection_refused = ECONNREFUSED,
281 connection_reset = ECONNRESET,
282 cross_device_link = EXDEV,
283 destination_address_required = EDESTADDRREQ,
284 device_or_resource_busy = EBUSY,
285 directory_not_empty = ENOTEMPTY,
286 executable_format_error = ENOEXEC,
287 file_exists = EEXIST,
288 file_too_large = EFBIG,
289 filename_too_long = ENAMETOOLONG,
290 function_not_supported = ENOSYS,
291 host_unreachable = EHOSTUNREACH,
292 identifier_removed = EIDRM,
293 illegal_byte_sequence = EILSEQ,
294 inappropriate_io_control_operation = ENOTTY,
295 interrupted = EINTR,
296 invalid_argument = EINVAL,
297 invalid_seek = ESPIPE,
298 io_error = EIO,
299 is_a_directory = EISDIR,
300 message_size = EMSGSIZE,
301 network_down = ENETDOWN,
302 network_reset = ENETRESET,
303 network_unreachable = ENETUNREACH,
304 no_buffer_space = ENOBUFS,
305 no_child_process = ECHILD,
306 no_link = ENOLINK,
307 no_lock_available = ENOLCK,
David Chisnall81e68582010-08-11 16:52:41 +0000308#ifdef ENODATA
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 no_message_available = ENODATA,
David Chisnall81e68582010-08-11 16:52:41 +0000310#else
311 no_message_available = ENOMSG,
312#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 no_message = ENOMSG,
314 no_protocol_option = ENOPROTOOPT,
315 no_space_on_device = ENOSPC,
David Chisnall81e68582010-08-11 16:52:41 +0000316#ifdef ENOSR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317 no_stream_resources = ENOSR,
David Chisnall81e68582010-08-11 16:52:41 +0000318#else
319 no_stream_resources = ENOMEM,
320#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 no_such_device_or_address = ENXIO,
322 no_such_device = ENODEV,
323 no_such_file_or_directory = ENOENT,
324 no_such_process = ESRCH,
325 not_a_directory = ENOTDIR,
326 not_a_socket = ENOTSOCK,
David Chisnall81e68582010-08-11 16:52:41 +0000327#ifdef ENOSTR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 not_a_stream = ENOSTR,
David Chisnall81e68582010-08-11 16:52:41 +0000329#else
330 not_a_stream = EINVAL,
331#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332 not_connected = ENOTCONN,
333 not_enough_memory = ENOMEM,
334 not_supported = ENOTSUP,
335 operation_canceled = ECANCELED,
336 operation_in_progress = EINPROGRESS,
337 operation_not_permitted = EPERM,
338 operation_not_supported = EOPNOTSUPP,
339 operation_would_block = EWOULDBLOCK,
340 owner_dead = EOWNERDEAD,
341 permission_denied = EACCES,
342 protocol_error = EPROTO,
343 protocol_not_supported = EPROTONOSUPPORT,
344 read_only_file_system = EROFS,
345 resource_deadlock_would_occur = EDEADLK,
346 resource_unavailable_try_again = EAGAIN,
347 result_out_of_range = ERANGE,
348 state_not_recoverable = ENOTRECOVERABLE,
David Chisnall81e68582010-08-11 16:52:41 +0000349#ifdef ETIME
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350 stream_timeout = ETIME,
David Chisnall81e68582010-08-11 16:52:41 +0000351#else
352 stream_timeout = ETIMEDOUT,
353#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354 text_file_busy = ETXTBSY,
355 timed_out = ETIMEDOUT,
356 too_many_files_open_in_system = ENFILE,
357 too_many_files_open = EMFILE,
358 too_many_links = EMLINK,
359 too_many_symbolic_link_levels = ELOOP,
360 value_too_large = EOVERFLOW,
361 wrong_protocol_type = EPROTOTYPE
362};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000363_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000365template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000366struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum<errc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367 : true_type { };
368
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000369#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000370template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000371struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum<errc::__lx>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 : true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000373#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
Howard Hinnant83eade62013-03-06 23:30:19 +0000375class _LIBCPP_TYPE_VIS error_condition;
376class _LIBCPP_TYPE_VIS error_code;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
378// class error_category
379
Howard Hinnant33be35e2012-09-14 00:39:16 +0000380class _LIBCPP_HIDDEN __do_message;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
Howard Hinnant83eade62013-03-06 23:30:19 +0000382class _LIBCPP_TYPE_VIS error_category
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383{
384public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000385 virtual ~error_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386
Marshall Clow5c316a62013-08-21 02:57:19 +0000387#ifdef _LIBCPP_BUILDING_SYSTEM_ERROR
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000388 error_category() _NOEXCEPT;
Marshall Clow5c316a62013-08-21 02:57:19 +0000389#else
Howard Hinnant8a1df3c2013-08-22 17:41:48 +0000390 _LIBCPP_ALWAYS_INLINE
Eric Fiseliere2d48922015-08-28 07:02:42 +0000391 _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT
Marshall Clow5c316a62013-08-21 02:57:19 +0000392#endif
Howard Hinnant9aa4e112012-03-21 16:18:57 +0000393private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394 error_category(const error_category&);// = delete;
395 error_category& operator=(const error_category&);// = delete;
396
397public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000398 virtual const char* name() const _NOEXCEPT = 0;
399 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
400 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
401 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402 virtual string message(int __ev) const = 0;
403
404 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000405 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406
407 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000408 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409
410 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000411 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412
Howard Hinnant33be35e2012-09-14 00:39:16 +0000413 friend class _LIBCPP_HIDDEN __do_message;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414};
415
416class _LIBCPP_HIDDEN __do_message
417 : public error_category
418{
419public:
420 virtual string message(int ev) const;
421};
422
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000423_LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
424_LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425
Howard Hinnant83eade62013-03-06 23:30:19 +0000426class _LIBCPP_TYPE_VIS error_condition
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427{
428 int __val_;
429 const error_category* __cat_;
430public:
431 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000432 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433
434 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000435 error_condition(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436 : __val_(__val), __cat_(&__cat) {}
437
Howard Hinnant99968442011-11-29 18:15:50 +0000438 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439 _LIBCPP_ALWAYS_INLINE
Howard Hinnant99968442011-11-29 18:15:50 +0000440 error_condition(_Ep __e,
441 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000442 ) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443 {*this = make_error_condition(__e);}
444
445 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000446 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447 {
448 __val_ = __val;
449 __cat_ = &__cat;
450 }
451
Howard Hinnant99968442011-11-29 18:15:50 +0000452 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453 _LIBCPP_ALWAYS_INLINE
454 typename enable_if
455 <
Howard Hinnant99968442011-11-29 18:15:50 +0000456 is_error_condition_enum<_Ep>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 error_condition&
458 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000459 operator=(_Ep __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460 {*this = make_error_condition(__e); return *this;}
461
462 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000463 void clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464 {
465 __val_ = 0;
466 __cat_ = &generic_category();
467 }
468
469 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000470 int value() const _NOEXCEPT {return __val_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471
472 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000473 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474 string message() const;
475
476 _LIBCPP_ALWAYS_INLINE
Howard Hinnant77861882012-02-21 21:46:43 +0000477 _LIBCPP_EXPLICIT
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000478 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479};
480
481inline _LIBCPP_INLINE_VISIBILITY
482error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000483make_error_condition(errc __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484{
485 return error_condition(static_cast<int>(__e), generic_category());
486}
487
488inline _LIBCPP_INLINE_VISIBILITY
489bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000490operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491{
492 return __x.category() < __y.category()
Howard Hinnantec3773c2011-12-01 20:21:04 +0000493 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494}
495
496// error_code
497
Howard Hinnant83eade62013-03-06 23:30:19 +0000498class _LIBCPP_TYPE_VIS error_code
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499{
500 int __val_;
501 const error_category* __cat_;
502public:
503 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000504 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505
506 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000507 error_code(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 : __val_(__val), __cat_(&__cat) {}
509
Howard Hinnant99968442011-11-29 18:15:50 +0000510 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511 _LIBCPP_ALWAYS_INLINE
Howard Hinnant99968442011-11-29 18:15:50 +0000512 error_code(_Ep __e,
513 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000514 ) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 {*this = make_error_code(__e);}
516
517 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000518 void assign(int __val, const error_category& __cat) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519 {
520 __val_ = __val;
521 __cat_ = &__cat;
522 }
523
Howard Hinnant99968442011-11-29 18:15:50 +0000524 template <class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525 _LIBCPP_ALWAYS_INLINE
526 typename enable_if
527 <
Howard Hinnant99968442011-11-29 18:15:50 +0000528 is_error_code_enum<_Ep>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529 error_code&
530 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000531 operator=(_Ep __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532 {*this = make_error_code(__e); return *this;}
533
534 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000535 void clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536 {
537 __val_ = 0;
538 __cat_ = &system_category();
539 }
540
541 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000542 int value() const _NOEXCEPT {return __val_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543
544 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000545 const error_category& category() const _NOEXCEPT {return *__cat_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546
547 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000548 error_condition default_error_condition() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 {return __cat_->default_error_condition(__val_);}
550
551 string message() const;
552
553 _LIBCPP_ALWAYS_INLINE
Howard Hinnant77861882012-02-21 21:46:43 +0000554 _LIBCPP_EXPLICIT
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000555 operator bool() const _NOEXCEPT {return __val_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556};
557
558inline _LIBCPP_INLINE_VISIBILITY
559error_code
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000560make_error_code(errc __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561{
562 return error_code(static_cast<int>(__e), generic_category());
563}
564
565inline _LIBCPP_INLINE_VISIBILITY
566bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000567operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568{
569 return __x.category() < __y.category()
Howard Hinnantec3773c2011-12-01 20:21:04 +0000570 || (__x.category() == __y.category() && __x.value() < __y.value());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571}
572
573inline _LIBCPP_INLINE_VISIBILITY
574bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000575operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576{
577 return __x.category() == __y.category() && __x.value() == __y.value();
578}
579
580inline _LIBCPP_INLINE_VISIBILITY
581bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000582operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583{
584 return __x.category().equivalent(__x.value(), __y)
585 || __y.category().equivalent(__x, __y.value());
586}
587
588inline _LIBCPP_INLINE_VISIBILITY
589bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000590operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591{
592 return __y == __x;
593}
594
595inline _LIBCPP_INLINE_VISIBILITY
596bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000597operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598{
599 return __x.category() == __y.category() && __x.value() == __y.value();
600}
601
602inline _LIBCPP_INLINE_VISIBILITY
603bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000604operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
605{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606
607inline _LIBCPP_INLINE_VISIBILITY
608bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000609operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
610{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
612inline _LIBCPP_INLINE_VISIBILITY
613bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000614operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
615{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616
617inline _LIBCPP_INLINE_VISIBILITY
618bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000619operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
620{return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621
622template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000623struct _LIBCPP_TYPE_VIS_ONLY hash<error_code>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624 : public unary_function<error_code, size_t>
625{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000627 size_t operator()(const error_code& __ec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628 {
629 return static_cast<size_t>(__ec.value());
630 }
631};
632
Marshall Clowdb7fa112016-11-14 18:22:19 +0000633template <>
634struct _LIBCPP_TYPE_VIS_ONLY hash<error_condition>
635 : public unary_function<error_condition, size_t>
636{
637 _LIBCPP_INLINE_VISIBILITY
638 size_t operator()(const error_condition& __ec) const _NOEXCEPT
639 {
640 return static_cast<size_t>(__ec.value());
641 }
642};
643
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644// system_error
645
Howard Hinnant83eade62013-03-06 23:30:19 +0000646class _LIBCPP_TYPE_VIS system_error
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647 : public runtime_error
648{
649 error_code __ec_;
650public:
651 system_error(error_code __ec, const string& __what_arg);
652 system_error(error_code __ec, const char* __what_arg);
653 system_error(error_code __ec);
654 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
655 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
656 system_error(int __ev, const error_category& __ecat);
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000657 ~system_error() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658
659 _LIBCPP_ALWAYS_INLINE
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000660 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661
662private:
663 static string __init(const error_code&, string);
664};
665
Aditya Kumar5db67372016-08-27 02:26:42 +0000666_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
667void __throw_system_error(int ev, const char* what_arg);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668
669_LIBCPP_END_NAMESPACE_STD
670
671#endif // _LIBCPP_SYSTEM_ERROR