summaryrefslogtreecommitdiff
path: root/include/exception
blob: 772ada4eadc00217db18defc50695de55e2c506e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// -*- C++ -*-
//===-------------------------- exception ---------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_EXCEPTION
#define _LIBCPP_EXCEPTION

/*
    exception synopsis

namespace std
{

class exception
{
public:
    exception() throw();
    exception(const exception&) throw();
    exception& operator=(const exception&) throw();
    virtual ~exception() throw();
    virtual const char* what() const throw();
};

class bad_exception
    : public exception
{
public:
    bad_exception() throw();
    bad_exception(const bad_exception&) throw();
    bad_exception& operator=(const bad_exception&) throw();
    virtual ~bad_exception() throw();
    virtual const char* what() const throw();
};

typedef void (*unexpected_handler)();
unexpected_handler set_unexpected(unexpected_handler  f ) throw();
void unexpected [[noreturn]] ();

typedef void (*terminate_handler)();
terminate_handler set_terminate(terminate_handler  f ) throw();
void terminate [[noreturn]] ();

bool uncaught_exception() throw();

typedef unspecified exception_ptr;

exception_ptr current_exception();
void rethrow_exception [[noreturn]] (exception_ptr p);
template<class E> exception_ptr make_exception_ptr(E e);

class nested_exception
{
public:
    nested_exception() throw();
    nested_exception(const nested_exception&) throw() = default;
    nested_exception& operator=(const nested_exception&) throw() = default;
    virtual ~nested_exception() = default;

    // access functions
    void rethrow_nested [[noreturn]] () const;
    exception_ptr nested_ptr() const;
};

template <class T> void throw_with_nested [[noreturn]] (T&& t);
template <class E> void rethrow_if_nested(const E& e);

}  // std

*/

#include <__config>
#include <cstddef>
#include <type_traits>

#pragma GCC system_header

namespace std  // purposefully not using versioning namespace
{

class _LIBCPP_EXCEPTION_ABI exception
{
public:
    _LIBCPP_INLINE_VISIBILITY exception() throw() {}
    virtual ~exception() throw();
    virtual const char* what() const throw();
};

class _LIBCPP_EXCEPTION_ABI bad_exception
    : public exception
{
public:
    _LIBCPP_INLINE_VISIBILITY bad_exception() throw() {}
    virtual ~bad_exception() throw();
    virtual const char* what() const throw();
};

typedef void (*unexpected_handler)();
_LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) throw();
_LIBCPP_VISIBLE void unexpected();

typedef void (*terminate_handler)();
_LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) throw();
_LIBCPP_VISIBLE void terminate() __attribute__((__noreturn__));

_LIBCPP_VISIBLE bool uncaught_exception() throw();

class exception_ptr;

exception_ptr current_exception();
void rethrow_exception(exception_ptr);  // noreturn

class exception_ptr
{
    void* __ptr_;
public:
    exception_ptr()  : __ptr_() {}
    exception_ptr(nullptr_t) : __ptr_() {}
    exception_ptr(const exception_ptr&);
    exception_ptr& operator=(const exception_ptr&);
    ~exception_ptr();

    // explicit
        operator bool() const {return __ptr_ != nullptr;}

    friend bool operator==(const exception_ptr& __x, const exception_ptr& __y)
        {return __x.__ptr_ == __y.__ptr_;}
    friend bool operator!=(const exception_ptr& __x, const exception_ptr& __y)
        {return !(__x == __y);}

    friend exception_ptr current_exception();
    friend void rethrow_exception(exception_ptr);  // noreturn
};

template<class _E>
exception_ptr
make_exception_ptr(_E __e)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
        throw __e;
    }
    catch (...)
    {
        return current_exception();
    }
#endif
}

// nested_exception

class _LIBCPP_EXCEPTION_ABI nested_exception
{
    exception_ptr __ptr_;
public:
    nested_exception();
//     nested_exception(const nested_exception&) throw() = default;
//     nested_exception& operator=(const nested_exception&) throw() = default;
    virtual ~nested_exception();

    // access functions
    void rethrow_nested /*[[noreturn]]*/ () const;
    exception_ptr nested_ptr() const {return __ptr_;}
};

template <class _Tp>
struct __nested
    : public _Tp,
      public nested_exception
{
    explicit __nested(const _Tp& __t) : _Tp(__t) {}
};

template <class _Tp>
void 
#ifdef _LIBCPP_MOVE
throw_with_nested /*[[noreturn]]*/ (_Tp&& __t, typename enable_if<
                  is_class<typename remove_reference<_Tp>::type>::value &&
                  !is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
                                    >::type* = 0)
#else
throw_with_nested (_Tp& __t, typename enable_if<
                  is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
                                    >::type* = 0)
#endif
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    throw __nested<typename remove_reference<_Tp>::type>(_STD::forward<_Tp>(__t));
#endif
}

template <class _Tp>
void 
#ifdef _LIBCPP_MOVE
throw_with_nested /*[[noreturn]]*/ (_Tp&& __t, typename enable_if<
                  !is_class<typename remove_reference<_Tp>::type>::value ||
                  is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
                                    >::type* = 0)
#else
throw_with_nested (_Tp& __t, typename enable_if<
                  !is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
                                    >::type* = 0)
#endif
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    throw _STD::forward<_Tp>(__t);
#endif
}

template <class _E>
inline
void
rethrow_if_nested(const _E& __e, typename enable_if<
                                   is_polymorphic<_E>::value
                                                   >::type* = 0)
{
    const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
    if (__nep)
        __nep->rethrow_nested();
}

template <class _E>
inline
void
rethrow_if_nested(const _E& __e, typename enable_if<
                                   !is_polymorphic<_E>::value
                                                   >::type* = 0)
{
}

}  // std

#endif  // _LIBCPP_EXCEPTION