1 // <system_error> -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 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 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 <c++0x_warning.h>
38 #include <bits/c++config.h>
39 #include <bits/error_constants.h>
43 _GLIBCXX_BEGIN_NAMESPACE(std)
46 class error_condition;
50 /// is_error_code_enum
51 template<typename _Tp>
52 struct is_error_code_enum : public false_type { };
55 struct is_error_code_enum<errc>
56 : public true_type { };
58 /// is_error_condition_enum
59 template<typename _Tp>
60 struct is_error_condition_enum : public false_type { };
63 struct is_error_condition_enum<errc>
64 : public true_type { };
71 error_category() = default;
74 virtual ~error_category() { }
76 error_category(const error_category&) = delete;
77 error_category& operator=(const error_category&) = delete;
83 message(int) const = 0;
85 virtual error_condition
86 default_error_condition(int __i) const;
89 equivalent(int __i, const error_condition& __cond) const;
92 equivalent(const error_code& __code, int __i) const;
95 operator<(const error_category& __other) const
96 { return less<const error_category*>()(this, &__other); }
99 operator==(const error_category& __other) const
100 { return this == &__other; }
103 operator!=(const error_category& __other) const
104 { return this != &__other; }
108 _GLIBCXX_CONST const error_category& system_category() throw ();
109 _GLIBCXX_CONST const error_category& generic_category() throw ();
112 // Implementation-specific error identification
116 : _M_value(0), _M_cat(&system_category()) { }
118 error_code(int __v, const error_category& __cat)
119 : _M_value(__v), _M_cat(&__cat) { }
121 template<typename _ErrorCodeEnum>
122 error_code(_ErrorCodeEnum __e,
123 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type* = 0)
124 : _M_value(static_cast<int>(__e)), _M_cat(&generic_category())
128 assign(int __v, const error_category& __cat)
136 { assign(0, system_category()); }
139 template<typename _ErrorCodeEnum>
140 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
142 operator=(_ErrorCodeEnum __e)
144 assign(static_cast<int>(__e), generic_category());
149 value() const { return _M_value; }
151 const error_category&
152 category() const { return *_M_cat; }
155 default_error_condition() const;
159 { return category().message(value()); }
162 // explicit operator bool() const throw()
163 // { return _M_value != 0; }
164 typedef void (*__bool_type)();
166 static void __not_bool_type() { }
168 operator __bool_type() const
169 { return _M_value != 0 ? &__not_bool_type : false; }
174 const error_category* _M_cat;
177 // 19.4.2.6 non-member functions
179 make_error_code(errc __e)
180 { return error_code(static_cast<int>(__e), generic_category()); }
183 operator<(const error_code& __lhs, const error_code& __rhs)
185 return (__lhs.category() < __rhs.category()
186 || (__lhs.category() == __rhs.category()
187 && __lhs.value() < __rhs.value()));
190 template<typename _CharT, typename _Traits>
191 basic_ostream<_CharT, _Traits>&
192 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
193 { return (__os << __e.category().name() << ':' << __e.value()); }
197 // Portable error identification
198 struct error_condition
200 error_condition() : _M_value(0), _M_cat(&generic_category()) { }
202 error_condition(int __v, const error_category& __cat)
203 : _M_value(__v), _M_cat(&__cat) { }
205 template<typename _ErrorConditionEnum>
206 error_condition(_ErrorConditionEnum __e,
207 typename enable_if<is_error_condition_enum
208 <_ErrorConditionEnum>::value>::type* = 0)
209 : _M_value(static_cast<int>(__e)), _M_cat(&generic_category()) { }
212 assign(int __v, const error_category& __cat)
219 template<typename _ErrorConditionEnum>
220 typename enable_if<is_error_condition_enum
221 <_ErrorConditionEnum>::value, error_condition&>::type
222 operator=(_ErrorConditionEnum __e)
224 assign(static_cast<int>(__e), generic_category());
230 { assign(0, generic_category()); }
232 // 19.4.3.4 observers
234 value() const { return _M_value; }
236 const error_category&
237 category() const { return *_M_cat; }
241 { return category().message(value()); }
244 // explicit operator bool() const throw()
245 // { return _M_value != 0; }
246 typedef void (*__bool_type)();
248 static void __not_bool_type() { }
250 operator __bool_type() const
251 { return _M_value != 0 ? &__not_bool_type : false; }
256 const error_category* _M_cat;
259 // 19.4.3.6 non-member functions
260 inline error_condition
261 make_error_condition(errc __e)
262 { return error_condition(static_cast<int>(__e), generic_category()); }
265 operator<(const error_condition& __lhs, const error_condition& __rhs)
267 return (__lhs.category() < __rhs.category()
268 || (__lhs.category() == __rhs.category()
269 && __lhs.value() < __rhs.value()));
272 // 19.4.4 Comparison operators
274 operator==(const error_code& __lhs, const error_code& __rhs)
275 { return (__lhs.category() == __rhs.category()
276 && __lhs.value() == __rhs.value()); }
279 operator==(const error_code& __lhs, const error_condition& __rhs)
281 return (__lhs.category().equivalent(__lhs.value(), __rhs)
282 || __rhs.category().equivalent(__lhs, __rhs.value()));
286 operator==(const error_condition& __lhs, const error_code& __rhs)
288 return (__rhs.category().equivalent(__rhs.value(), __lhs)
289 || __lhs.category().equivalent(__rhs, __lhs.value()));
293 operator==(const error_condition& __lhs, const error_condition& __rhs)
295 return (__lhs.category() == __rhs.category()
296 && __lhs.value() == __rhs.value());
300 operator!=(const error_code& __lhs, const error_code& __rhs)
301 { return !(__lhs == __rhs); }
304 operator!=(const error_code& __lhs, const error_condition& __rhs)
305 { return !(__lhs == __rhs); }
308 operator!=(const error_condition& __lhs, const error_code& __rhs)
309 { return !(__lhs == __rhs); }
312 operator!=(const error_condition& __lhs, const error_condition& __rhs)
313 { return !(__lhs == __rhs); }
317 * @brief Thrown to indicate error code of underlying system.
319 * @ingroup exceptions
321 class system_error : public std::runtime_error
327 system_error(error_code __ec = error_code())
328 : runtime_error(""), _M_code(__ec) { }
330 system_error(error_code __ec, const string& __what)
331 : runtime_error(__what), _M_code(__ec) { }
334 * TODO: Add const char* ctors to all exceptions.
336 * system_error(error_code __ec, const char* __what)
337 * : runtime_error(__what), _M_code(__ec) { }
339 * system_error(int __v, const error_category& __ecat, const char* __what)
340 * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
343 system_error(int __v, const error_category& __ecat)
344 : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
346 system_error(int __v, const error_category& __ecat, const string& __what)
347 : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
349 virtual ~system_error() throw();
352 code() const throw() { return _M_code; }
355 _GLIBCXX_END_NAMESPACE
357 #endif // __GXX_EXPERIMENTAL_CXX0X__
359 #endif // _GLIBCXX_SYSTEM_ERROR