]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/system_error
condition_variable.cc (condition_variable, [...]): Mark throw()
[thirdparty/gcc.git] / libstdc++-v3 / include / std / system_error
1 // <system_error> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /** @file system_error
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_SYSTEM_ERROR
30 #define _GLIBCXX_SYSTEM_ERROR 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <c++0x_warning.h>
36 #else
37
38 #include <bits/c++config.h>
39 #include <bits/error_constants.h>
40 #include <iosfwd>
41 #include <stdexcept>
42
43 _GLIBCXX_BEGIN_NAMESPACE(std)
44
45 class error_code;
46 class error_condition;
47 class error_category;
48 class system_error;
49
50 /// is_error_code_enum
51 template<typename _Tp>
52 struct is_error_code_enum : public false_type { };
53
54 template<>
55 struct is_error_code_enum<errc>
56 : public true_type { };
57
58 /// is_error_condition_enum
59 template<typename _Tp>
60 struct is_error_condition_enum : public false_type { };
61
62 template<>
63 struct is_error_condition_enum<errc>
64 : public true_type { };
65
66
67 /// error_category
68 class error_category
69 {
70 protected:
71 error_category() = default;
72
73 public:
74 virtual ~error_category() { }
75
76 error_category(const error_category&) = delete;
77 error_category& operator=(const error_category&) = delete;
78
79 virtual const char*
80 name() const = 0;
81
82 virtual string
83 message(int) const = 0;
84
85 virtual error_condition
86 default_error_condition(int __i) const;
87
88 virtual bool
89 equivalent(int __i, const error_condition& __cond) const;
90
91 virtual bool
92 equivalent(const error_code& __code, int __i) const;
93
94 bool
95 operator<(const error_category& __other) const
96 { return less<const error_category*>()(this, &__other); }
97
98 bool
99 operator==(const error_category& __other) const
100 { return this == &__other; }
101
102 bool
103 operator!=(const error_category& __other) const
104 { return this != &__other; }
105 };
106
107 // DR 890.
108 _GLIBCXX_CONST const error_category& system_category() throw ();
109 _GLIBCXX_CONST const error_category& generic_category() throw ();
110
111 /// error_code
112 // Implementation-specific error identification
113 struct error_code
114 {
115 error_code()
116 : _M_value(0), _M_cat(&system_category()) { }
117
118 error_code(int __v, const error_category& __cat)
119 : _M_value(__v), _M_cat(&__cat) { }
120
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())
125 { }
126
127 void
128 assign(int __v, const error_category& __cat)
129 {
130 _M_value = __v;
131 _M_cat = &__cat;
132 }
133
134 void
135 clear()
136 { assign(0, system_category()); }
137
138 // DR 804.
139 template<typename _ErrorCodeEnum>
140 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
141 error_code&>::type
142 operator=(_ErrorCodeEnum __e)
143 {
144 assign(static_cast<int>(__e), generic_category());
145 return *this;
146 }
147
148 int
149 value() const { return _M_value; }
150
151 const error_category&
152 category() const { return *_M_cat; }
153
154 error_condition
155 default_error_condition() const;
156
157 string
158 message() const
159 { return category().message(value()); }
160
161 // Safe bool idiom.
162 // explicit operator bool() const throw()
163 // { return _M_value != 0; }
164 typedef void (*__bool_type)();
165
166 static void __not_bool_type() { }
167
168 operator __bool_type() const
169 { return _M_value != 0 ? &__not_bool_type : false; }
170
171 // DR 804.
172 private:
173 int _M_value;
174 const error_category* _M_cat;
175 };
176
177 // 19.4.2.6 non-member functions
178 inline error_code
179 make_error_code(errc __e)
180 { return error_code(static_cast<int>(__e), generic_category()); }
181
182 inline bool
183 operator<(const error_code& __lhs, const error_code& __rhs)
184 {
185 return (__lhs.category() < __rhs.category()
186 || (__lhs.category() == __rhs.category()
187 && __lhs.value() < __rhs.value()));
188 }
189
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()); }
194
195
196 /// error_condition
197 // Portable error identification
198 struct error_condition
199 {
200 error_condition() : _M_value(0), _M_cat(&generic_category()) { }
201
202 error_condition(int __v, const error_category& __cat)
203 : _M_value(__v), _M_cat(&__cat) { }
204
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()) { }
210
211 void
212 assign(int __v, const error_category& __cat)
213 {
214 _M_value = __v;
215 _M_cat = &__cat;
216 }
217
218 // DR 804.
219 template<typename _ErrorConditionEnum>
220 typename enable_if<is_error_condition_enum
221 <_ErrorConditionEnum>::value, error_condition&>::type
222 operator=(_ErrorConditionEnum __e)
223 {
224 assign(static_cast<int>(__e), generic_category());
225 return *this;
226 }
227
228 void
229 clear()
230 { assign(0, generic_category()); }
231
232 // 19.4.3.4 observers
233 int
234 value() const { return _M_value; }
235
236 const error_category&
237 category() const { return *_M_cat; }
238
239 string
240 message() const
241 { return category().message(value()); }
242
243 // Safe bool idiom.
244 // explicit operator bool() const throw()
245 // { return _M_value != 0; }
246 typedef void (*__bool_type)();
247
248 static void __not_bool_type() { }
249
250 operator __bool_type() const
251 { return _M_value != 0 ? &__not_bool_type : false; }
252
253 // DR 804.
254 private:
255 int _M_value;
256 const error_category* _M_cat;
257 };
258
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()); }
263
264 inline bool
265 operator<(const error_condition& __lhs, const error_condition& __rhs)
266 {
267 return (__lhs.category() < __rhs.category()
268 || (__lhs.category() == __rhs.category()
269 && __lhs.value() < __rhs.value()));
270 }
271
272 // 19.4.4 Comparison operators
273 inline bool
274 operator==(const error_code& __lhs, const error_code& __rhs)
275 { return (__lhs.category() == __rhs.category()
276 && __lhs.value() == __rhs.value()); }
277
278 inline bool
279 operator==(const error_code& __lhs, const error_condition& __rhs)
280 {
281 return (__lhs.category().equivalent(__lhs.value(), __rhs)
282 || __rhs.category().equivalent(__lhs, __rhs.value()));
283 }
284
285 inline bool
286 operator==(const error_condition& __lhs, const error_code& __rhs)
287 {
288 return (__rhs.category().equivalent(__rhs.value(), __lhs)
289 || __lhs.category().equivalent(__rhs, __lhs.value()));
290 }
291
292 inline bool
293 operator==(const error_condition& __lhs, const error_condition& __rhs)
294 {
295 return (__lhs.category() == __rhs.category()
296 && __lhs.value() == __rhs.value());
297 }
298
299 inline bool
300 operator!=(const error_code& __lhs, const error_code& __rhs)
301 { return !(__lhs == __rhs); }
302
303 inline bool
304 operator!=(const error_code& __lhs, const error_condition& __rhs)
305 { return !(__lhs == __rhs); }
306
307 inline bool
308 operator!=(const error_condition& __lhs, const error_code& __rhs)
309 { return !(__lhs == __rhs); }
310
311 inline bool
312 operator!=(const error_condition& __lhs, const error_condition& __rhs)
313 { return !(__lhs == __rhs); }
314
315
316 /**
317 * @brief Thrown to indicate error code of underlying system.
318 *
319 * @ingroup exceptions
320 */
321 class system_error : public std::runtime_error
322 {
323 private:
324 error_code _M_code;
325
326 public:
327 system_error(error_code __ec = error_code())
328 : runtime_error(""), _M_code(__ec) { }
329
330 system_error(error_code __ec, const string& __what)
331 : runtime_error(__what), _M_code(__ec) { }
332
333 /*
334 * TODO: Add const char* ctors to all exceptions.
335 *
336 * system_error(error_code __ec, const char* __what)
337 * : runtime_error(__what), _M_code(__ec) { }
338 *
339 * system_error(int __v, const error_category& __ecat, const char* __what)
340 * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
341 */
342
343 system_error(int __v, const error_category& __ecat)
344 : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
345
346 system_error(int __v, const error_category& __ecat, const string& __what)
347 : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
348
349 virtual ~system_error() throw();
350
351 const error_code&
352 code() const throw() { return _M_code; }
353 };
354
355 _GLIBCXX_END_NAMESPACE
356
357 #endif // __GXX_EXPERIMENTAL_CXX0X__
358
359 #endif // _GLIBCXX_SYSTEM_ERROR
360