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-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.
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 #include <bits/move.h>
41 
42 #if !(defined(__ARM_EABI__) && !defined(__ARM_PCS_VFP))
43 #if ATOMIC_INT_LOCK_FREE < 2
44 # error This platform does not support exception propagation.
45 #endif
46 #endif
47 
48 extern "C++" {
49 
50 namespace std
51 {
52  /**
53  * @addtogroup exceptions
54  * @{
55  */
56 
57  /// Exception class with exception_ptr data member.
59  {
60  exception_ptr _M_ptr;
61 
62  public:
63  nested_exception() noexcept : _M_ptr(current_exception()) { }
64 
65  nested_exception(const nested_exception&) noexcept = default;
66 
67  nested_exception& operator=(const nested_exception&) noexcept = default;
68 
69  virtual ~nested_exception() noexcept;
70 
71  [[noreturn]]
72  void
73  rethrow_nested() const
74  {
75  if (_M_ptr)
76  rethrow_exception(_M_ptr);
78  }
79 
81  nested_ptr() const noexcept
82  { return _M_ptr; }
83  };
84 
85  template<typename _Except>
86  struct _Nested_exception : public _Except, public nested_exception
87  {
88  explicit _Nested_exception(const _Except& __ex)
89  : _Except(__ex)
90  { }
91 
92  explicit _Nested_exception(_Except&& __ex)
93  : _Except(static_cast<_Except&&>(__ex))
94  { }
95  };
96 
97  // [except.nested]/8
98  // Throw an exception of unspecified type that is publicly derived from
99  // both remove_reference_t<_Tp> and nested_exception.
100  template<typename _Tp>
101  inline void
102  __throw_with_nested_impl(_Tp&& __t, true_type)
103  {
104  using _Up = typename remove_reference<_Tp>::type;
105  throw _Nested_exception<_Up>{std::forward<_Tp>(__t)};
106  }
107 
108  template<typename _Tp>
109  inline void
110  __throw_with_nested_impl(_Tp&& __t, false_type)
111  { throw std::forward<_Tp>(__t); }
112 
113  /// If @p __t is derived from nested_exception, throws @p __t.
114  /// Else, throws an implementation-defined object derived from both.
115  template<typename _Tp>
116  [[noreturn]]
117  inline void
118  throw_with_nested(_Tp&& __t)
119  {
120  using _Up = typename decay<_Tp>::type;
121  using _CopyConstructible
122  = __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>;
123  static_assert(_CopyConstructible::value,
124  "throw_with_nested argument must be CopyConstructible");
125  using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>,
126  __not_<is_base_of<nested_exception, _Up>>>;
127  std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{});
128  }
129 
130  // Determine if dynamic_cast<const nested_exception&> would be well-formed.
131  template<typename _Tp>
132  using __rethrow_if_nested_cond = typename enable_if<
133  __and_<is_polymorphic<_Tp>,
134  __or_<__not_<is_base_of<nested_exception, _Tp>>,
135  is_convertible<_Tp*, nested_exception*>>>::value
136  >::type;
137 
138  // Attempt dynamic_cast to nested_exception and call rethrow_nested().
139  template<typename _Ex>
140  inline __rethrow_if_nested_cond<_Ex>
141  __rethrow_if_nested_impl(const _Ex* __ptr)
142  {
143  if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
144  __ne_ptr->rethrow_nested();
145  }
146 
147  // Otherwise, no effects.
148  inline void
149  __rethrow_if_nested_impl(const void*)
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  { std::__rethrow_if_nested_impl(std::__addressof(__ex)); }
157 
158  // @} group exceptions
159 } // namespace std
160 
161 } // extern "C++"
162 
163 #endif // C++11
164 
165 #pragma GCC visibility pop
166 
167 #endif // _GLIBCXX_NESTED_EXCEPTION_H
integral_constant
Definition: type_traits:69
void terminate() noexcept __attribute__((__noreturn__))
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
Exception class with exception_ptr data member.
ISO C++ entities toplevel namespace is std.
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
An opaque pointer to an arbitrary exception.
Definition: exception_ptr.h:79
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