1 // <system_error> -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
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;
52 /// is_error_code_enum
53 template<typename _Tp>
54 struct is_error_code_enum : public false_type { };
56 /// is_error_condition_enum
57 template<typename _Tp>
58 struct is_error_condition_enum : public false_type { };
61 struct is_error_condition_enum<errc>
62 : public true_type { };
72 virtual ~error_category() { }
74 error_category(const error_category&) = delete;
75 error_category& operator=(const error_category&) = delete;
81 message(int) const = 0;
83 virtual error_condition
84 default_error_condition(int __i) const;
87 equivalent(int __i, const error_condition& __cond) const;
90 equivalent(const error_code& __code, int __i) const;
93 operator<(const error_category& __other) const
94 { return less<const error_category*>()(this, &__other); }
97 operator==(const error_category& __other) const
98 { return this == &__other; }
101 operator!=(const error_category& __other) const
102 { return this != &__other; }
105 inline error_category::error_category() = default;
108 _GLIBCXX_CONST const error_category& system_category() throw();
109 _GLIBCXX_CONST const error_category& generic_category() throw();
111 error_code make_error_code(errc);
113 template<typename _Tp>
117 // Implementation-specific error identification
121 : _M_value(0), _M_cat(&system_category()) { }
123 error_code(int __v, const error_category& __cat)
124 : _M_value(__v), _M_cat(&__cat) { }
126 template<typename _ErrorCodeEnum>
127 error_code(_ErrorCodeEnum __e,
128 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type* = 0)
129 { *this = make_error_code(__e); }
132 assign(int __v, const error_category& __cat)
140 { assign(0, system_category()); }
143 template<typename _ErrorCodeEnum>
144 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
146 operator=(_ErrorCodeEnum __e)
147 { return *this = make_error_code(__e); }
150 value() const { return _M_value; }
152 const error_category&
153 category() const { return *_M_cat; }
156 default_error_condition() const;
160 { return category().message(value()); }
162 explicit operator bool() const
163 { return _M_value != 0 ? true : false; }
167 friend class hash<error_code>;
170 const error_category* _M_cat;
173 // 19.4.2.6 non-member functions
175 make_error_code(errc __e)
176 { return error_code(static_cast<int>(__e), generic_category()); }
179 operator<(const error_code& __lhs, const error_code& __rhs)
181 return (__lhs.category() < __rhs.category()
182 || (__lhs.category() == __rhs.category()
183 && __lhs.value() < __rhs.value()));
186 template<typename _CharT, typename _Traits>
187 basic_ostream<_CharT, _Traits>&
188 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
189 { return (__os << __e.category().name() << ':' << __e.value()); }
191 error_condition make_error_condition(errc);
194 // Portable error identification
195 struct error_condition
198 : _M_value(0), _M_cat(&generic_category()) { }
200 error_condition(int __v, const error_category& __cat)
201 : _M_value(__v), _M_cat(&__cat) { }
203 template<typename _ErrorConditionEnum>
204 error_condition(_ErrorConditionEnum __e,
205 typename enable_if<is_error_condition_enum
206 <_ErrorConditionEnum>::value>::type* = 0)
207 { *this = make_error_condition(__e); }
210 assign(int __v, const error_category& __cat)
217 template<typename _ErrorConditionEnum>
218 typename enable_if<is_error_condition_enum
219 <_ErrorConditionEnum>::value, error_condition&>::type
220 operator=(_ErrorConditionEnum __e)
221 { return *this = make_error_condition(__e); }
225 { assign(0, generic_category()); }
227 // 19.4.3.4 observers
229 value() const { return _M_value; }
231 const error_category&
232 category() const { return *_M_cat; }
236 { return category().message(value()); }
238 explicit operator bool() const
239 { return _M_value != 0 ? true : false; }
244 const error_category* _M_cat;
247 // 19.4.3.6 non-member functions
248 inline error_condition
249 make_error_condition(errc __e)
250 { return error_condition(static_cast<int>(__e), generic_category()); }
253 operator<(const error_condition& __lhs, const error_condition& __rhs)
255 return (__lhs.category() < __rhs.category()
256 || (__lhs.category() == __rhs.category()
257 && __lhs.value() < __rhs.value()));
260 // 19.4.4 Comparison operators
262 operator==(const error_code& __lhs, const error_code& __rhs)
263 { return (__lhs.category() == __rhs.category()
264 && __lhs.value() == __rhs.value()); }
267 operator==(const error_code& __lhs, const error_condition& __rhs)
269 return (__lhs.category().equivalent(__lhs.value(), __rhs)
270 || __rhs.category().equivalent(__lhs, __rhs.value()));
274 operator==(const error_condition& __lhs, const error_code& __rhs)
276 return (__rhs.category().equivalent(__rhs.value(), __lhs)
277 || __lhs.category().equivalent(__rhs, __lhs.value()));
281 operator==(const error_condition& __lhs, const error_condition& __rhs)
283 return (__lhs.category() == __rhs.category()
284 && __lhs.value() == __rhs.value());
288 operator!=(const error_code& __lhs, const error_code& __rhs)
289 { return !(__lhs == __rhs); }
292 operator!=(const error_code& __lhs, const error_condition& __rhs)
293 { return !(__lhs == __rhs); }
296 operator!=(const error_condition& __lhs, const error_code& __rhs)
297 { return !(__lhs == __rhs); }
300 operator!=(const error_condition& __lhs, const error_condition& __rhs)
301 { return !(__lhs == __rhs); }
305 * @brief Thrown to indicate error code of underlying system.
307 * @ingroup exceptions
309 class system_error : public std::runtime_error
315 system_error(error_code __ec = error_code())
316 : runtime_error(__ec.message()), _M_code(__ec) { }
318 system_error(error_code __ec, const string& __what)
319 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
322 * TODO: Add const char* ctors to all exceptions.
324 * system_error(error_code __ec, const char* __what)
325 * : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
327 * system_error(int __v, const error_category& __ecat, const char* __what)
328 * : runtime_error(__what + (": " + __ec.message())),
329 * _M_code(error_code(__v, __ecat)) { }
332 system_error(int __v, const error_category& __ecat)
333 : runtime_error(error_code(__v, __ecat).message()),
334 _M_code(__v, __ecat) { }
336 system_error(int __v, const error_category& __ecat, const string& __what)
337 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
338 _M_code(__v, __ecat) { }
340 virtual ~system_error() throw();
343 code() const throw() { return _M_code; }
346 _GLIBCXX_END_NAMESPACE_VERSION
349 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
351 #include <bits/functional_hash.h>
353 namespace std _GLIBCXX_VISIBILITY(default)
355 _GLIBCXX_BEGIN_NAMESPACE_VERSION
358 /// std::hash specialization for error_code.
360 struct hash<error_code>
361 : public __hash_base<size_t, error_code>
364 operator()(const error_code& __e) const
366 const size_t __tmp = std::_Hash_impl::hash(__e._M_value);
367 return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp);
371 _GLIBCXX_END_NAMESPACE_VERSION
374 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
376 #endif // __GXX_EXPERIMENTAL_CXX0X__
378 #endif // _GLIBCXX_SYSTEM_ERROR