OSDN Git Service

6a09ebf5272e5f552708c1acf71a5f5c3d6f3129
[pf3gnuchains/gcc-fork.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 _Tp>
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 _Tp>
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& posix_category = get_posix_category();
115   static const error_category& system_category = get_system_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(&posix_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     // DR 804.
148     template<typename _ErrorCodeEnum>
149       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
150                          error_code&>::type
151       operator=(_ErrorCodeEnum __e)
152       {
153         _M_value = __e;
154         _M_cat = &posix_category;
155         return *this;
156       }
157
158     int
159     value() const { return _M_value; }
160       
161     const error_category&  
162     category() const { return *_M_cat; }
163
164     error_condition 
165     default_error_condition() const;
166
167     string 
168     message() const
169     { return category().message(value()); }
170
171     // Safe bool idiom.
172     // explicit operator bool() const throw()
173     // { return _M_value != 0; }
174     typedef void (*__bool_type)();
175
176     static void __not_bool_type() { }
177
178     operator __bool_type() const
179     { return _M_value != 0 ? &__not_bool_type : false; }
180
181     // DR 804.
182   private:
183     int                         _M_value;
184     const error_category*       _M_cat;
185   };
186
187   // 19.4.2.5 non-member functions
188   inline bool
189   operator<(const error_code& __lhs, const error_code& __rhs)
190   { 
191     return (__lhs.category() < __rhs.category()
192             || (__lhs.category() == __rhs.category()
193                 && __lhs.value() < __rhs.value()));
194   }
195
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()); }
200
201
202   /// error_condition
203   // Portable error identification
204   struct error_condition 
205   {
206     error_condition() : _M_value(0), _M_cat(&posix_category) { }
207
208     error_condition(int __v, const error_category& __cat)     
209     : _M_value(__v), _M_cat(&__cat) { }
210
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) { }
216
217     void
218     assign(int __v, const error_category& __cat)
219     {
220       _M_value = __v;
221       _M_cat = &__cat;
222     }
223
224     // DR 804.
225     template<typename _ErrorConditionEnum>
226       typename enable_if<is_error_condition_enum
227                          <_ErrorConditionEnum>::value, error_condition&>::type
228       operator=(_ErrorConditionEnum __e)
229       {
230         _M_value = __e;
231         _M_cat = &posix_category;
232         return *this;
233       }
234
235     void 
236     clear()
237     {
238       _M_value = 0;
239       _M_cat = &posix_category;
240     }
241
242     // 19.4.3.4 observers
243     int 
244     value() const { return _M_value; }
245
246     const error_category&
247     category() const { return *_M_cat; }
248
249     string 
250     message() const
251     { return category().message(value()); }
252
253     // Safe bool idiom.
254     // explicit operator bool() const throw()
255     // { return _M_value != 0; }
256     typedef void (*__bool_type)();
257
258     static void __not_bool_type() { }
259
260     operator __bool_type() const
261     { return _M_value != 0 ? &__not_bool_type : false; }
262
263     // DR 804.
264   private:
265     int                         _M_value;
266     const error_category*       _M_cat;
267   };
268
269   // 19.4.3.5 non-member functions
270   inline bool 
271   operator<(const error_condition& __lhs, const error_condition& __rhs)
272   {
273     return (__lhs.category() < __rhs.category()
274             || (__lhs.category() == __rhs.category()
275                 && __lhs.value() < __rhs.value()));
276   }
277
278   namespace posix_error
279   {
280     inline error_code 
281     make_error_code(posix_errno __e)
282     { return error_code(__e, posix_category); }
283
284     inline error_condition 
285     make_error_condition(posix_errno __e)
286     { return error_condition(__e, posix_category); }
287   }
288
289   // 19.4.4 Comparison operators
290   inline bool
291   operator==(const error_code& __lhs, const error_code& __rhs)
292   { return (__lhs.category() == __rhs.category()
293             && __lhs.value() == __rhs.value()); }
294
295   inline bool
296   operator==(const error_code& __lhs, const error_condition& __rhs)
297   {
298     return (__lhs.category().equivalent(__lhs.value(), __rhs)
299             || __rhs.category().equivalent(__lhs, __rhs.value()));
300   }
301
302   inline bool
303   operator==(const error_condition& __lhs, const error_code& __rhs)
304   {
305     return (__rhs.category().equivalent(__rhs.value(), __lhs)
306             || __lhs.category().equivalent(__rhs, __lhs.value()));
307   }
308
309   inline bool
310   operator==(const error_condition& __lhs, const error_condition& __rhs)
311   {
312     return (__lhs.category() == __rhs.category()
313             && __lhs.value() == __rhs.value());
314   }
315
316   inline bool
317   operator!=(const error_code& __lhs, const error_code& __rhs)
318   { return !(__lhs == __rhs); }
319
320   inline bool
321   operator!=(const error_code& __lhs, const error_condition& __rhs)
322   { return !(__lhs == __rhs); }
323
324   inline bool
325   operator!=(const error_condition& __lhs, const error_code& __rhs)
326   { return !(__lhs == __rhs); }
327
328   inline bool
329   operator!=(const error_condition& __lhs, const error_condition& __rhs)
330   { return !(__lhs == __rhs); }
331
332
333   /// Thrown to indicate error code of underlying system.
334   class system_error : public std::runtime_error
335   {
336   private:
337     error_code  _M_code;
338
339   public:
340     system_error(error_code __ec = error_code())
341     : runtime_error(""), _M_code(__ec) { }
342
343     system_error(error_code __ec, const string& __what)
344     : runtime_error(__what), _M_code(__ec) { }
345
346     system_error(int __v, const error_category& __ecat)
347     : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
348
349     system_error(int __v, const error_category& __ecat, const string& __what)
350     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
351
352     virtual ~system_error() throw();
353
354     const error_code& 
355     code() const throw() { return _M_code; }
356   };
357
358 _GLIBCXX_END_NAMESPACE
359
360 #endif // __GXX_EXPERIMENTAL_CXX0X__
361
362 #endif // _GLIBCXX_SYSTEM_ERROR
363