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-2015 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&) noexcept = default;
59 
60  nested_exception& operator=(const nested_exception&) noexcept = default;
61 
62  virtual ~nested_exception() noexcept;
63 
64  [[noreturn]]
65  void
66  rethrow_nested() const
67  {
68  if (_M_ptr)
69  rethrow_exception(_M_ptr);
71  }
72 
74  nested_ptr() const noexcept
75  { return _M_ptr; }
76  };
77 
78  template<typename _Except>
79  struct _Nested_exception : public _Except, public nested_exception
80  {
81  explicit _Nested_exception(const _Except& __ex)
82  : _Except(__ex)
83  { }
84 
85  explicit _Nested_exception(_Except&& __ex)
86  : _Except(static_cast<_Except&&>(__ex))
87  { }
88  };
89 
90  template<typename _Tp,
91  bool __with_nested = !__is_base_of(nested_exception, _Tp)>
92  struct _Throw_with_nested_impl
93  {
94  template<typename _Up>
95  static void _S_throw(_Up&& __t)
96  { throw _Nested_exception<_Tp>{static_cast<_Up&&>(__t)}; }
97  };
98 
99  template<typename _Tp>
100  struct _Throw_with_nested_impl<_Tp, false>
101  {
102  template<typename _Up>
103  static void _S_throw(_Up&& __t)
104  { throw static_cast<_Up&&>(__t); }
105  };
106 
107  template<typename _Tp, bool = __is_class(_Tp) && !__is_final(_Tp)>
108  struct _Throw_with_nested_helper : _Throw_with_nested_impl<_Tp>
109  { };
110 
111  template<typename _Tp>
112  struct _Throw_with_nested_helper<_Tp, false>
113  : _Throw_with_nested_impl<_Tp, false>
114  { };
115 
116  template<typename _Tp>
117  struct _Throw_with_nested_helper<_Tp&, false>
118  : _Throw_with_nested_helper<_Tp>
119  { };
120 
121  template<typename _Tp>
122  struct _Throw_with_nested_helper<_Tp&&, false>
123  : _Throw_with_nested_helper<_Tp>
124  { };
125 
126  /// If @p __t is derived from nested_exception, throws @p __t.
127  /// Else, throws an implementation-defined object derived from both.
128  template<typename _Tp>
129  [[noreturn]]
130  inline void
131  throw_with_nested(_Tp&& __t)
132  {
133  _Throw_with_nested_helper<_Tp>::_S_throw(static_cast<_Tp&&>(__t));
134  }
135 
136  template<typename _Tp, bool = __is_polymorphic(_Tp)>
137  struct _Rethrow_if_nested_impl
138  {
139  static void _S_rethrow(const _Tp& __t)
140  {
141  if (auto __tp = dynamic_cast<const nested_exception*>(&__t))
142  __tp->rethrow_nested();
143  }
144  };
145 
146  template<typename _Tp>
147  struct _Rethrow_if_nested_impl<_Tp, false>
148  {
149  static void _S_rethrow(const _Tp&) { }
150  };
151 
152  /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
153  template<typename _Ex>
154  inline void
155  rethrow_if_nested(const _Ex& __ex)
156  {
157  _Rethrow_if_nested_impl<_Ex>::_S_rethrow(__ex);
158  }
159 
160  // @} group exceptions
161 } // namespace std
162 
163 } // extern "C++"
164 
165 #endif // C++11
166 
167 #pragma GCC visibility pop
168 
169 #endif // _GLIBCXX_NESTED_EXCEPTION_H
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
void terminate() noexcept __attribute__((__noreturn__))
An opaque pointer to an arbitrary exception.
Definition: exception_ptr.h:71
ISO C++ entities toplevel namespace is std.
void rethrow_if_nested(const _Ex &__ex)
If __ex is derived from nested_exception, __ex.rethrow_nested().
void throw_with_nested(_Tp &&__t)
If __t is derived from nested_exception, throws __t. Else, throws an implementation-defined object de...
exception_ptr current_exception() noexcept
Exception class with exception_ptr data member.