OSDN Git Service

2009-05-20 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.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   /// is_error_condition_enum
55   template<typename _Tp>
56     struct is_error_condition_enum : public false_type { };
57
58   template<> 
59     struct is_error_condition_enum<errc>
60     : public true_type { };
61
62
63   /// error_category
64   class error_category
65   {
66   protected:
67     error_category() = default;
68
69   public:
70     virtual ~error_category() { }
71
72     error_category(const error_category&) = delete;
73     error_category& operator=(const error_category&) = delete;
74
75     virtual const char* 
76     name() const = 0;
77
78     virtual string 
79     message(int) const = 0;
80
81     virtual error_condition
82     default_error_condition(int __i) const;
83
84     virtual bool 
85     equivalent(int __i, const error_condition& __cond) const;
86
87     virtual bool 
88     equivalent(const error_code& __code, int __i) const;
89
90     bool 
91     operator<(const error_category& __other) const
92     { return less<const error_category*>()(this, &__other); }
93
94     bool 
95     operator==(const error_category& __other) const
96     { return this == &__other; }
97
98     bool 
99     operator!=(const error_category& __other) const
100     { return this != &__other; }
101   };
102
103   // DR 890.
104   _GLIBCXX_CONST const error_category& system_category() throw ();
105   _GLIBCXX_CONST const error_category& generic_category() throw ();
106
107   error_code make_error_code(errc);
108
109   /// error_code
110   // Implementation-specific error identification
111   struct error_code
112   {
113     error_code()
114     : _M_value(0), _M_cat(&system_category()) { }
115
116     error_code(int __v, const error_category& __cat)
117     : _M_value(__v), _M_cat(&__cat) { }
118
119     template<typename _ErrorCodeEnum>
120       error_code(_ErrorCodeEnum __e,
121       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type* = 0)
122       { *this = make_error_code(__e); }
123
124     void 
125     assign(int __v, const error_category& __cat)
126     {
127       _M_value = __v;
128       _M_cat = &__cat; 
129     }
130
131     void 
132     clear()
133     { assign(0, system_category()); }
134
135     // DR 804.
136     template<typename _ErrorCodeEnum>
137       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
138                          error_code&>::type
139       operator=(_ErrorCodeEnum __e)
140       { return *this = make_error_code(__e); }
141
142     int
143     value() const { return _M_value; }
144       
145     const error_category&  
146     category() const { return *_M_cat; }
147
148     error_condition 
149     default_error_condition() const;
150
151     string 
152     message() const
153     { return category().message(value()); }
154
155     explicit operator bool() const
156     { return _M_value != 0 ? true : false; }
157
158     // DR 804.
159   private:
160     int                         _M_value;
161     const error_category*       _M_cat;
162   };
163
164   // 19.4.2.6 non-member functions
165   inline error_code
166   make_error_code(errc __e)
167   { return error_code(static_cast<int>(__e), generic_category()); }
168
169   inline bool
170   operator<(const error_code& __lhs, const error_code& __rhs)
171   { 
172     return (__lhs.category() < __rhs.category()
173             || (__lhs.category() == __rhs.category()
174                 && __lhs.value() < __rhs.value()));
175   }
176
177   template<typename _CharT, typename _Traits>
178     basic_ostream<_CharT, _Traits>&
179     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
180     { return (__os << __e.category().name() << ':' << __e.value()); }
181
182   error_condition make_error_condition(errc);
183
184   /// error_condition
185   // Portable error identification
186   struct error_condition 
187   {
188     error_condition()
189     : _M_value(0), _M_cat(&generic_category()) { }
190
191     error_condition(int __v, const error_category& __cat)     
192     : _M_value(__v), _M_cat(&__cat) { }
193
194     template<typename _ErrorConditionEnum>
195       error_condition(_ErrorConditionEnum __e,
196                       typename enable_if<is_error_condition_enum
197                                       <_ErrorConditionEnum>::value>::type* = 0)
198       { *this = make_error_condition(__e); }
199
200     void
201     assign(int __v, const error_category& __cat)
202     {
203       _M_value = __v;
204       _M_cat = &__cat;
205     }
206
207     // DR 804.
208     template<typename _ErrorConditionEnum>
209       typename enable_if<is_error_condition_enum
210                          <_ErrorConditionEnum>::value, error_condition&>::type
211       operator=(_ErrorConditionEnum __e)
212       { return *this = make_error_condition(__e); }
213
214     void 
215     clear()
216     { assign(0, generic_category()); }
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     explicit operator bool() const
230     { return _M_value != 0 ? true : false; }
231
232     // DR 804.
233   private:
234     int                         _M_value;
235     const error_category*       _M_cat;
236   };
237
238   // 19.4.3.6 non-member functions
239   inline error_condition
240   make_error_condition(errc __e)
241   { return error_condition(static_cast<int>(__e), generic_category()); }
242
243   inline bool 
244   operator<(const error_condition& __lhs, const error_condition& __rhs)
245   {
246     return (__lhs.category() < __rhs.category()
247             || (__lhs.category() == __rhs.category()
248                 && __lhs.value() < __rhs.value()));
249   }
250
251   // 19.4.4 Comparison operators
252   inline bool
253   operator==(const error_code& __lhs, const error_code& __rhs)
254   { return (__lhs.category() == __rhs.category()
255             && __lhs.value() == __rhs.value()); }
256
257   inline bool
258   operator==(const error_code& __lhs, const error_condition& __rhs)
259   {
260     return (__lhs.category().equivalent(__lhs.value(), __rhs)
261             || __rhs.category().equivalent(__lhs, __rhs.value()));
262   }
263
264   inline bool
265   operator==(const error_condition& __lhs, const error_code& __rhs)
266   {
267     return (__rhs.category().equivalent(__rhs.value(), __lhs)
268             || __lhs.category().equivalent(__rhs, __lhs.value()));
269   }
270
271   inline bool
272   operator==(const error_condition& __lhs, const error_condition& __rhs)
273   {
274     return (__lhs.category() == __rhs.category()
275             && __lhs.value() == __rhs.value());
276   }
277
278   inline bool
279   operator!=(const error_code& __lhs, const error_code& __rhs)
280   { return !(__lhs == __rhs); }
281
282   inline bool
283   operator!=(const error_code& __lhs, const error_condition& __rhs)
284   { return !(__lhs == __rhs); }
285
286   inline bool
287   operator!=(const error_condition& __lhs, const error_code& __rhs)
288   { return !(__lhs == __rhs); }
289
290   inline bool
291   operator!=(const error_condition& __lhs, const error_condition& __rhs)
292   { return !(__lhs == __rhs); }
293
294
295   /** 
296    *  @brief Thrown to indicate error code of underlying system.
297    *
298    *  @ingroup exceptions
299    */
300   class system_error : public std::runtime_error
301   {
302   private:
303     error_code  _M_code;
304
305   public:
306     system_error(error_code __ec = error_code())
307     : runtime_error(""), _M_code(__ec) { }
308
309     system_error(error_code __ec, const string& __what)
310     : runtime_error(__what), _M_code(__ec) { }
311     
312     /*
313      * TODO: Add const char* ctors to all exceptions.
314      *
315      * system_error(error_code __ec, const char* __what)
316      * : runtime_error(__what), _M_code(__ec) { }
317      *
318      * system_error(int __v, const error_category& __ecat, const char* __what)
319      * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
320      */
321
322     system_error(int __v, const error_category& __ecat)
323     : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
324
325     system_error(int __v, const error_category& __ecat, const string& __what)
326     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
327
328     virtual ~system_error() throw();
329
330     const error_code& 
331     code() const throw() { return _M_code; }
332   };
333
334 _GLIBCXX_END_NAMESPACE
335
336 #endif // __GXX_EXPERIMENTAL_CXX0X__
337
338 #endif // _GLIBCXX_SYSTEM_ERROR
339