xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/libsupc++/exception_ptr.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Exception Handling support header (exception_ptr class) for -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2008-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of GCC.
6*38fd1498Szrj //
7*38fd1498Szrj // GCC is free software; you can redistribute it and/or modify
8*38fd1498Szrj // it under the terms of the GNU General Public License as published by
9*38fd1498Szrj // the Free Software Foundation; either version 3, or (at your option)
10*38fd1498Szrj // any later version.
11*38fd1498Szrj //
12*38fd1498Szrj // GCC is distributed in the hope that it will be useful,
13*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*38fd1498Szrj // GNU General Public License for more details.
16*38fd1498Szrj //
17*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
18*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
19*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
20*38fd1498Szrj 
21*38fd1498Szrj // You should have received a copy of the GNU General Public License and
22*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
23*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24*38fd1498Szrj // <http://www.gnu.org/licenses/>.
25*38fd1498Szrj 
26*38fd1498Szrj /** @file bits/exception_ptr.h
27*38fd1498Szrj  *  This is an internal header file, included by other library headers.
28*38fd1498Szrj  *  Do not attempt to use it directly. @headername{exception}
29*38fd1498Szrj  */
30*38fd1498Szrj 
31*38fd1498Szrj #ifndef _EXCEPTION_PTR_H
32*38fd1498Szrj #define _EXCEPTION_PTR_H
33*38fd1498Szrj 
34*38fd1498Szrj #pragma GCC visibility push(default)
35*38fd1498Szrj 
36*38fd1498Szrj #include <bits/c++config.h>
37*38fd1498Szrj #include <bits/exception_defines.h>
38*38fd1498Szrj #include <bits/cxxabi_init_exception.h>
39*38fd1498Szrj #include <typeinfo>
40*38fd1498Szrj #include <new>
41*38fd1498Szrj 
42*38fd1498Szrj extern "C++" {
43*38fd1498Szrj 
44*38fd1498Szrj namespace std
45*38fd1498Szrj {
46*38fd1498Szrj   class type_info;
47*38fd1498Szrj 
48*38fd1498Szrj   /**
49*38fd1498Szrj    * @addtogroup exceptions
50*38fd1498Szrj    * @{
51*38fd1498Szrj    */
52*38fd1498Szrj   namespace __exception_ptr
53*38fd1498Szrj   {
54*38fd1498Szrj     class exception_ptr;
55*38fd1498Szrj   }
56*38fd1498Szrj 
57*38fd1498Szrj   using __exception_ptr::exception_ptr;
58*38fd1498Szrj 
59*38fd1498Szrj   /** Obtain an exception_ptr to the currently handled exception. If there
60*38fd1498Szrj    *  is none, or the currently handled exception is foreign, return the null
61*38fd1498Szrj    *  value.
62*38fd1498Szrj    */
63*38fd1498Szrj   exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT;
64*38fd1498Szrj 
65*38fd1498Szrj   template<typename _Ex>
66*38fd1498Szrj   exception_ptr make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT;
67*38fd1498Szrj 
68*38fd1498Szrj   /// Throw the object pointed to by the exception_ptr.
69*38fd1498Szrj   void rethrow_exception(exception_ptr) __attribute__ ((__noreturn__));
70*38fd1498Szrj 
71*38fd1498Szrj   namespace __exception_ptr
72*38fd1498Szrj   {
73*38fd1498Szrj     using std::rethrow_exception;
74*38fd1498Szrj 
75*38fd1498Szrj     /**
76*38fd1498Szrj      *  @brief An opaque pointer to an arbitrary exception.
77*38fd1498Szrj      *  @ingroup exceptions
78*38fd1498Szrj      */
79*38fd1498Szrj     class exception_ptr
80*38fd1498Szrj     {
81*38fd1498Szrj       void* _M_exception_object;
82*38fd1498Szrj 
83*38fd1498Szrj       explicit exception_ptr(void* __e) _GLIBCXX_USE_NOEXCEPT;
84*38fd1498Szrj 
85*38fd1498Szrj       void _M_addref() _GLIBCXX_USE_NOEXCEPT;
86*38fd1498Szrj       void _M_release() _GLIBCXX_USE_NOEXCEPT;
87*38fd1498Szrj 
88*38fd1498Szrj       void *_M_get() const _GLIBCXX_NOEXCEPT __attribute__ ((__pure__));
89*38fd1498Szrj 
90*38fd1498Szrj       friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT;
91*38fd1498Szrj       friend void std::rethrow_exception(exception_ptr);
92*38fd1498Szrj       template<typename _Ex>
93*38fd1498Szrj       friend exception_ptr std::make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT;
94*38fd1498Szrj 
95*38fd1498Szrj     public:
96*38fd1498Szrj       exception_ptr() _GLIBCXX_USE_NOEXCEPT;
97*38fd1498Szrj 
98*38fd1498Szrj       exception_ptr(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
99*38fd1498Szrj 
100*38fd1498Szrj #if __cplusplus >= 201103L
101*38fd1498Szrj       exception_ptr(nullptr_t) noexcept
102*38fd1498Szrj       : _M_exception_object(0)
103*38fd1498Szrj       { }
104*38fd1498Szrj 
105*38fd1498Szrj       exception_ptr(exception_ptr&& __o) noexcept
106*38fd1498Szrj       : _M_exception_object(__o._M_exception_object)
107*38fd1498Szrj       { __o._M_exception_object = 0; }
108*38fd1498Szrj #endif
109*38fd1498Szrj 
110*38fd1498Szrj #if (__cplusplus < 201103L) || defined (_GLIBCXX_EH_PTR_COMPAT)
111*38fd1498Szrj       typedef void (exception_ptr::*__safe_bool)();
112*38fd1498Szrj 
113*38fd1498Szrj       // For construction from nullptr or 0.
114*38fd1498Szrj       exception_ptr(__safe_bool) _GLIBCXX_USE_NOEXCEPT;
115*38fd1498Szrj #endif
116*38fd1498Szrj 
117*38fd1498Szrj       exception_ptr&
118*38fd1498Szrj       operator=(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
119*38fd1498Szrj 
120*38fd1498Szrj #if __cplusplus >= 201103L
121*38fd1498Szrj       exception_ptr&
122*38fd1498Szrj       operator=(exception_ptr&& __o) noexcept
123*38fd1498Szrj       {
124*38fd1498Szrj         exception_ptr(static_cast<exception_ptr&&>(__o)).swap(*this);
125*38fd1498Szrj         return *this;
126*38fd1498Szrj       }
127*38fd1498Szrj #endif
128*38fd1498Szrj 
129*38fd1498Szrj       ~exception_ptr() _GLIBCXX_USE_NOEXCEPT;
130*38fd1498Szrj 
131*38fd1498Szrj       void
132*38fd1498Szrj       swap(exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
133*38fd1498Szrj 
134*38fd1498Szrj #ifdef _GLIBCXX_EH_PTR_COMPAT
135*38fd1498Szrj       // Retained for compatibility with CXXABI_1.3.
136*38fd1498Szrj       void _M_safe_bool_dummy() _GLIBCXX_USE_NOEXCEPT
137*38fd1498Szrj 	__attribute__ ((__const__));
138*38fd1498Szrj       bool operator!() const _GLIBCXX_USE_NOEXCEPT
139*38fd1498Szrj 	__attribute__ ((__pure__));
140*38fd1498Szrj       operator __safe_bool() const _GLIBCXX_USE_NOEXCEPT;
141*38fd1498Szrj #endif
142*38fd1498Szrj 
143*38fd1498Szrj #if __cplusplus >= 201103L
144*38fd1498Szrj       explicit operator bool() const
145*38fd1498Szrj       { return _M_exception_object; }
146*38fd1498Szrj #endif
147*38fd1498Szrj 
148*38fd1498Szrj       friend bool
149*38fd1498Szrj       operator==(const exception_ptr&, const exception_ptr&)
150*38fd1498Szrj 	_GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
151*38fd1498Szrj 
152*38fd1498Szrj       const class std::type_info*
153*38fd1498Szrj       __cxa_exception_type() const _GLIBCXX_USE_NOEXCEPT
154*38fd1498Szrj 	__attribute__ ((__pure__));
155*38fd1498Szrj     };
156*38fd1498Szrj 
157*38fd1498Szrj     bool
158*38fd1498Szrj     operator==(const exception_ptr&, const exception_ptr&)
159*38fd1498Szrj       _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
160*38fd1498Szrj 
161*38fd1498Szrj     bool
162*38fd1498Szrj     operator!=(const exception_ptr&, const exception_ptr&)
163*38fd1498Szrj       _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
164*38fd1498Szrj 
165*38fd1498Szrj     inline void
166*38fd1498Szrj     swap(exception_ptr& __lhs, exception_ptr& __rhs)
167*38fd1498Szrj     { __lhs.swap(__rhs); }
168*38fd1498Szrj 
169*38fd1498Szrj     template<typename _Ex>
170*38fd1498Szrj       inline void
171*38fd1498Szrj       __dest_thunk(void* __x)
172*38fd1498Szrj       { static_cast<_Ex*>(__x)->~_Ex(); }
173*38fd1498Szrj 
174*38fd1498Szrj   } // namespace __exception_ptr
175*38fd1498Szrj 
176*38fd1498Szrj   /// Obtain an exception_ptr pointing to a copy of the supplied object.
177*38fd1498Szrj   template<typename _Ex>
178*38fd1498Szrj     exception_ptr
179*38fd1498Szrj     make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
180*38fd1498Szrj     {
181*38fd1498Szrj #if __cpp_exceptions
182*38fd1498Szrj       try
183*38fd1498Szrj 	{
184*38fd1498Szrj #if __cpp_rtti && !_GLIBCXX_HAVE_CDTOR_CALLABI
185*38fd1498Szrj           void *__e = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ex));
186*38fd1498Szrj           (void)__cxxabiv1::__cxa_init_primary_exception(
187*38fd1498Szrj 	      __e, const_cast<std::type_info*>(&typeid(__ex)),
188*38fd1498Szrj 	      __exception_ptr::__dest_thunk<_Ex>);
189*38fd1498Szrj           ::new (__e) _Ex(__ex);
190*38fd1498Szrj           return exception_ptr(__e);
191*38fd1498Szrj #else
192*38fd1498Szrj           throw __ex;
193*38fd1498Szrj #endif
194*38fd1498Szrj 	}
195*38fd1498Szrj       catch(...)
196*38fd1498Szrj 	{
197*38fd1498Szrj 	  return current_exception();
198*38fd1498Szrj 	}
199*38fd1498Szrj #else
200*38fd1498Szrj       return exception_ptr();
201*38fd1498Szrj #endif
202*38fd1498Szrj     }
203*38fd1498Szrj 
204*38fd1498Szrj   // @} group exceptions
205*38fd1498Szrj } // namespace std
206*38fd1498Szrj 
207*38fd1498Szrj } // extern "C++"
208*38fd1498Szrj 
209*38fd1498Szrj #pragma GCC visibility pop
210*38fd1498Szrj 
211*38fd1498Szrj #endif
212