Home | History | Annotate | Download | only in include
      1 // <system_error> -*- C++ -*-
      2 
      3 // Copyright (C) 2007-2014 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 include/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 #if __cplusplus < 201103L
     35 # include <bits/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 namespace std _GLIBCXX_VISIBILITY(default)
     44 {
     45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     46 
     47   class error_code;
     48   class error_condition;
     49   class error_category;
     50   class system_error;
     51 
     52   /// is_error_code_enum
     53   template<typename _Tp>
     54     struct is_error_code_enum : public false_type { };
     55 
     56   /// is_error_condition_enum
     57   template<typename _Tp>
     58     struct is_error_condition_enum : public false_type { };
     59 
     60   template<> 
     61     struct is_error_condition_enum<errc>
     62     : public true_type { };
     63 
     64 
     65   /// error_category
     66   class error_category
     67   {
     68   public:
     69 #ifdef _GLIBCXX_COMPATIBILITY_CXX0X
     70     error_category() noexcept;
     71 #else
     72     constexpr error_category() noexcept = default;
     73 #endif
     74 
     75     virtual ~error_category();
     76 
     77     error_category(const error_category&) = delete;
     78     error_category& operator=(const error_category&) = delete;
     79 
     80     virtual const char* 
     81     name() const noexcept = 0;
     82 
     83     virtual string 
     84     message(int) const = 0;
     85 
     86     virtual error_condition
     87     default_error_condition(int __i) const noexcept;
     88 
     89     virtual bool 
     90     equivalent(int __i, const error_condition& __cond) const noexcept;
     91 
     92     virtual bool 
     93     equivalent(const error_code& __code, int __i) const noexcept;
     94 
     95     bool 
     96     operator<(const error_category& __other) const noexcept
     97     { return less<const error_category*>()(this, &__other); }
     98 
     99     bool 
    100     operator==(const error_category& __other) const noexcept
    101     { return this == &__other; }
    102 
    103     bool 
    104     operator!=(const error_category& __other) const noexcept
    105     { return this != &__other; }
    106   };
    107 
    108   // DR 890.
    109   _GLIBCXX_CONST const error_category& system_category() noexcept;
    110   _GLIBCXX_CONST const error_category& generic_category() noexcept;
    111 
    112   error_code make_error_code(errc) noexcept;
    113 
    114   template<typename _Tp>
    115     struct hash;
    116 
    117   /// error_code
    118   // Implementation-specific error identification
    119   struct error_code
    120   {
    121     error_code() noexcept
    122     : _M_value(0), _M_cat(&system_category()) { }
    123 
    124     error_code(int __v, const error_category& __cat) noexcept
    125     : _M_value(__v), _M_cat(&__cat) { }
    126 
    127     template<typename _ErrorCodeEnum, typename = typename
    128 	     enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
    129       error_code(_ErrorCodeEnum __e) noexcept
    130       { *this = make_error_code(__e); }
    131 
    132     void 
    133     assign(int __v, const error_category& __cat) noexcept
    134     {
    135       _M_value = __v;
    136       _M_cat = &__cat; 
    137     }
    138 
    139     void 
    140     clear() noexcept
    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) noexcept
    148       { return *this = make_error_code(__e); }
    149 
    150     int
    151     value() const noexcept { return _M_value; }
    152       
    153     const error_category&  
    154     category() const noexcept { return *_M_cat; }
    155 
    156     error_condition 
    157     default_error_condition() const noexcept;
    158 
    159     string 
    160     message() const
    161     { return category().message(value()); }
    162 
    163     explicit operator bool() const noexcept
    164     { return _M_value != 0 ? true : false; }
    165 
    166     // DR 804.
    167   private:
    168     friend class hash<error_code>;
    169 
    170     int            		_M_value;
    171     const error_category* 	_M_cat;
    172   };
    173 
    174   // 19.4.2.6 non-member functions
    175   inline error_code
    176   make_error_code(errc __e) noexcept
    177   { return error_code(static_cast<int>(__e), generic_category()); }
    178 
    179   inline bool
    180   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
    181   { 
    182     return (__lhs.category() < __rhs.category()
    183 	    || (__lhs.category() == __rhs.category()
    184 		&& __lhs.value() < __rhs.value()));
    185   }
    186 
    187   template<typename _CharT, typename _Traits>
    188     basic_ostream<_CharT, _Traits>&
    189     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
    190     { return (__os << __e.category().name() << ':' << __e.value()); }
    191 
    192   error_condition make_error_condition(errc) noexcept;
    193 
    194   /// error_condition
    195   // Portable error identification
    196   struct error_condition 
    197   {
    198     error_condition() noexcept
    199     : _M_value(0), _M_cat(&generic_category()) { }
    200 
    201     error_condition(int __v, const error_category& __cat) noexcept
    202     : _M_value(__v), _M_cat(&__cat) { }
    203 
    204     template<typename _ErrorConditionEnum, typename = typename
    205 	 enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
    206       error_condition(_ErrorConditionEnum __e) noexcept
    207       { *this = make_error_condition(__e); }
    208 
    209     void
    210     assign(int __v, const error_category& __cat) noexcept
    211     {
    212       _M_value = __v;
    213       _M_cat = &__cat;
    214     }
    215 
    216     // DR 804.
    217     template<typename _ErrorConditionEnum>
    218       typename enable_if<is_error_condition_enum
    219 			 <_ErrorConditionEnum>::value, error_condition&>::type
    220       operator=(_ErrorConditionEnum __e) noexcept
    221       { return *this = make_error_condition(__e); }
    222 
    223     void 
    224     clear() noexcept
    225     { assign(0, generic_category()); }
    226 
    227     // 19.4.3.4 observers
    228     int
    229     value() const noexcept { return _M_value; }
    230 
    231     const error_category&
    232     category() const noexcept { return *_M_cat; }
    233 
    234     string 
    235     message() const
    236     { return category().message(value()); }
    237 
    238     explicit operator bool() const noexcept
    239     { return _M_value != 0 ? true : false; }
    240 
    241     // DR 804.
    242   private:
    243     int 			_M_value;
    244     const error_category* 	_M_cat;
    245   };
    246 
    247   // 19.4.3.6 non-member functions
    248   inline error_condition
    249   make_error_condition(errc __e) noexcept
    250   { return error_condition(static_cast<int>(__e), generic_category()); }
    251 
    252   inline bool 
    253   operator<(const error_condition& __lhs,
    254 	    const error_condition& __rhs) noexcept
    255   {
    256     return (__lhs.category() < __rhs.category()
    257 	    || (__lhs.category() == __rhs.category()
    258 		&& __lhs.value() < __rhs.value()));
    259   }
    260 
    261   // 19.4.4 Comparison operators
    262   inline bool
    263   operator==(const error_code& __lhs, const error_code& __rhs) noexcept
    264   { return (__lhs.category() == __rhs.category()
    265 	    && __lhs.value() == __rhs.value()); }
    266 
    267   inline bool
    268   operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
    269   {
    270     return (__lhs.category().equivalent(__lhs.value(), __rhs)
    271 	    || __rhs.category().equivalent(__lhs, __rhs.value()));
    272   }
    273 
    274   inline bool
    275   operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
    276   {
    277     return (__rhs.category().equivalent(__rhs.value(), __lhs)
    278 	    || __lhs.category().equivalent(__rhs, __lhs.value()));
    279   }
    280 
    281   inline bool
    282   operator==(const error_condition& __lhs,
    283 	     const error_condition& __rhs) noexcept
    284   {
    285     return (__lhs.category() == __rhs.category()
    286 	    && __lhs.value() == __rhs.value());
    287   }
    288 
    289   inline bool
    290   operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
    291   { return !(__lhs == __rhs); }
    292 
    293   inline bool
    294   operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
    295   { return !(__lhs == __rhs); }
    296 
    297   inline bool
    298   operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
    299   { return !(__lhs == __rhs); }
    300 
    301   inline bool
    302   operator!=(const error_condition& __lhs,
    303 	     const error_condition& __rhs) noexcept
    304   { return !(__lhs == __rhs); }
    305 
    306 
    307   /** 
    308    *  @brief Thrown to indicate error code of underlying system.
    309    *
    310    *  @ingroup exceptions
    311    */
    312   class system_error : public std::runtime_error
    313   {
    314   private:
    315     error_code 	_M_code;
    316 
    317   public:
    318     system_error(error_code __ec = error_code())
    319     : runtime_error(__ec.message()), _M_code(__ec) { }
    320 
    321     system_error(error_code __ec, const string& __what)
    322     : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
    323 
    324     /*
    325      * TODO: Add const char* ctors to all exceptions.
    326      *
    327      * system_error(error_code __ec, const char* __what)
    328      * : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
    329      *
    330      * system_error(int __v, const error_category& __ecat, const char* __what)
    331      * : runtime_error(__what + (": " + __ec.message())),
    332      *   _M_code(error_code(__v, __ecat)) { }
    333      */
    334 
    335     system_error(int __v, const error_category& __ecat)
    336     : runtime_error(error_code(__v, __ecat).message()),
    337       _M_code(__v, __ecat) { }
    338 
    339     system_error(int __v, const error_category& __ecat, const string& __what)
    340     : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
    341       _M_code(__v, __ecat) { }
    342 
    343     virtual ~system_error() noexcept;
    344 
    345     const error_code& 
    346     code() const noexcept { return _M_code; }
    347   };
    348 
    349 _GLIBCXX_END_NAMESPACE_VERSION
    350 } // namespace
    351 
    352 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
    353 
    354 #include <bits/functional_hash.h>
    355 
    356 namespace std _GLIBCXX_VISIBILITY(default)
    357 {
    358 _GLIBCXX_BEGIN_NAMESPACE_VERSION
    359 
    360   // DR 1182.
    361   /// std::hash specialization for error_code.
    362   template<>
    363     struct hash<error_code>
    364     : public __hash_base<size_t, error_code>
    365     {
    366       size_t
    367       operator()(const error_code& __e) const noexcept
    368       {
    369 	const size_t __tmp = std::_Hash_impl::hash(__e._M_value);
    370 	return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp);
    371       }
    372     };
    373 
    374 _GLIBCXX_END_NAMESPACE_VERSION
    375 } // namespace
    376 
    377 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
    378 
    379 #endif // C++11
    380 
    381 #endif // _GLIBCXX_SYSTEM_ERROR
    382