]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/system_error
PR libstdc++/36104 part four
[thirdparty/gcc.git] / libstdc++-v3 / include / std / system_error
1 // <system_error> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010 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 include/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 <bits/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 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 class error_code;
48 class error_condition;
49 class error_category;
50 class system_error;
51
52 /// is_error_code_enum
53 template<typename _Tp>
54 struct is_error_code_enum : public false_type { };
55
56 /// is_error_condition_enum
57 template<typename _Tp>
58 struct is_error_condition_enum : public false_type { };
59
60 template<>
61 struct is_error_condition_enum<errc>
62 : public true_type { };
63
64
65 /// error_category
66 class error_category
67 {
68 protected:
69 error_category();
70
71 public:
72 virtual ~error_category() { }
73
74 error_category(const error_category&) = delete;
75 error_category& operator=(const error_category&) = delete;
76
77 virtual const char*
78 name() const = 0;
79
80 virtual string
81 message(int) const = 0;
82
83 virtual error_condition
84 default_error_condition(int __i) const;
85
86 virtual bool
87 equivalent(int __i, const error_condition& __cond) const;
88
89 virtual bool
90 equivalent(const error_code& __code, int __i) const;
91
92 bool
93 operator<(const error_category& __other) const
94 { return less<const error_category*>()(this, &__other); }
95
96 bool
97 operator==(const error_category& __other) const
98 { return this == &__other; }
99
100 bool
101 operator!=(const error_category& __other) const
102 { return this != &__other; }
103 };
104
105 inline error_category::error_category() = default;
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 make_error_code(errc);
112
113 template<typename _Tp>
114 struct hash;
115
116 /// error_code
117 // Implementation-specific error identification
118 struct error_code
119 {
120 error_code()
121 : _M_value(0), _M_cat(&system_category()) { }
122
123 error_code(int __v, const error_category& __cat)
124 : _M_value(__v), _M_cat(&__cat) { }
125
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); }
130
131 void
132 assign(int __v, const error_category& __cat)
133 {
134 _M_value = __v;
135 _M_cat = &__cat;
136 }
137
138 void
139 clear()
140 { assign(0, system_category()); }
141
142 // DR 804.
143 template<typename _ErrorCodeEnum>
144 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
145 error_code&>::type
146 operator=(_ErrorCodeEnum __e)
147 { return *this = make_error_code(__e); }
148
149 int
150 value() const { return _M_value; }
151
152 const error_category&
153 category() const { return *_M_cat; }
154
155 error_condition
156 default_error_condition() const;
157
158 string
159 message() const
160 { return category().message(value()); }
161
162 explicit operator bool() const
163 { return _M_value != 0 ? true : false; }
164
165 // DR 804.
166 private:
167 friend class hash<error_code>;
168
169 int _M_value;
170 const error_category* _M_cat;
171 };
172
173 // 19.4.2.6 non-member functions
174 inline error_code
175 make_error_code(errc __e)
176 { return error_code(static_cast<int>(__e), generic_category()); }
177
178 inline bool
179 operator<(const error_code& __lhs, const error_code& __rhs)
180 {
181 return (__lhs.category() < __rhs.category()
182 || (__lhs.category() == __rhs.category()
183 && __lhs.value() < __rhs.value()));
184 }
185
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()); }
190
191 error_condition make_error_condition(errc);
192
193 /// error_condition
194 // Portable error identification
195 struct error_condition
196 {
197 error_condition()
198 : _M_value(0), _M_cat(&generic_category()) { }
199
200 error_condition(int __v, const error_category& __cat)
201 : _M_value(__v), _M_cat(&__cat) { }
202
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); }
208
209 void
210 assign(int __v, const error_category& __cat)
211 {
212 _M_value = __v;
213 _M_cat = &__cat;
214 }
215
216 // DR 804.
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); }
222
223 void
224 clear()
225 { assign(0, generic_category()); }
226
227 // 19.4.3.4 observers
228 int
229 value() const { return _M_value; }
230
231 const error_category&
232 category() const { return *_M_cat; }
233
234 string
235 message() const
236 { return category().message(value()); }
237
238 explicit operator bool() const
239 { return _M_value != 0 ? true : false; }
240
241 // DR 804.
242 private:
243 int _M_value;
244 const error_category* _M_cat;
245 };
246
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()); }
251
252 inline bool
253 operator<(const error_condition& __lhs, const error_condition& __rhs)
254 {
255 return (__lhs.category() < __rhs.category()
256 || (__lhs.category() == __rhs.category()
257 && __lhs.value() < __rhs.value()));
258 }
259
260 // 19.4.4 Comparison operators
261 inline bool
262 operator==(const error_code& __lhs, const error_code& __rhs)
263 { return (__lhs.category() == __rhs.category()
264 && __lhs.value() == __rhs.value()); }
265
266 inline bool
267 operator==(const error_code& __lhs, const error_condition& __rhs)
268 {
269 return (__lhs.category().equivalent(__lhs.value(), __rhs)
270 || __rhs.category().equivalent(__lhs, __rhs.value()));
271 }
272
273 inline bool
274 operator==(const error_condition& __lhs, const error_code& __rhs)
275 {
276 return (__rhs.category().equivalent(__rhs.value(), __lhs)
277 || __lhs.category().equivalent(__rhs, __lhs.value()));
278 }
279
280 inline bool
281 operator==(const error_condition& __lhs, const error_condition& __rhs)
282 {
283 return (__lhs.category() == __rhs.category()
284 && __lhs.value() == __rhs.value());
285 }
286
287 inline bool
288 operator!=(const error_code& __lhs, const error_code& __rhs)
289 { return !(__lhs == __rhs); }
290
291 inline bool
292 operator!=(const error_code& __lhs, const error_condition& __rhs)
293 { return !(__lhs == __rhs); }
294
295 inline bool
296 operator!=(const error_condition& __lhs, const error_code& __rhs)
297 { return !(__lhs == __rhs); }
298
299 inline bool
300 operator!=(const error_condition& __lhs, const error_condition& __rhs)
301 { return !(__lhs == __rhs); }
302
303
304 /**
305 * @brief Thrown to indicate error code of underlying system.
306 *
307 * @ingroup exceptions
308 */
309 class system_error : public std::runtime_error
310 {
311 private:
312 error_code _M_code;
313
314 public:
315 system_error(error_code __ec = error_code())
316 : runtime_error(__ec.message()), _M_code(__ec) { }
317
318 system_error(error_code __ec, const string& __what)
319 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
320
321 /*
322 * TODO: Add const char* ctors to all exceptions.
323 *
324 * system_error(error_code __ec, const char* __what)
325 * : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
326 *
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)) { }
330 */
331
332 system_error(int __v, const error_category& __ecat)
333 : runtime_error(error_code(__v, __ecat).message()),
334 _M_code(__v, __ecat) { }
335
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) { }
339
340 virtual ~system_error() throw();
341
342 const error_code&
343 code() const throw() { return _M_code; }
344 };
345
346 _GLIBCXX_END_NAMESPACE_VERSION
347 } // namespace
348
349 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
350
351 #include <bits/functional_hash.h>
352
353 namespace std _GLIBCXX_VISIBILITY(default)
354 {
355 _GLIBCXX_BEGIN_NAMESPACE_VERSION
356
357 // DR 1182.
358 /// std::hash specialization for error_code.
359 template<>
360 struct hash<error_code>
361 : public __hash_base<size_t, error_code>
362 {
363 size_t
364 operator()(const error_code& __e) const
365 {
366 const size_t __tmp = std::_Hash_impl::hash(__e._M_value);
367 return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp);
368 }
369 };
370
371 _GLIBCXX_END_NAMESPACE_VERSION
372 } // namespace
373
374 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
375
376 #endif // __GXX_EXPERIMENTAL_CXX0X__
377
378 #endif // _GLIBCXX_SYSTEM_ERROR