]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/system_error
tuple: Ifndef __GXX_EXPERIMENTAL_CXX0X__ just error out.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / system_error
1 // <system_error> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008 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 2, 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 // 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.
20
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.
29
30 /** @file system_error
31 * This is a Standard C++ Library header.
32 */
33
34 #ifndef _GLIBCXX_SYSTEM_ERROR
35 #define _GLIBCXX_SYSTEM_ERROR 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #else
42
43 #include <bits/c++config.h>
44 #include <bits/error_constants.h>
45 #include <iosfwd>
46 #include <stdexcept>
47
48 _GLIBCXX_BEGIN_NAMESPACE(std)
49
50 class error_code;
51 class error_condition;
52 class error_category;
53 class system_error;
54
55 /// is_error_code_enum
56 template<typename _T>
57 struct is_error_code_enum : public false_type { };
58
59 template<>
60 struct is_error_code_enum<posix_error::posix_errno>
61 : public true_type { };
62
63 /// is_error_condition_enum
64 template<typename _T>
65 struct is_error_condition_enum : public false_type { };
66
67 template<>
68 struct is_error_condition_enum<posix_error::posix_errno>
69 : public true_type { };
70
71
72 /// error_category
73 struct error_category
74 {
75 error_category() { }
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 private:
105 error_category(const error_category&);
106
107 error_category&
108 operator=(const error_category&);
109 };
110
111 const error_category& get_posix_category();
112 const error_category& get_system_category();
113
114 static const error_category& system_category = get_system_category();
115 static const error_category& native_category = get_posix_category();
116
117 /// error_code
118 // Implementation-specific error identification
119 struct error_code
120 {
121 error_code()
122 : _M_value(0), _M_cat(&system_category) { }
123
124 error_code(int __v, const error_category& __cat)
125 : _M_value(__v), _M_cat(&__cat) { }
126
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(&system_category)
131 { }
132
133 void
134 assign(int __v, const error_category& __cat)
135 {
136 _M_value = __v;
137 _M_cat = &__cat;
138 }
139
140 void
141 clear()
142 {
143 _M_value = 0;
144 _M_cat = &system_category;
145 }
146
147 template<typename _ErrorCodeEnum>
148 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type&
149 operator=(_ErrorCodeEnum __e)
150 { _M_value = __e; }
151
152 int
153 value() const { return _M_value; }
154
155 const error_category&
156 category() const { return *_M_cat; }
157
158 error_condition
159 default_error_condition() const;
160
161 string
162 message() const
163 { return category().message(value()); }
164
165 // Safe bool idiom.
166 // explicit operator bool() const throw()
167 // { return _M_value != 0; }
168 typedef void (*__bool_type)();
169
170 static void __not_bool_type() { }
171
172 operator __bool_type() const
173 { return _M_value != 0 ? &__not_bool_type : false; }
174
175 private:
176 int _M_value;
177 const error_category* _M_cat;
178 };
179
180 error_code
181 make_error_code(posix_error::posix_errno);
182
183 // 19.4.2.5 non-member functions
184 bool operator<(const error_code& lhs, const error_code& rhs);
185
186 template<typename charT, typename traits>
187 basic_ostream<charT,traits>&
188 operator<<(basic_ostream<charT, traits>& os, const error_code& __code);
189
190
191 /// error_condition
192 // Portable error identification
193 struct error_condition
194 {
195 error_condition() : _M_value(0), _M_cat(system_category) { }
196
197 error_condition(int __v, const error_category& __cat)
198 : _M_value(__v), _M_cat(__cat) { }
199
200 template<typename _ErrorEnum>
201 error_condition(typename enable_if<
202 is_error_condition_enum<_ErrorEnum>::value,
203 _ErrorEnum>::type __v)
204 : _M_value(__v), _M_cat(system_category) { }
205
206 void
207 assign(int val, const error_category& cat);
208
209 template<typename _ErrorEnum>
210 error_condition&
211 operator=(typename enable_if<is_error_condition_enum<_ErrorEnum>::value,
212 _ErrorEnum>::type __v)
213 { _M_value = __v; }
214
215 void
216 clear();
217
218 // 19.4.3.4 observers
219 int
220 value() const { return _M_value; }
221
222 const error_category&
223 category() const { return _M_cat; }
224
225 string
226 message() const
227 { return category().message(value()); }
228
229 // Safe bool idiom.
230 // explicit operator bool() const throw()
231 // { return _M_value != 0; }
232 typedef void (*__bool_type)();
233
234 static void __not_bool_type() { }
235
236 operator __bool_type() const
237 { return _M_value != 0 ? &__not_bool_type : false; }
238
239 private:
240 int _M_value;
241 const error_category& _M_cat;
242 };
243
244 error_condition
245 make_error_condition(posix_error::posix_errno);
246
247 // 19.4.3.5 non-member functions
248 inline bool
249 operator<(const error_condition& lhs, const error_condition& rhs)
250 {
251 bool __t1 = lhs.category() < rhs.category();
252 bool __t2 = lhs.category() == rhs.category() && lhs.value() < rhs.value();
253 return __t1 || __t2;
254 }
255
256 // 19.4.4 Comparison operators
257 inline bool
258 operator==(const error_code& lhs, const error_code& rhs)
259 { return lhs.category() == rhs.category() && lhs.value() == rhs.value(); }
260
261 inline bool
262 operator==(const error_code& lhs, const error_condition& rhs)
263 {
264 bool __t1 = lhs.category().equivalent(lhs.value(), rhs);
265 bool __t2 = rhs.category().equivalent(lhs, rhs.value());
266 return __t1 || __t2;
267 }
268
269 inline bool
270 operator==(const error_condition& lhs, const error_code& rhs)
271 {
272 bool __t1 = rhs.category().equivalent(rhs.value(), lhs);
273 bool __t2 = lhs.category().equivalent(rhs, lhs.value());
274 return __t1 || __t2;
275 }
276
277 inline bool
278 operator==(const error_condition& lhs, const error_condition& rhs)
279 { return lhs.category() == rhs.category() && lhs.value() == rhs.value(); }
280
281 inline bool
282 operator!=(const error_code& lhs, const error_code& rhs)
283 { return !(lhs == rhs); }
284
285 inline bool
286 operator!=(const error_code& lhs, const error_condition& rhs)
287 { return !(lhs == rhs); }
288
289 inline bool
290 operator!=(const error_condition& lhs, const error_code& rhs)
291 { return !(lhs == rhs); }
292
293 inline bool
294 operator!=(const error_condition& lhs, const error_condition& rhs)
295 { return !(lhs == rhs); }
296
297 /// Thrown to indicate error code of underlying system.
298 class system_error : public std::runtime_error
299 {
300 private:
301 error_code _M_code;
302
303 public:
304 system_error(error_code __ec = error_code())
305 : runtime_error(""), _M_code(__ec) { }
306
307 system_error(error_code __ec, const string& __what)
308 : runtime_error(__what), _M_code(__ec) { }
309
310 system_error(int __v, const error_category& __ecat)
311 : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
312
313 system_error(int __v, const error_category& __ecat, const string& __what)
314 : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
315
316 virtual ~system_error() throw();
317
318 const error_code&
319 code() const throw() { return _M_code; }
320 };
321
322 _GLIBCXX_END_NAMESPACE
323
324 #endif // __GXX_EXPERIMENTAL_CXX0X__
325
326 #endif // _GLIBCXX_SYSTEM_ERROR
327