comparison libstdc++-v3/include/std/system_error @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children 494b0b89df80
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 // <system_error> -*- C++ -*- 1 // <system_error> -*- C++ -*-
2 2
3 // Copyright (C) 2007-2018 Free Software Foundation, Inc. 3 // Copyright (C) 2007-2020 Free Software Foundation, Inc.
4 // 4 //
5 // This file is part of the GNU ISO C++ Library. This library is free 5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the 6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the 7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option) 8 // Free Software Foundation; either version 3, or (at your option)
41 #include <stdexcept> 41 #include <stdexcept>
42 42
43 namespace std _GLIBCXX_VISIBILITY(default) 43 namespace std _GLIBCXX_VISIBILITY(default)
44 { 44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION 45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /** @addtogroup diagnostics
48 * @{
49 */
46 50
47 class error_code; 51 class error_code;
48 class error_condition; 52 class error_condition;
49 class system_error; 53 class system_error;
50 54
68 inline constexpr bool is_error_condition_enum_v = 72 inline constexpr bool is_error_condition_enum_v =
69 is_error_condition_enum<_Tp>::value; 73 is_error_condition_enum<_Tp>::value;
70 #endif // C++17 74 #endif // C++17
71 inline namespace _V2 { 75 inline namespace _V2 {
72 76
73 /// error_category 77 /** Abstract base class for types defining a category of error codes.
78 *
79 * An error category defines a context that give meaning to the integer
80 * stored in an `error_code` or `error_condition` object. For example,
81 * the standard `errno` constants such a `EINVAL` and `ENOMEM` are
82 * associated with the "generic" category and other OS-specific error
83 * numbers are associated with the "system" category, but a user-defined
84 * category might give different meanings to the same numerical values.
85 */
74 class error_category 86 class error_category
75 { 87 {
76 public: 88 public:
77 constexpr error_category() noexcept = default; 89 constexpr error_category() noexcept = default;
78 90
129 operator!=(const error_category& __other) const noexcept 141 operator!=(const error_category& __other) const noexcept
130 { return this != &__other; } 142 { return this != &__other; }
131 }; 143 };
132 144
133 // DR 890. 145 // DR 890.
146
147 /// Error category for `errno` error codes.
148 _GLIBCXX_CONST const error_category& generic_category() noexcept;
149
150 /// Error category for other error codes defined by the OS.
134 _GLIBCXX_CONST const error_category& system_category() noexcept; 151 _GLIBCXX_CONST const error_category& system_category() noexcept;
135 _GLIBCXX_CONST const error_category& generic_category() noexcept;
136 152
137 } // end inline namespace 153 } // end inline namespace
138 154
139 error_code make_error_code(errc) noexcept; 155 error_code make_error_code(errc) noexcept;
140 156
141 template<typename _Tp> 157 /** Class error_code
142 struct hash; 158 *
143 159 * This class is a value type storing an integer error number and a
144 /// error_code 160 * category that gives meaning to the error number. Typically this is done
145 // Implementation-specific error identification 161 * close the the point where the error happens, to capture the original
162 * error value.
163 *
164 * An `error_code` object can be used to store the original error value
165 * emitted by some subsystem, with a category relevant to the subsystem.
166 * For example, errors from POSIX library functions can be represented by
167 * an `errno` value and the "generic" category, but errors from an HTTP
168 * library might be represented by an HTTP response status code (e.g. 404)
169 * and a custom category defined by the library.
170 */
146 struct error_code 171 struct error_code
147 { 172 {
148 error_code() noexcept 173 error_code() noexcept
149 : _M_value(0), _M_cat(&system_category()) { } 174 : _M_value(0), _M_cat(&system_category()) { }
150 175
191 explicit operator bool() const noexcept 216 explicit operator bool() const noexcept
192 { return _M_value != 0; } 217 { return _M_value != 0; }
193 218
194 // DR 804. 219 // DR 804.
195 private: 220 private:
196 friend class hash<error_code>;
197
198 int _M_value; 221 int _M_value;
199 const error_category* _M_cat; 222 const error_category* _M_cat;
200 }; 223 };
201 224
202 // 19.4.2.6 non-member functions 225 // 19.4.2.6 non-member functions
226
227 /// @relates error_code @{
228
203 inline error_code 229 inline error_code
204 make_error_code(errc __e) noexcept 230 make_error_code(errc __e) noexcept
205 { return error_code(static_cast<int>(__e), generic_category()); } 231 { return error_code(static_cast<int>(__e), generic_category()); }
206 232
207 inline bool 233 inline bool
215 template<typename _CharT, typename _Traits> 241 template<typename _CharT, typename _Traits>
216 basic_ostream<_CharT, _Traits>& 242 basic_ostream<_CharT, _Traits>&
217 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e) 243 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
218 { return (__os << __e.category().name() << ':' << __e.value()); } 244 { return (__os << __e.category().name() << ':' << __e.value()); }
219 245
246 // @}
247
220 error_condition make_error_condition(errc) noexcept; 248 error_condition make_error_condition(errc) noexcept;
221 249
222 /// error_condition 250 /** Class error_condition
223 // Portable error identification 251 *
252 * This class represents error conditions that may be visible at an API
253 * boundary. Different `error_code` values that can occur within a library
254 * or module might map to the same `error_condition`.
255 *
256 * An `error_condition` represents something that the program can test for,
257 * and subsequently take appropriate action.
258 */
224 struct error_condition 259 struct error_condition
225 { 260 {
226 error_condition() noexcept 261 error_condition() noexcept
227 : _M_value(0), _M_cat(&generic_category()) { } 262 : _M_value(0), _M_cat(&generic_category()) { }
228 263
272 int _M_value; 307 int _M_value;
273 const error_category* _M_cat; 308 const error_category* _M_cat;
274 }; 309 };
275 310
276 // 19.4.3.6 non-member functions 311 // 19.4.3.6 non-member functions
312
313 /// Create an `error_condition` representing a standard `errc` condition.
314 /// @relates error_condition
277 inline error_condition 315 inline error_condition
278 make_error_condition(errc __e) noexcept 316 make_error_condition(errc __e) noexcept
279 { return error_condition(static_cast<int>(__e), generic_category()); } 317 { return error_condition(static_cast<int>(__e), generic_category()); }
280 318
319 /// Define an ordering for error_condition objects.
320 /// @relates error_condition
281 inline bool 321 inline bool
282 operator<(const error_condition& __lhs, 322 operator<(const error_condition& __lhs,
283 const error_condition& __rhs) noexcept 323 const error_condition& __rhs) noexcept
284 { 324 {
285 return (__lhs.category() < __rhs.category() 325 return (__lhs.category() < __rhs.category()
286 || (__lhs.category() == __rhs.category() 326 || (__lhs.category() == __rhs.category()
287 && __lhs.value() < __rhs.value())); 327 && __lhs.value() < __rhs.value()));
288 } 328 }
289 329
290 // 19.4.4 Comparison operators 330 // 19.4.4 Comparison operators
331
332 /// @relates error_code
291 inline bool 333 inline bool
292 operator==(const error_code& __lhs, const error_code& __rhs) noexcept 334 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
293 { return (__lhs.category() == __rhs.category() 335 { return (__lhs.category() == __rhs.category()
294 && __lhs.value() == __rhs.value()); } 336 && __lhs.value() == __rhs.value()); }
295 337
338 /// @relates error_code
339 /// @relates error_condition
296 inline bool 340 inline bool
297 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept 341 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
298 { 342 {
299 return (__lhs.category().equivalent(__lhs.value(), __rhs) 343 return (__lhs.category().equivalent(__lhs.value(), __rhs)
300 || __rhs.category().equivalent(__lhs, __rhs.value())); 344 || __rhs.category().equivalent(__lhs, __rhs.value()));
301 } 345 }
302 346
347 /// @relates error_code
348 /// @relates error_condition
303 inline bool 349 inline bool
304 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept 350 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
305 { 351 {
306 return (__rhs.category().equivalent(__rhs.value(), __lhs) 352 return (__rhs.category().equivalent(__rhs.value(), __lhs)
307 || __lhs.category().equivalent(__rhs, __lhs.value())); 353 || __lhs.category().equivalent(__rhs, __lhs.value()));
308 } 354 }
309 355
356 /// @relates error_condition
310 inline bool 357 inline bool
311 operator==(const error_condition& __lhs, 358 operator==(const error_condition& __lhs,
312 const error_condition& __rhs) noexcept 359 const error_condition& __rhs) noexcept
313 { 360 {
314 return (__lhs.category() == __rhs.category() 361 return (__lhs.category() == __rhs.category()
315 && __lhs.value() == __rhs.value()); 362 && __lhs.value() == __rhs.value());
316 } 363 }
317 364
365 /// @relates error_code
318 inline bool 366 inline bool
319 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept 367 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
320 { return !(__lhs == __rhs); } 368 { return !(__lhs == __rhs); }
321 369
370 /// @relates error_code
371 /// @relates error_condition
322 inline bool 372 inline bool
323 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept 373 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
324 { return !(__lhs == __rhs); } 374 { return !(__lhs == __rhs); }
325 375
376 /// @relates error_code
377 /// @relates error_condition
326 inline bool 378 inline bool
327 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept 379 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
328 { return !(__lhs == __rhs); } 380 { return !(__lhs == __rhs); }
329 381
382 /// @relates error_condition
330 inline bool 383 inline bool
331 operator!=(const error_condition& __lhs, 384 operator!=(const error_condition& __lhs,
332 const error_condition& __rhs) noexcept 385 const error_condition& __rhs) noexcept
333 { return !(__lhs == __rhs); } 386 { return !(__lhs == __rhs); }
334 387
335 388
336 /** 389 /**
337 * @brief Thrown to indicate error code of underlying system. 390 * @brief An exception type that includes an `error_code` value.
338 * 391 *
339 * @ingroup exceptions 392 * Typically used to report errors from the operating system and other
393 * low-level APIs.
394 *
395 * @ingroup exceptions
340 */ 396 */
341 class system_error : public std::runtime_error 397 class system_error : public std::runtime_error
342 { 398 {
343 private: 399 private:
344 error_code _M_code; 400 error_code _M_code;
385 _GLIBCXX_BEGIN_NAMESPACE_VERSION 441 _GLIBCXX_BEGIN_NAMESPACE_VERSION
386 442
387 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X 443 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
388 // DR 1182. 444 // DR 1182.
389 /// std::hash specialization for error_code. 445 /// std::hash specialization for error_code.
446 /// @relates error_code
390 template<> 447 template<>
391 struct hash<error_code> 448 struct hash<error_code>
392 : public __hash_base<size_t, error_code> 449 : public __hash_base<size_t, error_code>
393 { 450 {
394 size_t 451 size_t
395 operator()(const error_code& __e) const noexcept 452 operator()(const error_code& __e) const noexcept
396 { 453 {
397 const size_t __tmp = std::_Hash_impl::hash(__e._M_value); 454 const size_t __tmp = std::_Hash_impl::hash(__e.value());
398 return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp); 455 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
399 } 456 }
400 }; 457 };
401 #endif // _GLIBCXX_COMPATIBILITY_CXX0X 458 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
402 459
403 #if __cplusplus > 201402L 460 #if __cplusplus >= 201703L
404 // DR 2686. 461 // DR 2686.
405 /// std::hash specialization for error_condition. 462 /// std::hash specialization for error_condition.
463 /// @relates error_condition
406 template<> 464 template<>
407 struct hash<error_condition> 465 struct hash<error_condition>
408 : public __hash_base<size_t, error_condition> 466 : public __hash_base<size_t, error_condition>
409 { 467 {
410 size_t 468 size_t
411 operator()(const error_condition& __e) const noexcept 469 operator()(const error_condition& __e) const noexcept
412 { 470 {
413 const size_t __tmp = std::_Hash_impl::hash(__e.value()); 471 const size_t __tmp = std::_Hash_impl::hash(__e.value());
414 return std::_Hash_impl::__hash_combine(__e.category(), __tmp); 472 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
415 } 473 }
416 }; 474 };
417 #endif 475 #endif
418 476
419 _GLIBCXX_END_NAMESPACE_VERSION 477 _GLIBCXX_END_NAMESPACE_VERSION