Path: blob/main/contrib/llvm-project/libcxx/include/__exception/exception_ptr.h
35234 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___EXCEPTION_EXCEPTION_PTR_H9#define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H1011#include <__config>12#include <__exception/operations.h>13#include <__memory/addressof.h>14#include <__memory/construct_at.h>15#include <__type_traits/decay.h>16#include <cstddef>17#include <cstdlib>18#include <new>19#include <typeinfo>2021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif2425#ifndef _LIBCPP_ABI_MICROSOFT2627# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION2829namespace __cxxabiv1 {3031extern "C" {32_LIBCPP_OVERRIDABLE_FUNC_VIS void* __cxa_allocate_exception(size_t) throw();33_LIBCPP_OVERRIDABLE_FUNC_VIS void __cxa_free_exception(void*) throw();3435struct __cxa_exception;36_LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(37void*,38std::type_info*,39# if defined(_WIN32)40void(__thiscall*)(void*)) throw();41# elif defined(__wasm__)42// In Wasm, a destructor returns its argument43void* (*)(void*)) throw();44# else45void (*)(void*)) throw();46# endif47}4849} // namespace __cxxabiv15051# endif5253#endif5455namespace std { // purposefully not using versioning namespace5657#ifndef _LIBCPP_ABI_MICROSOFT5859class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {60void* __ptr_;6162static exception_ptr __from_native_exception_pointer(void*) _NOEXCEPT;6364template <class _Ep>65friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;6667public:68// exception_ptr is basically a COW string.69using __trivially_relocatable = exception_ptr;7071_LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}72_LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}7374exception_ptr(const exception_ptr&) _NOEXCEPT;75exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;76~exception_ptr() _NOEXCEPT;7778_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; }7980friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {81return __x.__ptr_ == __y.__ptr_;82}8384friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {85return !(__x == __y);86}8788friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;89friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);90};9192template <class _Ep>93_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {94# ifndef _LIBCPP_HAS_NO_EXCEPTIONS95# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L96using _Ep2 = __decay_t<_Ep>;9798void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));99# ifdef __wasm__100// In Wasm, a destructor returns its argument101(void)__cxxabiv1::__cxa_init_primary_exception(102__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {103# else104(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {105# endif106std::__destroy_at(static_cast<_Ep2*>(__p));107# ifdef __wasm__108return __p;109# endif110});111112try {113::new (__ex) _Ep2(__e);114return exception_ptr::__from_native_exception_pointer(__ex);115} catch (...) {116__cxxabiv1::__cxa_free_exception(__ex);117return current_exception();118}119# else120try {121throw __e;122} catch (...) {123return current_exception();124}125# endif126# else127((void)__e);128std::abort();129# endif130}131132#else // _LIBCPP_ABI_MICROSOFT133134class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {135_LIBCPP_DIAGNOSTIC_PUSH136_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")137void* __ptr1_;138void* __ptr2_;139_LIBCPP_DIAGNOSTIC_POP140141public:142exception_ptr() _NOEXCEPT;143exception_ptr(nullptr_t) _NOEXCEPT;144exception_ptr(const exception_ptr& __other) _NOEXCEPT;145exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;146exception_ptr& operator=(nullptr_t) _NOEXCEPT;147~exception_ptr() _NOEXCEPT;148explicit operator bool() const _NOEXCEPT;149};150151_LIBCPP_EXPORTED_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;152153inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {154return !(__x == __y);155}156157_LIBCPP_EXPORTED_FROM_ABI void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;158159_LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr);160_LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;161_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);162163// This is a built-in template function which automagically extracts the required164// information.165template <class _E>166void* __GetExceptionInfo(_E);167168template <class _Ep>169_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {170return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e));171}172173#endif // _LIBCPP_ABI_MICROSOFT174} // namespace std175176#endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H177178179