1 // <system_error> -*- C++ -*-
3 // Copyright (C) 2007-2016 Free Software Foundation, Inc.
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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/system_error
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_SYSTEM_ERROR
30 #define _GLIBCXX_SYSTEM_ERROR 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
38 #include <bits/c++config.h>
39 #include <bits/error_constants.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 class error_condition;
51 /// is_error_code_enum
52 template<typename _Tp>
53 struct is_error_code_enum : public false_type { };
55 /// is_error_condition_enum
56 template<typename _Tp>
57 struct is_error_condition_enum : public false_type { };
60 struct is_error_condition_enum<errc>
61 : public true_type { };
63 #if __cplusplus > 201402L
64 template <typename _Tp>
65 constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
66 template <typename _Tp>
67 constexpr bool is_error_condition_enum_v =
68 is_error_condition_enum<_Tp>::value;
70 inline namespace _V2 {
76 constexpr error_category() noexcept = default;
78 virtual ~error_category();
80 error_category(const error_category&) = delete;
81 error_category& operator=(const error_category&) = delete;
84 name() const noexcept = 0;
86 // We need two different virtual functions here, one returning a
87 // COW string and one returning an SSO string. Their positions in the
88 // vtable must be consistent for dynamic dispatch to work, but which one
89 // the name "message()" finds depends on which ABI the caller is using.
90 #if _GLIBCXX_USE_CXX11_ABI
92 _GLIBCXX_DEFAULT_ABI_TAG
94 _M_message(int) const;
97 _GLIBCXX_DEFAULT_ABI_TAG
99 message(int) const = 0;
102 message(int) const = 0;
106 _M_message(int) const;
110 virtual error_condition
111 default_error_condition(int __i) const noexcept;
114 equivalent(int __i, const error_condition& __cond) const noexcept;
117 equivalent(const error_code& __code, int __i) const noexcept;
120 operator<(const error_category& __other) const noexcept
121 { return less<const error_category*>()(this, &__other); }
124 operator==(const error_category& __other) const noexcept
125 { return this == &__other; }
128 operator!=(const error_category& __other) const noexcept
129 { return this != &__other; }
133 _GLIBCXX_CONST const error_category& system_category() noexcept;
134 _GLIBCXX_CONST const error_category& generic_category() noexcept;
136 } // end inline namespace
138 error_code make_error_code(errc) noexcept;
140 template<typename _Tp>
144 // Implementation-specific error identification
147 error_code() noexcept
148 : _M_value(0), _M_cat(&system_category()) { }
150 error_code(int __v, const error_category& __cat) noexcept
151 : _M_value(__v), _M_cat(&__cat) { }
153 template<typename _ErrorCodeEnum, typename = typename
154 enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
155 error_code(_ErrorCodeEnum __e) noexcept
156 { *this = make_error_code(__e); }
159 assign(int __v, const error_category& __cat) noexcept
167 { assign(0, system_category()); }
170 template<typename _ErrorCodeEnum>
171 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
173 operator=(_ErrorCodeEnum __e) noexcept
174 { return *this = make_error_code(__e); }
177 value() const noexcept { return _M_value; }
179 const error_category&
180 category() const noexcept { return *_M_cat; }
183 default_error_condition() const noexcept;
185 _GLIBCXX_DEFAULT_ABI_TAG
188 { return category().message(value()); }
190 explicit operator bool() const noexcept
191 { return _M_value != 0; }
195 friend class hash<error_code>;
198 const error_category* _M_cat;
201 // 19.4.2.6 non-member functions
203 make_error_code(errc __e) noexcept
204 { return error_code(static_cast<int>(__e), generic_category()); }
207 operator<(const error_code& __lhs, const error_code& __rhs) noexcept
209 return (__lhs.category() < __rhs.category()
210 || (__lhs.category() == __rhs.category()
211 && __lhs.value() < __rhs.value()));
214 template<typename _CharT, typename _Traits>
215 basic_ostream<_CharT, _Traits>&
216 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
217 { return (__os << __e.category().name() << ':' << __e.value()); }
219 error_condition make_error_condition(errc) noexcept;
222 // Portable error identification
223 struct error_condition
225 error_condition() noexcept
226 : _M_value(0), _M_cat(&generic_category()) { }
228 error_condition(int __v, const error_category& __cat) noexcept
229 : _M_value(__v), _M_cat(&__cat) { }
231 template<typename _ErrorConditionEnum, typename = typename
232 enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
233 error_condition(_ErrorConditionEnum __e) noexcept
234 { *this = make_error_condition(__e); }
237 assign(int __v, const error_category& __cat) noexcept
244 template<typename _ErrorConditionEnum>
245 typename enable_if<is_error_condition_enum
246 <_ErrorConditionEnum>::value, error_condition&>::type
247 operator=(_ErrorConditionEnum __e) noexcept
248 { return *this = make_error_condition(__e); }
252 { assign(0, generic_category()); }
254 // 19.4.3.4 observers
256 value() const noexcept { return _M_value; }
258 const error_category&
259 category() const noexcept { return *_M_cat; }
261 _GLIBCXX_DEFAULT_ABI_TAG
264 { return category().message(value()); }
266 explicit operator bool() const noexcept
267 { return _M_value != 0; }
272 const error_category* _M_cat;
275 // 19.4.3.6 non-member functions
276 inline error_condition
277 make_error_condition(errc __e) noexcept
278 { return error_condition(static_cast<int>(__e), generic_category()); }
281 operator<(const error_condition& __lhs,
282 const error_condition& __rhs) noexcept
284 return (__lhs.category() < __rhs.category()
285 || (__lhs.category() == __rhs.category()
286 && __lhs.value() < __rhs.value()));
289 // 19.4.4 Comparison operators
291 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
292 { return (__lhs.category() == __rhs.category()
293 && __lhs.value() == __rhs.value()); }
296 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
298 return (__lhs.category().equivalent(__lhs.value(), __rhs)
299 || __rhs.category().equivalent(__lhs, __rhs.value()));
303 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
305 return (__rhs.category().equivalent(__rhs.value(), __lhs)
306 || __lhs.category().equivalent(__rhs, __lhs.value()));
310 operator==(const error_condition& __lhs,
311 const error_condition& __rhs) noexcept
313 return (__lhs.category() == __rhs.category()
314 && __lhs.value() == __rhs.value());
318 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
319 { return !(__lhs == __rhs); }
322 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
323 { return !(__lhs == __rhs); }
326 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
327 { return !(__lhs == __rhs); }
330 operator!=(const error_condition& __lhs,
331 const error_condition& __rhs) noexcept
332 { return !(__lhs == __rhs); }
336 * @brief Thrown to indicate error code of underlying system.
338 * @ingroup exceptions
340 class system_error : public std::runtime_error
346 system_error(error_code __ec = error_code())
347 : runtime_error(__ec.message()), _M_code(__ec) { }
349 system_error(error_code __ec, const string& __what)
350 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
352 system_error(error_code __ec, const char* __what)
353 : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
355 system_error(int __v, const error_category& __ecat, const char* __what)
356 : system_error(error_code(__v, __ecat), __what) { }
358 system_error(int __v, const error_category& __ecat)
359 : runtime_error(error_code(__v, __ecat).message()),
360 _M_code(__v, __ecat) { }
362 system_error(int __v, const error_category& __ecat, const string& __what)
363 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
364 _M_code(__v, __ecat) { }
366 virtual ~system_error() noexcept;
369 code() const noexcept { return _M_code; }
372 _GLIBCXX_END_NAMESPACE_VERSION
375 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
377 #include <bits/functional_hash.h>
379 namespace std _GLIBCXX_VISIBILITY(default)
381 _GLIBCXX_BEGIN_NAMESPACE_VERSION
384 /// std::hash specialization for error_code.
386 struct hash<error_code>
387 : public __hash_base<size_t, error_code>
390 operator()(const error_code& __e) const noexcept
392 const size_t __tmp = std::_Hash_impl::hash(__e._M_value);
393 return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp);
397 _GLIBCXX_END_NAMESPACE_VERSION
400 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
404 #endif // _GLIBCXX_SYSTEM_ERROR