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<posix_error::posix_errno>
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<posix_error::posix_errno>
69 : public true_type { };
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 error_category(const error_category&);
108 operator=(const error_category&);
111 const error_category& get_posix_category();
112 const error_category& get_system_category();
114 static const error_category& posix_category = get_posix_category();
115 static const error_category& system_category = get_system_category();
118 // Implementation-specific error identification
122 : _M_value(0), _M_cat(&system_category) { }
124 error_code(int __v, const error_category& __cat)
125 : _M_value(__v), _M_cat(&__cat) { }
127 template<typename _ErrorCodeEnum>
128 error_code(_ErrorCodeEnum __e,
129 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type* = 0)
130 : _M_value(__e), _M_cat(&posix_category)
134 assign(int __v, const error_category& __cat)
144 _M_cat = &system_category;
148 template<typename _ErrorCodeEnum>
149 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
151 operator=(_ErrorCodeEnum __e)
154 _M_cat = &posix_category;
159 value() const { return _M_value; }
161 const error_category&
162 category() const { return *_M_cat; }
165 default_error_condition() const;
169 { return category().message(value()); }
172 // explicit operator bool() const throw()
173 // { return _M_value != 0; }
174 typedef void (*__bool_type)();
176 static void __not_bool_type() { }
178 operator __bool_type() const
179 { return _M_value != 0 ? &__not_bool_type : false; }
184 const error_category* _M_cat;
187 // 19.4.2.5 non-member functions
189 operator<(const error_code& __lhs, const error_code& __rhs)
191 return (__lhs.category() < __rhs.category()
192 || (__lhs.category() == __rhs.category()
193 && __lhs.value() < __rhs.value()));
196 template<typename _CharT, typename _Traits>
197 basic_ostream<_CharT, _Traits>&
198 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
199 { return (__os << __e.category().name() << ':' << __e.value()); }
203 // Portable error identification
204 struct error_condition
206 error_condition() : _M_value(0), _M_cat(&posix_category) { }
208 error_condition(int __v, const error_category& __cat)
209 : _M_value(__v), _M_cat(&__cat) { }
211 template<typename _ErrorConditionEnum>
212 error_condition(_ErrorConditionEnum __e,
213 typename enable_if<is_error_condition_enum
214 <_ErrorConditionEnum>::value>::type* = 0)
215 : _M_value(__e), _M_cat(&posix_category) { }
218 assign(int __v, const error_category& __cat)
225 template<typename _ErrorConditionEnum>
226 typename enable_if<is_error_condition_enum
227 <_ErrorConditionEnum>::value, error_condition&>::type
228 operator=(_ErrorConditionEnum __e)
231 _M_cat = &posix_category;
239 _M_cat = &posix_category;
242 // 19.4.3.4 observers
244 value() const { return _M_value; }
246 const error_category&
247 category() const { return *_M_cat; }
251 { return category().message(value()); }
254 // explicit operator bool() const throw()
255 // { return _M_value != 0; }
256 typedef void (*__bool_type)();
258 static void __not_bool_type() { }
260 operator __bool_type() const
261 { return _M_value != 0 ? &__not_bool_type : false; }
266 const error_category* _M_cat;
269 // 19.4.3.5 non-member functions
271 operator<(const error_condition& __lhs, const error_condition& __rhs)
273 return (__lhs.category() < __rhs.category()
274 || (__lhs.category() == __rhs.category()
275 && __lhs.value() < __rhs.value()));
278 namespace posix_error
281 make_error_code(posix_errno __e)
282 { return error_code(__e, posix_category); }
284 inline error_condition
285 make_error_condition(posix_errno __e)
286 { return error_condition(__e, posix_category); }
289 // 19.4.4 Comparison operators
291 operator==(const error_code& __lhs, const error_code& __rhs)
292 { return (__lhs.category() == __rhs.category()
293 && __lhs.value() == __rhs.value()); }
296 operator==(const error_code& __lhs, const error_condition& __rhs)
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)
305 return (__rhs.category().equivalent(__rhs.value(), __lhs)
306 || __lhs.category().equivalent(__rhs, __lhs.value()));
310 operator==(const error_condition& __lhs, const error_condition& __rhs)
312 return (__lhs.category() == __rhs.category()
313 && __lhs.value() == __rhs.value());
317 operator!=(const error_code& __lhs, const error_code& __rhs)
318 { return !(__lhs == __rhs); }
321 operator!=(const error_code& __lhs, const error_condition& __rhs)
322 { return !(__lhs == __rhs); }
325 operator!=(const error_condition& __lhs, const error_code& __rhs)
326 { return !(__lhs == __rhs); }
329 operator!=(const error_condition& __lhs, const error_condition& __rhs)
330 { return !(__lhs == __rhs); }
333 /// Thrown to indicate error code of underlying system.
334 class system_error : public std::runtime_error
340 system_error(error_code __ec = error_code())
341 : runtime_error(""), _M_code(__ec) { }
343 system_error(error_code __ec, const string& __what)
344 : runtime_error(__what), _M_code(__ec) { }
346 system_error(int __v, const error_category& __ecat)
347 : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
349 system_error(int __v, const error_category& __ecat, const string& __what)
350 : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
352 virtual ~system_error() throw();
355 code() const throw() { return _M_code; }
358 _GLIBCXX_END_NAMESPACE
360 #endif // __GXX_EXPERIMENTAL_CXX0X__
362 #endif // _GLIBCXX_SYSTEM_ERROR