OSDN Git Service

2008-10-22 Chris Fairles <cfairles@gcc.gnu.org>
[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<errc>
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<errc>
69     : public true_type { };
70
71
72   /// error_category
73   class error_category
74   {
75   protected:
76     error_category() = default;
77
78   public:
79     virtual ~error_category() { }
80
81     error_category(const error_category&) = delete;
82     error_category& operator=(const error_category&) = delete;
83
84     virtual const char* 
85     name() const = 0;
86
87     virtual string 
88     message(int) const = 0;
89
90     virtual error_condition
91     default_error_condition(int __i) const;
92
93     virtual bool 
94     equivalent(int __i, const error_condition& __cond) const;
95
96     virtual bool 
97     equivalent(const error_code& __code, int __i) const;
98
99     bool 
100     operator<(const error_category& __other) const
101     { return less<const error_category*>()(this, &__other); }
102
103     bool 
104     operator==(const error_category& __other) const
105     { return this == &__other; }
106
107     bool 
108     operator!=(const error_category& __other) const
109     { return this != &__other; }
110   };
111
112   // DR 890.
113   extern const error_category& system_category;
114   extern const error_category& generic_category;
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       : _M_value(static_cast<int>(__e)), _M_cat(&generic_category)
130       { }
131
132     void 
133     assign(int __v, const error_category& __cat)
134     {
135       _M_value = __v;
136       _M_cat = &__cat; 
137     }
138
139     void 
140     clear()
141     { assign(0, system_category); }
142
143     // DR 804.
144     template<typename _ErrorCodeEnum>
145       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
146                          error_code&>::type
147       operator=(_ErrorCodeEnum __e)
148       {
149         assign(static_cast<int>(__e), generic_category);
150         return *this;
151       }
152
153     int
154     value() const { return _M_value; }
155       
156     const error_category&  
157     category() const { return *_M_cat; }
158
159     error_condition 
160     default_error_condition() const;
161
162     string 
163     message() const
164     { return category().message(value()); }
165
166     // Safe bool idiom.
167     // explicit operator bool() const throw()
168     // { return _M_value != 0; }
169     typedef void (*__bool_type)();
170
171     static void __not_bool_type() { }
172
173     operator __bool_type() const
174     { return _M_value != 0 ? &__not_bool_type : false; }
175
176     // DR 804.
177   private:
178     int                         _M_value;
179     const error_category*       _M_cat;
180   };
181
182   // 19.4.2.6 non-member functions
183   inline error_code
184   make_error_code(errc __e)
185   { return error_code(static_cast<int>(__e), generic_category); }
186
187   inline bool
188   operator<(const error_code& __lhs, const error_code& __rhs)
189   { 
190     return (__lhs.category() < __rhs.category()
191             || (__lhs.category() == __rhs.category()
192                 && __lhs.value() < __rhs.value()));
193   }
194
195   template<typename _CharT, typename _Traits>
196     basic_ostream<_CharT, _Traits>&
197     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
198     { return (__os << __e.category().name() << ':' << __e.value()); }
199
200
201   /// error_condition
202   // Portable error identification
203   struct error_condition 
204   {
205     error_condition() : _M_value(0), _M_cat(&generic_category) { }
206
207     error_condition(int __v, const error_category& __cat)     
208     : _M_value(__v), _M_cat(&__cat) { }
209
210     template<typename _ErrorConditionEnum>
211       error_condition(_ErrorConditionEnum __e,
212                       typename enable_if<is_error_condition_enum
213                                       <_ErrorConditionEnum>::value>::type* = 0)
214       : _M_value(static_cast<int>(__e)), _M_cat(&generic_category) { }
215
216     void
217     assign(int __v, const error_category& __cat)
218     {
219       _M_value = __v;
220       _M_cat = &__cat;
221     }
222
223     // DR 804.
224     template<typename _ErrorConditionEnum>
225       typename enable_if<is_error_condition_enum
226                          <_ErrorConditionEnum>::value, error_condition&>::type
227       operator=(_ErrorConditionEnum __e)
228       {
229         assign(static_cast<int>(__e), generic_category);
230         return *this;
231       }
232
233     void 
234     clear()
235     { assign(0, generic_category); }
236
237     // 19.4.3.4 observers
238     int 
239     value() const { return _M_value; }
240
241     const error_category&
242     category() const { return *_M_cat; }
243
244     string 
245     message() const
246     { return category().message(value()); }
247
248     // Safe bool idiom.
249     // explicit operator bool() const throw()
250     // { return _M_value != 0; }
251     typedef void (*__bool_type)();
252
253     static void __not_bool_type() { }
254
255     operator __bool_type() const
256     { return _M_value != 0 ? &__not_bool_type : false; }
257
258     // DR 804.
259   private:
260     int                         _M_value;
261     const error_category*       _M_cat;
262   };
263
264   // 19.4.3.6 non-member functions
265   inline error_condition
266   make_error_condition(errc __e)
267   { return error_condition(static_cast<int>(__e), generic_category); }
268
269   inline bool 
270   operator<(const error_condition& __lhs, const error_condition& __rhs)
271   {
272     return (__lhs.category() < __rhs.category()
273             || (__lhs.category() == __rhs.category()
274                 && __lhs.value() < __rhs.value()));
275   }
276
277   // 19.4.4 Comparison operators
278   inline bool
279   operator==(const error_code& __lhs, const error_code& __rhs)
280   { return (__lhs.category() == __rhs.category()
281             && __lhs.value() == __rhs.value()); }
282
283   inline bool
284   operator==(const error_code& __lhs, const error_condition& __rhs)
285   {
286     return (__lhs.category().equivalent(__lhs.value(), __rhs)
287             || __rhs.category().equivalent(__lhs, __rhs.value()));
288   }
289
290   inline bool
291   operator==(const error_condition& __lhs, const error_code& __rhs)
292   {
293     return (__rhs.category().equivalent(__rhs.value(), __lhs)
294             || __lhs.category().equivalent(__rhs, __lhs.value()));
295   }
296
297   inline bool
298   operator==(const error_condition& __lhs, const error_condition& __rhs)
299   {
300     return (__lhs.category() == __rhs.category()
301             && __lhs.value() == __rhs.value());
302   }
303
304   inline bool
305   operator!=(const error_code& __lhs, const error_code& __rhs)
306   { return !(__lhs == __rhs); }
307
308   inline bool
309   operator!=(const error_code& __lhs, const error_condition& __rhs)
310   { return !(__lhs == __rhs); }
311
312   inline bool
313   operator!=(const error_condition& __lhs, const error_code& __rhs)
314   { return !(__lhs == __rhs); }
315
316   inline bool
317   operator!=(const error_condition& __lhs, const error_condition& __rhs)
318   { return !(__lhs == __rhs); }
319
320
321   /// Thrown to indicate error code of underlying system.
322   class system_error : public std::runtime_error
323   {
324   private:
325     error_code  _M_code;
326
327   public:
328     system_error(error_code __ec = error_code())
329     : runtime_error(""), _M_code(__ec) { }
330
331     system_error(error_code __ec, const string& __what)
332     : runtime_error(__what), _M_code(__ec) { }
333     
334     /*
335      * TODO: Add const char* ctors to all exceptions.
336      *
337      * system_error(error_code __ec, const char* __what)
338      * : runtime_error(__what), _M_code(__ec) { }
339      *
340      * system_error(int __v, const error_category& __ecat, const char* __what)
341      * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
342      */
343
344     system_error(int __v, const error_category& __ecat)
345     : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
346
347     system_error(int __v, const error_category& __ecat, const string& __what)
348     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
349
350     virtual ~system_error() throw();
351
352     const error_code& 
353     code() const throw() { return _M_code; }
354   };
355
356 _GLIBCXX_END_NAMESPACE
357
358 #endif // __GXX_EXPERIMENTAL_CXX0X__
359
360 #endif // _GLIBCXX_SYSTEM_ERROR
361