blob: ae78f891e70b9226608ae9cad8d4cc7ea268db23 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===-------------------------- exception ---------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:165//
Howard Hinnant412dbeb2010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_EXCEPTION
12#define _LIBCPP_EXCEPTION
13
14/*
15 exception synopsis
16
17namespace std
18{
19
20class exception
21{
22public:
Howard Hinnantfafca582011-05-26 18:23:5923 exception() noexcept;
24 exception(const exception&) noexcept;
25 exception& operator=(const exception&) noexcept;
26 virtual ~exception() noexcept;
27 virtual const char* what() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1628};
29
30class bad_exception
31 : public exception
32{
33public:
Howard Hinnantfafca582011-05-26 18:23:5934 bad_exception() noexcept;
35 bad_exception(const bad_exception&) noexcept;
36 bad_exception& operator=(const bad_exception&) noexcept;
37 virtual ~bad_exception() noexcept;
38 virtual const char* what() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1639};
40
41typedef void (*unexpected_handler)();
Howard Hinnantfafca582011-05-26 18:23:5942unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
43unexpected_handler get_unexpected() noexcept;
44[[noreturn]] void unexpected();
Howard Hinnant3e519522010-05-11 19:42:1645
46typedef void (*terminate_handler)();
Howard Hinnantfafca582011-05-26 18:23:5947terminate_handler set_terminate(terminate_handler f ) noexcept;
48terminate_handler get_terminate() noexcept;
49[[noreturn]] void terminate() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1650
Howard Hinnantfafca582011-05-26 18:23:5951bool uncaught_exception() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1652
53typedef unspecified exception_ptr;
54
Howard Hinnantfafca582011-05-26 18:23:5955exception_ptr current_exception() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1656void rethrow_exception [[noreturn]] (exception_ptr p);
Howard Hinnantfafca582011-05-26 18:23:5957template<class E> exception_ptr make_exception_ptr(E e) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1658
59class nested_exception
60{
61public:
Howard Hinnantfafca582011-05-26 18:23:5962 nested_exception() noexcept;
63 nested_exception(const nested_exception&) noexcept = default;
64 nested_exception& operator=(const nested_exception&) noexcept = default;
Howard Hinnant3e519522010-05-11 19:42:1665 virtual ~nested_exception() = default;
66
67 // access functions
Howard Hinnantfafca582011-05-26 18:23:5968 [[noreturn]] void rethrow_nested() const;
69 exception_ptr nested_ptr() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1670};
71
Howard Hinnantfafca582011-05-26 18:23:5972template <class T> [[noreturn]] void throw_with_nested(T&& t);
Howard Hinnant3e519522010-05-11 19:42:1673template <class E> void rethrow_if_nested(const E& e);
74
75} // std
76
77*/
78
79#include <__config>
80#include <cstddef>
Howard Hinnant019fe4b2010-05-27 17:06:5281#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:1682
Howard Hinnant073458b2011-10-17 20:05:1083#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:1684#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:1085#endif
Howard Hinnant3e519522010-05-11 19:42:1686
87namespace std // purposefully not using versioning namespace
88{
89
90class _LIBCPP_EXCEPTION_ABI exception
91{
92public:
Howard Hinnantfafca582011-05-26 18:23:5993 _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
94 virtual ~exception() _NOEXCEPT;
95 virtual const char* what() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:1696};
97
98class _LIBCPP_EXCEPTION_ABI bad_exception
99 : public exception
100{
101public:
Howard Hinnantfafca582011-05-26 18:23:59102 _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
103 virtual ~bad_exception() _NOEXCEPT;
104 virtual const char* what() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16105};
106
107typedef void (*unexpected_handler)();
Howard Hinnantfafca582011-05-26 18:23:59108_LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
109_LIBCPP_VISIBLE unexpected_handler get_unexpected() _NOEXCEPT;
Howard Hinnant400b24432011-05-26 17:07:32110_ATTRIBUTE(noreturn) _LIBCPP_VISIBLE void unexpected();
Howard Hinnant3e519522010-05-11 19:42:16111
112typedef void (*terminate_handler)();
Howard Hinnantfafca582011-05-26 18:23:59113_LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
114_LIBCPP_VISIBLE terminate_handler get_terminate() _NOEXCEPT;
Howard Hinnant400b24432011-05-26 17:07:32115_ATTRIBUTE(noreturn) _LIBCPP_VISIBLE void terminate() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16116
Howard Hinnantfafca582011-05-26 18:23:59117_LIBCPP_VISIBLE bool uncaught_exception() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16118
119class exception_ptr;
120
Howard Hinnantfafca582011-05-26 18:23:59121exception_ptr current_exception() _NOEXCEPT;
Howard Hinnant400b24432011-05-26 17:07:32122_ATTRIBUTE(noreturn) void rethrow_exception(exception_ptr);
Howard Hinnant3e519522010-05-11 19:42:16123
Howard Hinnantfb100022010-09-21 21:28:23124class _LIBCPP_VISIBLE exception_ptr
Howard Hinnant3e519522010-05-11 19:42:16125{
126 void* __ptr_;
127public:
Howard Hinnantfafca582011-05-26 18:23:59128 _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
129 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
130 exception_ptr(const exception_ptr&) _NOEXCEPT;
131 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
132 ~exception_ptr() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16133
Howard Hinnantfb100022010-09-21 21:28:23134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16135 // explicit
Howard Hinnantfafca582011-05-26 18:23:59136 operator bool() const _NOEXCEPT {return __ptr_ != nullptr;}
Howard Hinnant3e519522010-05-11 19:42:16137
Howard Hinnantfb100022010-09-21 21:28:23138 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnantfafca582011-05-26 18:23:59139 bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16140 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnantfb100022010-09-21 21:28:23141 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnantfafca582011-05-26 18:23:59142 bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16143 {return !(__x == __y);}
144
Howard Hinnantfafca582011-05-26 18:23:59145 friend exception_ptr current_exception() _NOEXCEPT;
Howard Hinnant400b24432011-05-26 17:07:32146 _ATTRIBUTE(noreturn) friend void rethrow_exception(exception_ptr);
Howard Hinnant3e519522010-05-11 19:42:16147};
148
Howard Hinnantc003db12011-11-29 18:15:50149template<class _Ep>
Howard Hinnant3e519522010-05-11 19:42:16150exception_ptr
Howard Hinnantc003db12011-11-29 18:15:50151make_exception_ptr(_Ep __e) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16152{
Howard Hinnant54b409f2010-08-11 17:04:31153#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16154 try
155 {
156 throw __e;
157 }
158 catch (...)
159 {
160 return current_exception();
161 }
Howard Hinnantb3371f62010-08-22 00:02:43162#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16163}
164
Howard Hinnant019fe4b2010-05-27 17:06:52165// nested_exception
166
167class _LIBCPP_EXCEPTION_ABI nested_exception
168{
169 exception_ptr __ptr_;
170public:
Howard Hinnantfafca582011-05-26 18:23:59171 nested_exception() _NOEXCEPT;
172// nested_exception(const nested_exception&) noexcept = default;
173// nested_exception& operator=(const nested_exception&) noexcept = default;
174 virtual ~nested_exception() _NOEXCEPT;
Howard Hinnant019fe4b2010-05-27 17:06:52175
176 // access functions
Howard Hinnant400b24432011-05-26 17:07:32177 _ATTRIBUTE(noreturn) void rethrow_nested() const;
Howard Hinnantfafca582011-05-26 18:23:59178 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
Howard Hinnant019fe4b2010-05-27 17:06:52179};
180
181template <class _Tp>
182struct __nested
183 : public _Tp,
184 public nested_exception
185{
Howard Hinnantfb100022010-09-21 21:28:23186 _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
Howard Hinnant019fe4b2010-05-27 17:06:52187};
188
189template <class _Tp>
Howard Hinnant400b24432011-05-26 17:07:32190_ATTRIBUTE(noreturn)
Howard Hinnantb3371f62010-08-22 00:02:43191void
Howard Hinnant7609c9b2010-09-04 23:28:19192#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant400b24432011-05-26 17:07:32193throw_with_nested(_Tp&& __t, typename enable_if<
Howard Hinnant019fe4b2010-05-27 17:06:52194 is_class<typename remove_reference<_Tp>::type>::value &&
195 !is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
196 >::type* = 0)
Howard Hinnant7609c9b2010-09-04 23:28:19197#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant019fe4b2010-05-27 17:06:52198throw_with_nested (_Tp& __t, typename enable_if<
199 is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
200 >::type* = 0)
Howard Hinnant7609c9b2010-09-04 23:28:19201#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant019fe4b2010-05-27 17:06:52202{
Howard Hinnant54b409f2010-08-11 17:04:31203#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantce48a112011-06-30 21:18:19204 throw __nested<typename remove_reference<_Tp>::type>(_VSTD::forward<_Tp>(__t));
Howard Hinnant54b409f2010-08-11 17:04:31205#endif
Howard Hinnant019fe4b2010-05-27 17:06:52206}
207
208template <class _Tp>
Howard Hinnant400b24432011-05-26 17:07:32209_ATTRIBUTE(noreturn)
Howard Hinnantb3371f62010-08-22 00:02:43210void
Howard Hinnant7609c9b2010-09-04 23:28:19211#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant400b24432011-05-26 17:07:32212throw_with_nested(_Tp&& __t, typename enable_if<
Howard Hinnant019fe4b2010-05-27 17:06:52213 !is_class<typename remove_reference<_Tp>::type>::value ||
214 is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
215 >::type* = 0)
Howard Hinnant7609c9b2010-09-04 23:28:19216#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant019fe4b2010-05-27 17:06:52217throw_with_nested (_Tp& __t, typename enable_if<
218 !is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
219 >::type* = 0)
Howard Hinnant7609c9b2010-09-04 23:28:19220#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant019fe4b2010-05-27 17:06:52221{
Howard Hinnant54b409f2010-08-11 17:04:31222#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantce48a112011-06-30 21:18:19223 throw _VSTD::forward<_Tp>(__t);
Howard Hinnant54b409f2010-08-11 17:04:31224#endif
Howard Hinnant019fe4b2010-05-27 17:06:52225}
226
Howard Hinnantc003db12011-11-29 18:15:50227template <class _Ep>
Howard Hinnantfb100022010-09-21 21:28:23228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant019fe4b2010-05-27 17:06:52229void
Howard Hinnantc003db12011-11-29 18:15:50230rethrow_if_nested(const _Ep& __e, typename enable_if<
231 is_polymorphic<_Ep>::value
Howard Hinnant019fe4b2010-05-27 17:06:52232 >::type* = 0)
233{
Howard Hinnanta391bc12010-05-28 13:35:41234 const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
235 if (__nep)
236 __nep->rethrow_nested();
Howard Hinnant019fe4b2010-05-27 17:06:52237}
238
Howard Hinnantc003db12011-11-29 18:15:50239template <class _Ep>
Howard Hinnantfb100022010-09-21 21:28:23240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant019fe4b2010-05-27 17:06:52241void
Howard Hinnantc003db12011-11-29 18:15:50242rethrow_if_nested(const _Ep& __e, typename enable_if<
243 !is_polymorphic<_Ep>::value
Howard Hinnant019fe4b2010-05-27 17:06:52244 >::type* = 0)
245{
246}
247
Howard Hinnant3e519522010-05-11 19:42:16248} // std
249
250#endif // _LIBCPP_EXCEPTION