libstdc++
system_error
Go to the documentation of this file.
1 // <system_error> -*- C++ -*-
2 
3 // Copyright (C) 2007-2017 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 system_error;
50 
51  /// is_error_code_enum
52  template<typename _Tp>
53  struct is_error_code_enum : public false_type { };
54 
55  /// is_error_condition_enum
56  template<typename _Tp>
57  struct is_error_condition_enum : public false_type { };
58 
59  template<>
60  struct is_error_condition_enum<errc>
61  : public true_type { };
62 
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 {
71 
72  /// error_category
73  class error_category
74  {
75  public:
76  constexpr error_category() noexcept = default;
77 
78  virtual ~error_category();
79 
80  error_category(const error_category&) = delete;
81  error_category& operator=(const error_category&) = delete;
82 
83  virtual const char*
84  name() const noexcept = 0;
85 
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;
95 
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;
103 
104  private:
105  virtual __sso_string
106  _M_message(int) const;
107 #endif
108 
109  public:
110  virtual error_condition
111  default_error_condition(int __i) const noexcept;
112 
113  virtual bool
114  equivalent(int __i, const error_condition& __cond) const noexcept;
115 
116  virtual bool
117  equivalent(const error_code& __code, int __i) const noexcept;
118 
119  bool
120  operator<(const error_category& __other) const noexcept
121  { return less<const error_category*>()(this, &__other); }
122 
123  bool
124  operator==(const error_category& __other) const noexcept
125  { return this == &__other; }
126 
127  bool
128  operator!=(const error_category& __other) const noexcept
129  { return this != &__other; }
130  };
131 
132  // DR 890.
133  _GLIBCXX_CONST const error_category& system_category() noexcept;
134  _GLIBCXX_CONST const error_category& generic_category() noexcept;
135 
136  } // end inline namespace
137 
138  error_code make_error_code(errc) noexcept;
139 
140  template<typename _Tp>
141  struct hash;
142 
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()) { }
149 
150  error_code(int __v, const error_category& __cat) noexcept
151  : _M_value(__v), _M_cat(&__cat) { }
152 
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); }
157 
158  void
159  assign(int __v, const error_category& __cat) noexcept
160  {
161  _M_value = __v;
162  _M_cat = &__cat;
163  }
164 
165  void
166  clear() noexcept
167  { assign(0, system_category()); }
168 
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); }
175 
176  int
177  value() const noexcept { return _M_value; }
178 
179  const error_category&
180  category() const noexcept { return *_M_cat; }
181 
182  error_condition
183  default_error_condition() const noexcept;
184 
185  _GLIBCXX_DEFAULT_ABI_TAG
186  string
187  message() const
188  { return category().message(value()); }
189 
190  explicit operator bool() const noexcept
191  { return _M_value != 0; }
192 
193  // DR 804.
194  private:
195  friend class hash<error_code>;
196 
197  int _M_value;
198  const error_category* _M_cat;
199  };
200 
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()); }
205 
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  }
213 
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()); }
218 
219  error_condition make_error_condition(errc) noexcept;
220 
221  /// error_condition
222  // Portable error identification
223  struct error_condition
224  {
225  error_condition() noexcept
226  : _M_value(0), _M_cat(&generic_category()) { }
227 
228  error_condition(int __v, const error_category& __cat) noexcept
229  : _M_value(__v), _M_cat(&__cat) { }
230 
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); }
235 
236  void
237  assign(int __v, const error_category& __cat) noexcept
238  {
239  _M_value = __v;
240  _M_cat = &__cat;
241  }
242 
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); }
249 
250  void
251  clear() noexcept
252  { assign(0, generic_category()); }
253 
254  // 19.4.3.4 observers
255  int
256  value() const noexcept { return _M_value; }
257 
258  const error_category&
259  category() const noexcept { return *_M_cat; }
260 
261  _GLIBCXX_DEFAULT_ABI_TAG
262  string
263  message() const
264  { return category().message(value()); }
265 
266  explicit operator bool() const noexcept
267  { return _M_value != 0; }
268 
269  // DR 804.
270  private:
271  int _M_value;
272  const error_category* _M_cat;
273  };
274 
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()); }
279 
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  }
288 
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()); }
294 
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  }
301 
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  }
308 
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  }
316 
317  inline bool
318  operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
319  { return !(__lhs == __rhs); }
320 
321  inline bool
322  operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
323  { return !(__lhs == __rhs); }
324 
325  inline bool
326  operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
327  { return !(__lhs == __rhs); }
328 
329  inline bool
330  operator!=(const error_condition& __lhs,
331  const error_condition& __rhs) noexcept
332  { return !(__lhs == __rhs); }
333 
334 
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;
344 
345  public:
346  system_error(error_code __ec = error_code())
347  : runtime_error(__ec.message()), _M_code(__ec) { }
348 
349  system_error(error_code __ec, const string& __what)
350  : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
351 
352  system_error(error_code __ec, const char* __what)
353  : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
354 
355  system_error(int __v, const error_category& __ecat, const char* __what)
356  : system_error(error_code(__v, __ecat), __what) { }
357 
358  system_error(int __v, const error_category& __ecat)
359  : runtime_error(error_code(__v, __ecat).message()),
360  _M_code(__v, __ecat) { }
361 
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) { }
365 
366  virtual ~system_error() noexcept;
367 
368  const error_code&
369  code() const noexcept { return _M_code; }
370  };
371 
372 _GLIBCXX_END_NAMESPACE_VERSION
373 } // namespace
374 
375 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
376 
377 #include <bits/functional_hash.h>
378 
379 namespace std _GLIBCXX_VISIBILITY(default)
380 {
381 _GLIBCXX_BEGIN_NAMESPACE_VERSION
382 
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  };
396 
397 _GLIBCXX_END_NAMESPACE_VERSION
398 } // namespace
399 
400 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
401 
402 #endif // C++11
403 
404 #endif // _GLIBCXX_SYSTEM_ERROR