1 // <system_error> -*- C++ -*-
3 // Copyright (C) 2007, 2008 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file system_error
31 * This is a Standard C++ Library header.
34 #ifndef _GLIBCXX_SYSTEM_ERROR
35 #define _GLIBCXX_SYSTEM_ERROR 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
43 #include <bits/c++config.h>
44 #include <bits/error_constants.h>
48 _GLIBCXX_BEGIN_NAMESPACE(std)
51 class error_condition;
55 /// is_error_code_enum
56 template<typename _Tp>
57 struct is_error_code_enum : public false_type { };
60 struct is_error_code_enum<errc>
61 : public true_type { };
63 /// is_error_condition_enum
64 template<typename _Tp>
65 struct is_error_condition_enum : public false_type { };
68 struct is_error_condition_enum<errc>
69 : public true_type { };
76 error_category() = default;
79 virtual ~error_category() { }
81 error_category(const error_category&) = delete;
82 error_category& operator=(const error_category&) = delete;
88 message(int) const = 0;
90 virtual error_condition
91 default_error_condition(int __i) const;
94 equivalent(int __i, const error_condition& __cond) const;
97 equivalent(const error_code& __code, int __i) const;
100 operator<(const error_category& __other) const
101 { return less<const error_category*>()(this, &__other); }
104 operator==(const error_category& __other) const
105 { return this == &__other; }
108 operator!=(const error_category& __other) const
109 { return this != &__other; }
113 extern const error_category& system_category;
114 extern const error_category& generic_category;
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 : _M_value(static_cast<int>(__e)), _M_cat(&generic_category)
133 assign(int __v, const error_category& __cat)
141 { assign(0, system_category); }
144 template<typename _ErrorCodeEnum>
145 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
147 operator=(_ErrorCodeEnum __e)
149 assign(static_cast<int>(__e), generic_category);
154 value() const { return _M_value; }
156 const error_category&
157 category() const { return *_M_cat; }
160 default_error_condition() const;
164 { return category().message(value()); }
167 // explicit operator bool() const throw()
168 // { return _M_value != 0; }
169 typedef void (*__bool_type)();
171 static void __not_bool_type() { }
173 operator __bool_type() const
174 { return _M_value != 0 ? &__not_bool_type : false; }
179 const error_category* _M_cat;
182 // 19.4.2.6 non-member functions
184 make_error_code(errc __e)
185 { return error_code(static_cast<int>(__e), generic_category); }
188 operator<(const error_code& __lhs, const error_code& __rhs)
190 return (__lhs.category() < __rhs.category()
191 || (__lhs.category() == __rhs.category()
192 && __lhs.value() < __rhs.value()));
195 template<typename _CharT, typename _Traits>
196 basic_ostream<_CharT, _Traits>&
197 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
198 { return (__os << __e.category().name() << ':' << __e.value()); }
202 // Portable error identification
203 struct error_condition
205 error_condition() : _M_value(0), _M_cat(&generic_category) { }
207 error_condition(int __v, const error_category& __cat)
208 : _M_value(__v), _M_cat(&__cat) { }
210 template<typename _ErrorConditionEnum>
211 error_condition(_ErrorConditionEnum __e,
212 typename enable_if<is_error_condition_enum
213 <_ErrorConditionEnum>::value>::type* = 0)
214 : _M_value(static_cast<int>(__e)), _M_cat(&generic_category) { }
217 assign(int __v, const error_category& __cat)
224 template<typename _ErrorConditionEnum>
225 typename enable_if<is_error_condition_enum
226 <_ErrorConditionEnum>::value, error_condition&>::type
227 operator=(_ErrorConditionEnum __e)
229 assign(static_cast<int>(__e), generic_category);
235 { assign(0, generic_category); }
237 // 19.4.3.4 observers
239 value() const { return _M_value; }
241 const error_category&
242 category() const { return *_M_cat; }
246 { return category().message(value()); }
249 // explicit operator bool() const throw()
250 // { return _M_value != 0; }
251 typedef void (*__bool_type)();
253 static void __not_bool_type() { }
255 operator __bool_type() const
256 { return _M_value != 0 ? &__not_bool_type : false; }
261 const error_category* _M_cat;
264 // 19.4.3.6 non-member functions
265 inline error_condition
266 make_error_condition(errc __e)
267 { return error_condition(static_cast<int>(__e), generic_category); }
270 operator<(const error_condition& __lhs, const error_condition& __rhs)
272 return (__lhs.category() < __rhs.category()
273 || (__lhs.category() == __rhs.category()
274 && __lhs.value() < __rhs.value()));
277 // 19.4.4 Comparison operators
279 operator==(const error_code& __lhs, const error_code& __rhs)
280 { return (__lhs.category() == __rhs.category()
281 && __lhs.value() == __rhs.value()); }
284 operator==(const error_code& __lhs, const error_condition& __rhs)
286 return (__lhs.category().equivalent(__lhs.value(), __rhs)
287 || __rhs.category().equivalent(__lhs, __rhs.value()));
291 operator==(const error_condition& __lhs, const error_code& __rhs)
293 return (__rhs.category().equivalent(__rhs.value(), __lhs)
294 || __lhs.category().equivalent(__rhs, __lhs.value()));
298 operator==(const error_condition& __lhs, const error_condition& __rhs)
300 return (__lhs.category() == __rhs.category()
301 && __lhs.value() == __rhs.value());
305 operator!=(const error_code& __lhs, const error_code& __rhs)
306 { return !(__lhs == __rhs); }
309 operator!=(const error_code& __lhs, const error_condition& __rhs)
310 { return !(__lhs == __rhs); }
313 operator!=(const error_condition& __lhs, const error_code& __rhs)
314 { return !(__lhs == __rhs); }
317 operator!=(const error_condition& __lhs, const error_condition& __rhs)
318 { return !(__lhs == __rhs); }
321 /// Thrown to indicate error code of underlying system.
322 class system_error : public std::runtime_error
328 system_error(error_code __ec = error_code())
329 : runtime_error(""), _M_code(__ec) { }
331 system_error(error_code __ec, const string& __what)
332 : runtime_error(__what), _M_code(__ec) { }
335 * TODO: Add const char* ctors to all exceptions.
337 * system_error(error_code __ec, const char* __what)
338 * : runtime_error(__what), _M_code(__ec) { }
340 * system_error(int __v, const error_category& __ecat, const char* __what)
341 * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
344 system_error(int __v, const error_category& __ecat)
345 : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
347 system_error(int __v, const error_category& __ecat, const string& __what)
348 : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
350 virtual ~system_error() throw();
353 code() const throw() { return _M_code; }
356 _GLIBCXX_END_NAMESPACE
358 #endif // __GXX_EXPERIMENTAL_CXX0X__
360 #endif // _GLIBCXX_SYSTEM_ERROR