OSDN Git Service

2008-05-16 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 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 #endif
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<is_error_condition_enum<_ErrorEnum>::value, _ErrorEnum>::type __v) : _M_value(__v), _M_cat(system_category) { }
202
203     void 
204     assign(int val, const error_category& cat);
205
206     template<typename _ErrorEnum>
207       error_condition&
208       operator=(typename enable_if<is_error_condition_enum<_ErrorEnum>::value, _ErrorEnum>::type __v)
209       { _M_value = __v; }
210
211     void 
212     clear();
213
214     // 19.4.3.4 observers
215     int 
216     value() const { return _M_value; }
217
218     const error_category& 
219     category() const { return _M_cat; }
220
221     string 
222     message() const
223     { return category().message(value()); }
224
225     // Safe bool idiom.
226     // explicit operator bool() const throw()
227     // { return _M_value != 0; }
228     typedef void (*__bool_type)();
229
230     static void __not_bool_type() { }
231
232     operator __bool_type() const
233     { return _M_value != 0 ? &__not_bool_type : false; }
234
235   private:
236     int                         _M_value;
237     const error_category&       _M_cat;
238   };
239
240   error_condition 
241   make_error_condition(posix_error::posix_errno);
242
243   // 19.4.3.5 non-member functions
244   inline bool 
245   operator<(const error_condition& lhs, const error_condition& rhs)
246   { 
247     bool __t1 = lhs.category() < rhs.category();
248     bool __t2 = lhs.category() == rhs.category() && lhs.value() < rhs.value();
249     return __t1 || __t2;
250   }
251
252   // 19.4.4 Comparison operators
253   inline bool 
254   operator==(const error_code& lhs, const error_code& rhs)
255   { return lhs.category() == rhs.category() && lhs.value() == rhs.value(); }
256
257   inline bool 
258   operator==(const error_code& lhs, const error_condition& rhs)
259   {
260     bool __t1 = lhs.category().equivalent(lhs.value(), rhs);
261     bool __t2 = rhs.category().equivalent(lhs, rhs.value());
262     return __t1 || __t2;
263   }
264
265   inline bool 
266   operator==(const error_condition& lhs, const error_code& rhs)
267   {
268     bool __t1 = rhs.category().equivalent(rhs.value(), lhs);
269     bool __t2 = lhs.category().equivalent(rhs, lhs.value());
270     return __t1 || __t2;
271   }
272
273   inline bool 
274   operator==(const error_condition& lhs, const error_condition& rhs)
275   { return lhs.category() == rhs.category() && lhs.value() == rhs.value(); }
276
277   inline bool 
278   operator!=(const error_code& lhs, const error_code& rhs)
279   { return !(lhs == rhs); }
280
281   inline bool 
282   operator!=(const error_code& lhs, const error_condition& rhs)
283   { return !(lhs == rhs); }
284
285   inline bool 
286   operator!=(const error_condition& lhs, const error_code& rhs)
287   { return !(lhs == rhs); }
288
289   inline bool 
290   operator!=(const error_condition& lhs, const error_condition& rhs)
291   { return !(lhs == rhs); }
292
293   /// Thrown to indicate error code of underlying system.
294   class system_error : public std::runtime_error
295   {
296   private:
297     error_code  _M_code;
298
299   public:
300     system_error(error_code __ec = error_code())
301     : runtime_error(""), _M_code(__ec) { }
302
303     system_error(error_code __ec, const string& __what)
304     : runtime_error(__what), _M_code(__ec) { }
305
306     system_error(int __v, const error_category& __ecat)
307     : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
308
309     system_error(int __v, const error_category& __ecat, const string& __what)
310     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
311
312     virtual ~system_error() throw();
313
314     const error_code& 
315     code() const throw() { return _M_code; }
316   };
317
318 _GLIBCXX_END_NAMESPACE
319
320 #endif 
321