libstdc++
nested_exception.h
Go to the documentation of this file.
1 // Nested Exception support header (nested_exception class) for -*- C++ -*-
2 
3 // Copyright (C) 2009-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 bits/nested_exception.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{exception}
28  */
29 
30 #ifndef _GLIBCXX_NESTED_EXCEPTION_H
31 #define _GLIBCXX_NESTED_EXCEPTION_H 1
32 
33 #pragma GCC visibility push(default)
34 
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
37 #else
38 
39 #include <bits/c++config.h>
40 
41 extern "C++" {
42 
43 namespace std
44 {
45  /**
46  * @addtogroup exceptions
47  * @{
48  */
49 
50  /// Exception class with exception_ptr data member.
52  {
53  exception_ptr _M_ptr;
54 
55  public:
56  nested_exception() noexcept : _M_ptr(current_exception()) { }
57 
58  nested_exception(const nested_exception&) = default;
59 
60  nested_exception& operator=(const nested_exception&) = default;
61 
62  virtual ~nested_exception() noexcept;
63 
64  void
65  rethrow_nested() const __attribute__ ((__noreturn__))
66  { rethrow_exception(_M_ptr); }
67 
68  exception_ptr
69  nested_ptr() const
70  { return _M_ptr; }
71  };
72 
73  template<typename _Except>
74  struct _Nested_exception : public _Except, public nested_exception
75  {
76  explicit _Nested_exception(_Except&& __ex)
77  : _Except(static_cast<_Except&&>(__ex))
78  { }
79  };
80 
81  template<typename _Ex>
82  struct __get_nested_helper
83  {
84  static const nested_exception*
85  _S_get(const _Ex& __ex)
86  { return dynamic_cast<const nested_exception*>(&__ex); }
87  };
88 
89  template<typename _Ex>
90  struct __get_nested_helper<_Ex*>
91  {
92  static const nested_exception*
93  _S_get(const _Ex* __ex)
94  { return dynamic_cast<const nested_exception*>(__ex); }
95  };
96 
97  template<typename _Ex>
98  inline const nested_exception*
99  __get_nested_exception(const _Ex& __ex)
100  { return __get_nested_helper<_Ex>::_S_get(__ex); }
101 
102  template<typename _Ex>
103  void
104  __throw_with_nested(_Ex&&, const nested_exception* = 0)
105  __attribute__ ((__noreturn__));
106 
107  template<typename _Ex>
108  void
109  __throw_with_nested(_Ex&&, ...) __attribute__ ((__noreturn__));
110 
111  // This function should never be called, but is needed to avoid a warning
112  // about ambiguous base classes when instantiating throw_with_nested<_Ex>()
113  // with a type that has an accessible nested_exception base.
114  template<typename _Ex>
115  inline void
116  __throw_with_nested(_Ex&& __ex, const nested_exception*)
117  { throw __ex; }
118 
119  template<typename _Ex>
120  inline void
121  __throw_with_nested(_Ex&& __ex, ...)
122  { throw _Nested_exception<_Ex>(static_cast<_Ex&&>(__ex)); }
123 
124  template<typename _Ex>
125  void
126  throw_with_nested(_Ex __ex) __attribute__ ((__noreturn__));
127 
128  /// If @p __ex is derived from nested_exception, @p __ex.
129  /// Else, an implementation-defined object derived from both.
130  template<typename _Ex>
131  inline void
133  {
134  if (__get_nested_exception(__ex))
135  throw __ex;
136  __throw_with_nested(static_cast<_Ex&&>(__ex), &__ex);
137  }
138 
139  /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
140  template<typename _Ex>
141  inline void
142  rethrow_if_nested(const _Ex& __ex)
143  {
144  if (const nested_exception* __nested = __get_nested_exception(__ex))
145  __nested->rethrow_nested();
146  }
147 
148  /// Overload, See N2619
149  inline void
151  { __ex.rethrow_nested(); }
152 
153  // @} group exceptions
154 } // namespace std
155 
156 } // extern "C++"
157 
158 #endif // C++11
159 
160 #pragma GCC visibility pop
161 
162 #endif // _GLIBCXX_NESTED_EXCEPTION_H
Exception class with exception_ptr data member.
exception_ptr current_exception() noexcept
void rethrow_if_nested(const _Ex &__ex)
If __ex is derived from nested_exception, __ex.rethrow_nested().
ISO C++ entities toplevel namespace is std.
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
void throw_with_nested(_Ex __ex) __attribute__((__noreturn__))
If __ex is derived from nested_exception, __ex. Else, an implementation-defined object derived from b...