system_error

Go to the documentation of this file.
00001 // <system_error> -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file system_error
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_SYSTEM_ERROR
00030 #define _GLIBCXX_SYSTEM_ERROR 1
00031 
00032 #pragma GCC system_header
00033 
00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00035 # include <c++0x_warning.h>
00036 #else
00037 
00038 #include <bits/c++config.h>
00039 #include <bits/error_constants.h>
00040 #include <iosfwd>
00041 #include <stdexcept>
00042 
00043 _GLIBCXX_BEGIN_NAMESPACE(std)
00044 
00045   class error_code;
00046   class error_condition;
00047   class error_category;
00048   class system_error;
00049 
00050   /// is_error_code_enum
00051   template<typename _Tp>
00052     struct is_error_code_enum : public false_type { };
00053 
00054   /// is_error_condition_enum
00055   template<typename _Tp>
00056     struct is_error_condition_enum : public false_type { };
00057 
00058   template<> 
00059     struct is_error_condition_enum<errc>
00060     : public true_type { };
00061 
00062 
00063   /// error_category
00064   class error_category
00065   {
00066   protected:
00067     error_category();
00068 
00069   public:
00070     virtual ~error_category() { }
00071 
00072     error_category(const error_category&) = delete;
00073     error_category& operator=(const error_category&) = delete;
00074 
00075     virtual const char* 
00076     name() const = 0;
00077 
00078     virtual string 
00079     message(int) const = 0;
00080 
00081     virtual error_condition
00082     default_error_condition(int __i) const;
00083 
00084     virtual bool 
00085     equivalent(int __i, const error_condition& __cond) const;
00086 
00087     virtual bool 
00088     equivalent(const error_code& __code, int __i) const;
00089 
00090     bool 
00091     operator<(const error_category& __other) const
00092     { return less<const error_category*>()(this, &__other); }
00093 
00094     bool 
00095     operator==(const error_category& __other) const
00096     { return this == &__other; }
00097 
00098     bool 
00099     operator!=(const error_category& __other) const
00100     { return this != &__other; }
00101   };
00102 
00103   inline error_category::error_category() = default;
00104 
00105   // DR 890.
00106   _GLIBCXX_CONST const error_category& system_category() throw ();
00107   _GLIBCXX_CONST const error_category& generic_category() throw ();
00108 
00109   error_code make_error_code(errc);
00110 
00111   /// error_code
00112   // Implementation-specific error identification
00113   struct error_code
00114   {
00115     error_code()
00116     : _M_value(0), _M_cat(&system_category()) { }
00117 
00118     error_code(int __v, const error_category& __cat)
00119     : _M_value(__v), _M_cat(&__cat) { }
00120 
00121     template<typename _ErrorCodeEnum>
00122       error_code(_ErrorCodeEnum __e,
00123       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type* = 0)
00124       { *this = make_error_code(__e); }
00125 
00126     void 
00127     assign(int __v, const error_category& __cat)
00128     {
00129       _M_value = __v;
00130       _M_cat = &__cat; 
00131     }
00132 
00133     void 
00134     clear()
00135     { assign(0, system_category()); }
00136 
00137     // DR 804.
00138     template<typename _ErrorCodeEnum>
00139       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
00140              error_code&>::type
00141       operator=(_ErrorCodeEnum __e)
00142       { return *this = make_error_code(__e); }
00143 
00144     int
00145     value() const { return _M_value; }
00146       
00147     const error_category&  
00148     category() const { return *_M_cat; }
00149 
00150     error_condition 
00151     default_error_condition() const;
00152 
00153     string 
00154     message() const
00155     { return category().message(value()); }
00156 
00157     explicit operator bool() const
00158     { return _M_value != 0 ? true : false; }
00159 
00160     // DR 804.
00161   private:
00162     int                 _M_value;
00163     const error_category*   _M_cat;
00164   };
00165 
00166   // 19.4.2.6 non-member functions
00167   inline error_code
00168   make_error_code(errc __e)
00169   { return error_code(static_cast<int>(__e), generic_category()); }
00170 
00171   inline bool
00172   operator<(const error_code& __lhs, const error_code& __rhs)
00173   { 
00174     return (__lhs.category() < __rhs.category()
00175         || (__lhs.category() == __rhs.category()
00176         && __lhs.value() < __rhs.value()));
00177   }
00178 
00179   template<typename _CharT, typename _Traits>
00180     basic_ostream<_CharT, _Traits>&
00181     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
00182     { return (__os << __e.category().name() << ':' << __e.value()); }
00183 
00184   error_condition make_error_condition(errc);
00185 
00186   /// error_condition
00187   // Portable error identification
00188   struct error_condition 
00189   {
00190     error_condition()
00191     : _M_value(0), _M_cat(&generic_category()) { }
00192 
00193     error_condition(int __v, const error_category& __cat)     
00194     : _M_value(__v), _M_cat(&__cat) { }
00195 
00196     template<typename _ErrorConditionEnum>
00197       error_condition(_ErrorConditionEnum __e,
00198               typename enable_if<is_error_condition_enum
00199                               <_ErrorConditionEnum>::value>::type* = 0)
00200       { *this = make_error_condition(__e); }
00201 
00202     void
00203     assign(int __v, const error_category& __cat)
00204     {
00205       _M_value = __v;
00206       _M_cat = &__cat;
00207     }
00208 
00209     // DR 804.
00210     template<typename _ErrorConditionEnum>
00211       typename enable_if<is_error_condition_enum
00212              <_ErrorConditionEnum>::value, error_condition&>::type
00213       operator=(_ErrorConditionEnum __e)
00214       { return *this = make_error_condition(__e); }
00215 
00216     void 
00217     clear()
00218     { assign(0, generic_category()); }
00219 
00220     // 19.4.3.4 observers
00221     int 
00222     value() const { return _M_value; }
00223 
00224     const error_category&
00225     category() const { return *_M_cat; }
00226 
00227     string 
00228     message() const
00229     { return category().message(value()); }
00230 
00231     explicit operator bool() const
00232     { return _M_value != 0 ? true : false; }
00233 
00234     // DR 804.
00235   private:
00236     int             _M_value;
00237     const error_category*   _M_cat;
00238   };
00239 
00240   // 19.4.3.6 non-member functions
00241   inline error_condition
00242   make_error_condition(errc __e)
00243   { return error_condition(static_cast<int>(__e), generic_category()); }
00244 
00245   inline bool 
00246   operator<(const error_condition& __lhs, const error_condition& __rhs)
00247   {
00248     return (__lhs.category() < __rhs.category()
00249         || (__lhs.category() == __rhs.category()
00250         && __lhs.value() < __rhs.value()));
00251   }
00252 
00253   // 19.4.4 Comparison operators
00254   inline bool
00255   operator==(const error_code& __lhs, const error_code& __rhs)
00256   { return (__lhs.category() == __rhs.category()
00257         && __lhs.value() == __rhs.value()); }
00258 
00259   inline bool
00260   operator==(const error_code& __lhs, const error_condition& __rhs)
00261   {
00262     return (__lhs.category().equivalent(__lhs.value(), __rhs)
00263         || __rhs.category().equivalent(__lhs, __rhs.value()));
00264   }
00265 
00266   inline bool
00267   operator==(const error_condition& __lhs, const error_code& __rhs)
00268   {
00269     return (__rhs.category().equivalent(__rhs.value(), __lhs)
00270         || __lhs.category().equivalent(__rhs, __lhs.value()));
00271   }
00272 
00273   inline bool
00274   operator==(const error_condition& __lhs, const error_condition& __rhs)
00275   {
00276     return (__lhs.category() == __rhs.category()
00277         && __lhs.value() == __rhs.value());
00278   }
00279 
00280   inline bool
00281   operator!=(const error_code& __lhs, const error_code& __rhs)
00282   { return !(__lhs == __rhs); }
00283 
00284   inline bool
00285   operator!=(const error_code& __lhs, const error_condition& __rhs)
00286   { return !(__lhs == __rhs); }
00287 
00288   inline bool
00289   operator!=(const error_condition& __lhs, const error_code& __rhs)
00290   { return !(__lhs == __rhs); }
00291 
00292   inline bool
00293   operator!=(const error_condition& __lhs, const error_condition& __rhs)
00294   { return !(__lhs == __rhs); }
00295 
00296 
00297   /** 
00298    *  @brief Thrown to indicate error code of underlying system.
00299    *
00300    *  @ingroup exceptions
00301    */
00302   class system_error : public std::runtime_error
00303   {
00304   private:
00305     error_code  _M_code;
00306 
00307   public:
00308     system_error(error_code __ec = error_code())
00309     : runtime_error(""), _M_code(__ec) { }
00310 
00311     system_error(error_code __ec, const string& __what)
00312     : runtime_error(__what), _M_code(__ec) { }
00313     
00314     /*
00315      * TODO: Add const char* ctors to all exceptions.
00316      *
00317      * system_error(error_code __ec, const char* __what)
00318      * : runtime_error(__what), _M_code(__ec) { }
00319      *
00320      * system_error(int __v, const error_category& __ecat, const char* __what)
00321      * : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
00322      */
00323 
00324     system_error(int __v, const error_category& __ecat)
00325     : runtime_error(""), _M_code(error_code(__v, __ecat)) { }
00326 
00327     system_error(int __v, const error_category& __ecat, const string& __what)
00328     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
00329 
00330     virtual ~system_error() throw();
00331 
00332     const error_code& 
00333     code() const throw() { return _M_code; }
00334   };
00335 
00336 _GLIBCXX_END_NAMESPACE
00337 
00338 #endif // __GXX_EXPERIMENTAL_CXX0X__
00339 
00340 #endif // _GLIBCXX_SYSTEM_ERROR
00341 

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1