* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / libstdc++-v3 / include / std / system_error
blob1ef64f46015d86b4747de7222abec876bc484739
1 // <system_error> -*- C++ -*-
3 // Copyright (C) 2007-2016 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 include/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 #if __cplusplus < 201103L
35 # include <bits/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 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47   class error_code;
48   class error_condition;
49   class system_error;
51   /// is_error_code_enum
52   template<typename _Tp>
53     struct is_error_code_enum : public false_type { };
55   /// is_error_condition_enum
56   template<typename _Tp>
57     struct is_error_condition_enum : public false_type { };
59   template<>
60     struct is_error_condition_enum<errc>
61     : public true_type { };
63 #if __cplusplus > 201402L
64   template <typename _Tp>
65     constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
66   template <typename _Tp>
67     constexpr bool is_error_condition_enum_v =
68       is_error_condition_enum<_Tp>::value;
69 #endif // C++17
70   inline namespace _V2 {
72   /// error_category
73   class error_category
74   {
75   public:
76     constexpr error_category() noexcept = default;
78     virtual ~error_category();
80     error_category(const error_category&) = delete;
81     error_category& operator=(const error_category&) = delete;
83     virtual const char*
84     name() const noexcept = 0;
86     // We need two different virtual functions here, one returning a
87     // COW string and one returning an SSO string. Their positions in the
88     // vtable must be consistent for dynamic dispatch to work, but which one
89     // the name "message()" finds depends on which ABI the caller is using.
90 #if _GLIBCXX_USE_CXX11_ABI
91   private:
92     _GLIBCXX_DEFAULT_ABI_TAG
93     virtual __cow_string
94     _M_message(int) const;
96   public:
97     _GLIBCXX_DEFAULT_ABI_TAG
98     virtual string
99     message(int) const = 0;
100 #else
101     virtual string
102     message(int) const = 0;
104   private:
105     virtual __sso_string
106     _M_message(int) const;
107 #endif
109   public:
110     virtual error_condition
111     default_error_condition(int __i) const noexcept;
113     virtual bool
114     equivalent(int __i, const error_condition& __cond) const noexcept;
116     virtual bool
117     equivalent(const error_code& __code, int __i) const noexcept;
119     bool
120     operator<(const error_category& __other) const noexcept
121     { return less<const error_category*>()(this, &__other); }
123     bool
124     operator==(const error_category& __other) const noexcept
125     { return this == &__other; }
127     bool
128     operator!=(const error_category& __other) const noexcept
129     { return this != &__other; }
130   };
132   // DR 890.
133   _GLIBCXX_CONST const error_category& system_category() noexcept;
134   _GLIBCXX_CONST const error_category& generic_category() noexcept;
136   } // end inline namespace
138   error_code make_error_code(errc) noexcept;
140   template<typename _Tp>
141     struct hash;
143   /// error_code
144   // Implementation-specific error identification
145   struct error_code
146   {
147     error_code() noexcept
148     : _M_value(0), _M_cat(&system_category()) { }
150     error_code(int __v, const error_category& __cat) noexcept
151     : _M_value(__v), _M_cat(&__cat) { }
153     template<typename _ErrorCodeEnum, typename = typename
154              enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
155       error_code(_ErrorCodeEnum __e) noexcept
156       { *this = make_error_code(__e); }
158     void
159     assign(int __v, const error_category& __cat) noexcept
160     {
161       _M_value = __v;
162       _M_cat = &__cat;
163     }
165     void
166     clear() noexcept
167     { assign(0, system_category()); }
169     // DR 804.
170     template<typename _ErrorCodeEnum>
171       typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
172                          error_code&>::type
173       operator=(_ErrorCodeEnum __e) noexcept
174       { return *this = make_error_code(__e); }
176     int
177     value() const noexcept { return _M_value; }
179     const error_category&
180     category() const noexcept { return *_M_cat; }
182     error_condition
183     default_error_condition() const noexcept;
185     _GLIBCXX_DEFAULT_ABI_TAG
186     string
187     message() const
188     { return category().message(value()); }
190     explicit operator bool() const noexcept
191     { return _M_value != 0; }
193     // DR 804.
194   private:
195     friend class hash<error_code>;
197     int                         _M_value;
198     const error_category*       _M_cat;
199   };
201   // 19.4.2.6 non-member functions
202   inline error_code
203   make_error_code(errc __e) noexcept
204   { return error_code(static_cast<int>(__e), generic_category()); }
206   inline bool
207   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
208   {
209     return (__lhs.category() < __rhs.category()
210             || (__lhs.category() == __rhs.category()
211                 && __lhs.value() < __rhs.value()));
212   }
214   template<typename _CharT, typename _Traits>
215     basic_ostream<_CharT, _Traits>&
216     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
217     { return (__os << __e.category().name() << ':' << __e.value()); }
219   error_condition make_error_condition(errc) noexcept;
221   /// error_condition
222   // Portable error identification
223   struct error_condition
224   {
225     error_condition() noexcept
226     : _M_value(0), _M_cat(&generic_category()) { }
228     error_condition(int __v, const error_category& __cat) noexcept
229     : _M_value(__v), _M_cat(&__cat) { }
231     template<typename _ErrorConditionEnum, typename = typename
232          enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
233       error_condition(_ErrorConditionEnum __e) noexcept
234       { *this = make_error_condition(__e); }
236     void
237     assign(int __v, const error_category& __cat) noexcept
238     {
239       _M_value = __v;
240       _M_cat = &__cat;
241     }
243     // DR 804.
244     template<typename _ErrorConditionEnum>
245       typename enable_if<is_error_condition_enum
246                          <_ErrorConditionEnum>::value, error_condition&>::type
247       operator=(_ErrorConditionEnum __e) noexcept
248       { return *this = make_error_condition(__e); }
250     void
251     clear() noexcept
252     { assign(0, generic_category()); }
254     // 19.4.3.4 observers
255     int
256     value() const noexcept { return _M_value; }
258     const error_category&
259     category() const noexcept { return *_M_cat; }
261     _GLIBCXX_DEFAULT_ABI_TAG
262     string
263     message() const
264     { return category().message(value()); }
266     explicit operator bool() const noexcept
267     { return _M_value != 0; }
269     // DR 804.
270   private:
271     int                         _M_value;
272     const error_category*       _M_cat;
273   };
275   // 19.4.3.6 non-member functions
276   inline error_condition
277   make_error_condition(errc __e) noexcept
278   { return error_condition(static_cast<int>(__e), generic_category()); }
280   inline bool
281   operator<(const error_condition& __lhs,
282             const error_condition& __rhs) noexcept
283   {
284     return (__lhs.category() < __rhs.category()
285             || (__lhs.category() == __rhs.category()
286                 && __lhs.value() < __rhs.value()));
287   }
289   // 19.4.4 Comparison operators
290   inline bool
291   operator==(const error_code& __lhs, const error_code& __rhs) noexcept
292   { return (__lhs.category() == __rhs.category()
293             && __lhs.value() == __rhs.value()); }
295   inline bool
296   operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
297   {
298     return (__lhs.category().equivalent(__lhs.value(), __rhs)
299             || __rhs.category().equivalent(__lhs, __rhs.value()));
300   }
302   inline bool
303   operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
304   {
305     return (__rhs.category().equivalent(__rhs.value(), __lhs)
306             || __lhs.category().equivalent(__rhs, __lhs.value()));
307   }
309   inline bool
310   operator==(const error_condition& __lhs,
311              const error_condition& __rhs) noexcept
312   {
313     return (__lhs.category() == __rhs.category()
314             && __lhs.value() == __rhs.value());
315   }
317   inline bool
318   operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
319   { return !(__lhs == __rhs); }
321   inline bool
322   operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
323   { return !(__lhs == __rhs); }
325   inline bool
326   operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
327   { return !(__lhs == __rhs); }
329   inline bool
330   operator!=(const error_condition& __lhs,
331              const error_condition& __rhs) noexcept
332   { return !(__lhs == __rhs); }
335   /**
336    *  @brief Thrown to indicate error code of underlying system.
337    *
338    *  @ingroup exceptions
339    */
340   class system_error : public std::runtime_error
341   {
342   private:
343     error_code  _M_code;
345   public:
346     system_error(error_code __ec = error_code())
347     : runtime_error(__ec.message()), _M_code(__ec) { }
349     system_error(error_code __ec, const string& __what)
350     : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
352     system_error(error_code __ec, const char* __what)
353     : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
355     system_error(int __v, const error_category& __ecat, const char* __what)
356     : system_error(error_code(__v, __ecat), __what) { }
358     system_error(int __v, const error_category& __ecat)
359     : runtime_error(error_code(__v, __ecat).message()),
360       _M_code(__v, __ecat) { }
362     system_error(int __v, const error_category& __ecat, const string& __what)
363     : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
364       _M_code(__v, __ecat) { }
366     virtual ~system_error() noexcept;
368     const error_code&
369     code() const noexcept { return _M_code; }
370   };
372 _GLIBCXX_END_NAMESPACE_VERSION
373 } // namespace
375 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
377 #include <bits/functional_hash.h>
379 namespace std _GLIBCXX_VISIBILITY(default)
381 _GLIBCXX_BEGIN_NAMESPACE_VERSION
383   // DR 1182.
384   /// std::hash specialization for error_code.
385   template<>
386     struct hash<error_code>
387     : public __hash_base<size_t, error_code>
388     {
389       size_t
390       operator()(const error_code& __e) const noexcept
391       {
392         const size_t __tmp = std::_Hash_impl::hash(__e._M_value);
393         return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp);
394       }
395     };
397 _GLIBCXX_END_NAMESPACE_VERSION
398 } // namespace
400 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
402 #endif // C++11
404 #endif // _GLIBCXX_SYSTEM_ERROR