llvm-project/libcxx/include/exception

251 lines
7.1 KiB
Plaintext
Raw Normal View History

2010-05-12 03:42:16 +08:00
// -*- C++ -*-
//===-------------------------- exception ---------------------------------===//
//
// The LLVM Compiler Infrastructure
2010-05-12 03:42:16 +08:00
//
2010-11-17 06:09:02 +08:00
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
2010-05-12 03:42:16 +08:00
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_EXCEPTION
#define _LIBCPP_EXCEPTION
/*
exception synopsis
namespace std
{
class exception
{
public:
exception() noexcept;
exception(const exception&) noexcept;
exception& operator=(const exception&) noexcept;
virtual ~exception() noexcept;
virtual const char* what() const noexcept;
2010-05-12 03:42:16 +08:00
};
class bad_exception
: public exception
{
public:
bad_exception() noexcept;
bad_exception(const bad_exception&) noexcept;
bad_exception& operator=(const bad_exception&) noexcept;
virtual ~bad_exception() noexcept;
virtual const char* what() const noexcept;
2010-05-12 03:42:16 +08:00
};
typedef void (*unexpected_handler)();
unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
unexpected_handler get_unexpected() noexcept;
[[noreturn]] void unexpected();
2010-05-12 03:42:16 +08:00
typedef void (*terminate_handler)();
terminate_handler set_terminate(terminate_handler f ) noexcept;
terminate_handler get_terminate() noexcept;
[[noreturn]] void terminate() noexcept;
2010-05-12 03:42:16 +08:00
bool uncaught_exception() noexcept;
2010-05-12 03:42:16 +08:00
typedef unspecified exception_ptr;
exception_ptr current_exception() noexcept;
2010-05-12 03:42:16 +08:00
void rethrow_exception [[noreturn]] (exception_ptr p);
template<class E> exception_ptr make_exception_ptr(E e) noexcept;
2010-05-12 03:42:16 +08:00
class nested_exception
{
public:
nested_exception() noexcept;
nested_exception(const nested_exception&) noexcept = default;
nested_exception& operator=(const nested_exception&) noexcept = default;
2010-05-12 03:42:16 +08:00
virtual ~nested_exception() = default;
// access functions
[[noreturn]] void rethrow_nested() const;
exception_ptr nested_ptr() const noexcept;
2010-05-12 03:42:16 +08:00
};
template <class T> [[noreturn]] void throw_with_nested(T&& t);
2010-05-12 03:42:16 +08:00
template <class E> void rethrow_if_nested(const E& e);
} // std
*/
#include <__config>
#include <cstddef>
2010-05-28 01:06:52 +08:00
#include <type_traits>
2010-05-12 03:42:16 +08:00
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2010-05-12 03:42:16 +08:00
#pragma GCC system_header
#endif
2010-05-12 03:42:16 +08:00
namespace std // purposefully not using versioning namespace
{
class _LIBCPP_EXCEPTION_ABI exception
{
public:
_LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
virtual ~exception() _NOEXCEPT;
virtual const char* what() const _NOEXCEPT;
2010-05-12 03:42:16 +08:00
};
class _LIBCPP_EXCEPTION_ABI bad_exception
: public exception
{
public:
_LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
virtual ~bad_exception() _NOEXCEPT;
virtual const char* what() const _NOEXCEPT;
2010-05-12 03:42:16 +08:00
};
typedef void (*unexpected_handler)();
_LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
_LIBCPP_VISIBLE unexpected_handler get_unexpected() _NOEXCEPT;
_LIBCPP_NORETURN _LIBCPP_VISIBLE void unexpected();
2010-05-12 03:42:16 +08:00
typedef void (*terminate_handler)();
_LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
_LIBCPP_VISIBLE terminate_handler get_terminate() _NOEXCEPT;
_LIBCPP_NORETURN _LIBCPP_VISIBLE void terminate() _NOEXCEPT;
2010-05-12 03:42:16 +08:00
_LIBCPP_VISIBLE bool uncaught_exception() _NOEXCEPT;
2010-05-12 03:42:16 +08:00
class exception_ptr;
exception_ptr current_exception() _NOEXCEPT;
_LIBCPP_NORETURN void rethrow_exception(exception_ptr);
2010-05-12 03:42:16 +08:00
class _LIBCPP_VISIBLE exception_ptr
2010-05-12 03:42:16 +08:00
{
void* __ptr_;
public:
_LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
_LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
exception_ptr(const exception_ptr&) _NOEXCEPT;
exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
~exception_ptr() _NOEXCEPT;
2010-05-12 03:42:16 +08:00
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_EXPLICIT
operator bool() const _NOEXCEPT {return __ptr_ != nullptr;}
2010-05-12 03:42:16 +08:00
friend _LIBCPP_INLINE_VISIBILITY
bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.__ptr_ == __y.__ptr_;}
friend _LIBCPP_INLINE_VISIBILITY
bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return !(__x == __y);}
friend exception_ptr current_exception() _NOEXCEPT;
_LIBCPP_NORETURN friend void rethrow_exception(exception_ptr);
2010-05-12 03:42:16 +08:00
};
template<class _Ep>
2010-05-12 03:42:16 +08:00
exception_ptr
make_exception_ptr(_Ep __e) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
#ifndef _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
try
{
throw __e;
}
catch (...)
{
return current_exception();
}
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
}
2010-05-28 01:06:52 +08:00
// nested_exception
class _LIBCPP_EXCEPTION_ABI nested_exception
{
exception_ptr __ptr_;
public:
nested_exception() _NOEXCEPT;
// nested_exception(const nested_exception&) noexcept = default;
// nested_exception& operator=(const nested_exception&) noexcept = default;
virtual ~nested_exception() _NOEXCEPT;
2010-05-28 01:06:52 +08:00
// access functions
_LIBCPP_NORETURN void rethrow_nested() const;
_LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
2010-05-28 01:06:52 +08:00
};
template <class _Tp>
struct __nested
: public _Tp,
public nested_exception
{
_LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
2010-05-28 01:06:52 +08:00
};
template <class _Tp>
_LIBCPP_NORETURN
void
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
throw_with_nested(_Tp&& __t, typename enable_if<
2010-05-28 01:06:52 +08:00
is_class<typename remove_reference<_Tp>::type>::value &&
!is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
>::type* = 0)
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2010-05-28 01:06:52 +08:00
throw_with_nested (_Tp& __t, typename enable_if<
is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
>::type* = 0)
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2010-05-28 01:06:52 +08:00
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw __nested<typename remove_reference<_Tp>::type>(_VSTD::forward<_Tp>(__t));
#endif
2010-05-28 01:06:52 +08:00
}
template <class _Tp>
_LIBCPP_NORETURN
void
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
throw_with_nested(_Tp&& __t, typename enable_if<
2010-05-28 01:06:52 +08:00
!is_class<typename remove_reference<_Tp>::type>::value ||
is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
>::type* = 0)
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2010-05-28 01:06:52 +08:00
throw_with_nested (_Tp& __t, typename enable_if<
!is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
>::type* = 0)
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2010-05-28 01:06:52 +08:00
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw _VSTD::forward<_Tp>(__t);
#endif
2010-05-28 01:06:52 +08:00
}
template <class _Ep>
inline _LIBCPP_INLINE_VISIBILITY
2010-05-28 01:06:52 +08:00
void
rethrow_if_nested(const _Ep& __e, typename enable_if<
is_polymorphic<_Ep>::value
2010-05-28 01:06:52 +08:00
>::type* = 0)
{
const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
if (__nep)
__nep->rethrow_nested();
2010-05-28 01:06:52 +08:00
}
template <class _Ep>
inline _LIBCPP_INLINE_VISIBILITY
2010-05-28 01:06:52 +08:00
void
rethrow_if_nested(const _Ep&, typename enable_if<
!is_polymorphic<_Ep>::value
2010-05-28 01:06:52 +08:00
>::type* = 0)
{
}
2010-05-12 03:42:16 +08:00
} // std
#endif // _LIBCPP_EXCEPTION