Merge up to mainline revision 153570.
[official-gcc.git] / libstdc++-v3 / include / std / system_error
blob864741129a9d0986e70365c8fac3976745113b24
1 // <system_error> -*- C++ -*-
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.
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.
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.
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/>.
25 /** @file system_error
26  *  This is a Standard C++ Library header.
27  */
29 #ifndef _GLIBCXX_SYSTEM_ERROR
30 #define _GLIBCXX_SYSTEM_ERROR 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <c++0x_warning.h>
36 #else
38 #include <bits/c++config.h>
39 #include <bits/error_constants.h>
40 #include <iosfwd>
41 #include <stdexcept>
43 _GLIBCXX_BEGIN_NAMESPACE(std)
45   class error_code;
46   class error_condition;
47   class error_category;
48   class system_error;
50   /// is_error_code_enum
51   template<typename _Tp>
52     struct is_error_code_enum : public false_type { };
54   /// is_error_condition_enum
55   template<typename _Tp>
56     struct is_error_condition_enum : public false_type { };
58   template<> 
59     struct is_error_condition_enum<errc>
60     : public true_type { };
63   /// error_category
64   class error_category
65   {
66   protected:
67     error_category();
69   public:
70     virtual ~error_category() { }
72     error_category(const error_category&) = delete;
73     error_category& operator=(const error_category&) = delete;
75     virtual const char* 
76     name() const = 0;
78     virtual string 
79     message(int) const = 0;
81     virtual error_condition
82     default_error_condition(int __i) const;
84     virtual bool 
85     equivalent(int __i, const error_condition& __cond) const;
87     virtual bool 
88     equivalent(const error_code& __code, int __i) const;
90     bool 
91     operator<(const error_category& __other) const
92     { return less<const error_category*>()(this, &__other); }
94     bool 
95     operator==(const error_category& __other) const
96     { return this == &__other; }
98     bool 
99     operator!=(const error_category& __other) const
100     { return this != &__other; }
101   };
103   inline error_category::error_category() = default;
105   // DR 890.
106   _GLIBCXX_CONST const error_category& system_category() throw ();
107   _GLIBCXX_CONST const error_category& generic_category() throw ();
109   error_code make_error_code(errc);
111   /// error_code
112   // Implementation-specific error identification
113   struct error_code
114   {
115     error_code()
116     : _M_value(0), _M_cat(&system_category()) { }
118     error_code(int __v, const error_category& __cat)
119     : _M_value(__v), _M_cat(&__cat) { }
121     template<typename _ErrorCodeEnum>
122       error_code(_ErrorCodeEnum __e,
123       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type* = 0)
124       { *this = make_error_code(__e); }
126     void 
127     assign(int __v, const error_category& __cat)
128     {
129       _M_value = __v;
130       _M_cat = &__cat; 
131     }
133     void 
134     clear()
135     { assign(0, system_category()); }
137     // DR 804.
138     template<typename _ErrorCodeEnum>
139       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
140                          error_code&>::type
141       operator=(_ErrorCodeEnum __e)
142       { return *this = make_error_code(__e); }
144     int
145     value() const { return _M_value; }
146       
147     const error_category&  
148     category() const { return *_M_cat; }
150     error_condition 
151     default_error_condition() const;
153     string 
154     message() const
155     { return category().message(value()); }
157     explicit operator bool() const
158     { return _M_value != 0 ? true : false; }
160     // DR 804.
161   private:
162     int                         _M_value;
163     const error_category*       _M_cat;
164   };
166   // 19.4.2.6 non-member functions
167   inline error_code
168   make_error_code(errc __e)
169   { return error_code(static_cast<int>(__e), generic_category()); }
171   inline bool
172   operator<(const error_code& __lhs, const error_code& __rhs)
173   { 
174     return (__lhs.category() < __rhs.category()
175             || (__lhs.category() == __rhs.category()
176                 && __lhs.value() < __rhs.value()));
177   }
179   template<typename _CharT, typename _Traits>
180     basic_ostream<_CharT, _Traits>&
181     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
182     { return (__os << __e.category().name() << ':' << __e.value()); }
184   error_condition make_error_condition(errc);
186   /// error_condition
187   // Portable error identification
188   struct error_condition 
189   {
190     error_condition()
191     : _M_value(0), _M_cat(&generic_category()) { }
193     error_condition(int __v, const error_category& __cat)     
194     : _M_value(__v), _M_cat(&__cat) { }
196     template<typename _ErrorConditionEnum>
197       error_condition(_ErrorConditionEnum __e,
198                       typename enable_if<is_error_condition_enum
199                                       <_ErrorConditionEnum>::value>::type* = 0)
200       { *this = make_error_condition(__e); }
202     void
203     assign(int __v, const error_category& __cat)
204     {
205       _M_value = __v;
206       _M_cat = &__cat;
207     }
209     // DR 804.
210     template<typename _ErrorConditionEnum>
211       typename enable_if<is_error_condition_enum
212                          <_ErrorConditionEnum>::value, error_condition&>::type
213       operator=(_ErrorConditionEnum __e)
214       { return *this = make_error_condition(__e); }
216     void 
217     clear()
218     { assign(0, generic_category()); }
220     // 19.4.3.4 observers
221     int 
222     value() const { return _M_value; }
224     const error_category&
225     category() const { return *_M_cat; }
227     string 
228     message() const
229     { return category().message(value()); }
231     explicit operator bool() const
232     { return _M_value != 0 ? true : false; }
234     // DR 804.
235   private:
236     int                         _M_value;
237     const error_category*       _M_cat;
238   };
240   // 19.4.3.6 non-member functions
241   inline error_condition
242   make_error_condition(errc __e)
243   { return error_condition(static_cast<int>(__e), generic_category()); }
245   inline bool 
246   operator<(const error_condition& __lhs, const error_condition& __rhs)
247   {
248     return (__lhs.category() < __rhs.category()
249             || (__lhs.category() == __rhs.category()
250                 && __lhs.value() < __rhs.value()));
251   }
253   // 19.4.4 Comparison operators
254   inline bool
255   operator==(const error_code& __lhs, const error_code& __rhs)
256   { return (__lhs.category() == __rhs.category()
257             && __lhs.value() == __rhs.value()); }
259   inline bool
260   operator==(const error_code& __lhs, const error_condition& __rhs)
261   {
262     return (__lhs.category().equivalent(__lhs.value(), __rhs)
263             || __rhs.category().equivalent(__lhs, __rhs.value()));
264   }
266   inline bool
267   operator==(const error_condition& __lhs, const error_code& __rhs)
268   {
269     return (__rhs.category().equivalent(__rhs.value(), __lhs)
270             || __lhs.category().equivalent(__rhs, __lhs.value()));
271   }
273   inline bool
274   operator==(const error_condition& __lhs, const error_condition& __rhs)
275   {
276     return (__lhs.category() == __rhs.category()
277             && __lhs.value() == __rhs.value());
278   }
280   inline bool
281   operator!=(const error_code& __lhs, const error_code& __rhs)
282   { return !(__lhs == __rhs); }
284   inline bool
285   operator!=(const error_code& __lhs, const error_condition& __rhs)
286   { return !(__lhs == __rhs); }
288   inline bool
289   operator!=(const error_condition& __lhs, const error_code& __rhs)
290   { return !(__lhs == __rhs); }
292   inline bool
293   operator!=(const error_condition& __lhs, const error_condition& __rhs)
294   { return !(__lhs == __rhs); }
297   /** 
298    *  @brief Thrown to indicate error code of underlying system.
299    *
300    *  @ingroup exceptions
301    */
302   class system_error : public std::runtime_error
303   {
304   private:
305     error_code  _M_code;
307   public:
308     system_error(error_code __ec = error_code())
309     : runtime_error(""), _M_code(__ec) { }
311     system_error(error_code __ec, const string& __what)
312     : runtime_error(__what), _M_code(__ec) { }
313     
314     /*
315      * TODO: Add const char* ctors to all exceptions.
316      *
317      * system_error(error_code __ec, const char* __what)
318      * : runtime_error(__what), _M_code(__ec) { }
319      *
320      * system_error(int __v, const error_category& __ecat, const char* __what)
321      * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
322      */
324     system_error(int __v, const error_category& __ecat)
325     : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
327     system_error(int __v, const error_category& __ecat, const string& __what)
328     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
330     virtual ~system_error() throw();
332     const error_code& 
333     code() const throw() { return _M_code; }
334   };
336 _GLIBCXX_END_NAMESPACE
338 #endif // __GXX_EXPERIMENTAL_CXX0X__
340 #endif // _GLIBCXX_SYSTEM_ERROR