Path: blob/main/contrib/llvm-project/libcxx/include/__exception/exception.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_H9#define _LIBCPP___EXCEPTION_EXCEPTION_H1011#include <__config>1213// <vcruntime_exception.h> defines its own std::exception and std::bad_exception types,14// which we use in order to be ABI-compatible with other STLs on Windows.15#if defined(_LIBCPP_ABI_VCRUNTIME)16# include <vcruntime_exception.h>17#endif1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223namespace std { // purposefully not using versioning namespace2425#if defined(_LIBCPP_ABI_VCRUNTIME) && (!defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS != 0)26// The std::exception class was already included above, but we're explicit about this condition here for clarity.2728#elif defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 029// However, <vcruntime_exception.h> does not define std::exception and std::bad_exception30// when _HAS_EXCEPTIONS == 0.31//32// Since libc++ still wants to provide the std::exception hierarchy even when _HAS_EXCEPTIONS == 033// (after all those are simply types like any other), we define an ABI-compatible version34// of the VCRuntime std::exception and std::bad_exception types in that mode.3536struct __std_exception_data {37char const* _What;38bool _DoFree;39};4041class exception { // base of all library exceptions42public:43exception() _NOEXCEPT : __data_() {}4445explicit exception(char const* __message) _NOEXCEPT : __data_() {46__data_._What = __message;47__data_._DoFree = true;48}4950exception(exception const&) _NOEXCEPT {}5152exception& operator=(exception const&) _NOEXCEPT { return *this; }5354virtual ~exception() _NOEXCEPT {}5556virtual char const* what() const _NOEXCEPT { return __data_._What ? __data_._What : "Unknown exception"; }5758private:59__std_exception_data __data_;60};6162class bad_exception : public exception {63public:64bad_exception() _NOEXCEPT : exception("bad exception") {}65};6667#else // !defined(_LIBCPP_ABI_VCRUNTIME)68// On all other platforms, we define our own std::exception and std::bad_exception types69// regardless of whether exceptions are turned on as a language feature.7071class _LIBCPP_EXPORTED_FROM_ABI exception {72public:73_LIBCPP_HIDE_FROM_ABI exception() _NOEXCEPT {}74_LIBCPP_HIDE_FROM_ABI exception(const exception&) _NOEXCEPT = default;75_LIBCPP_HIDE_FROM_ABI exception& operator=(const exception&) _NOEXCEPT = default;7677virtual ~exception() _NOEXCEPT;78virtual const char* what() const _NOEXCEPT;79};8081class _LIBCPP_EXPORTED_FROM_ABI bad_exception : public exception {82public:83_LIBCPP_HIDE_FROM_ABI bad_exception() _NOEXCEPT {}84_LIBCPP_HIDE_FROM_ABI bad_exception(const bad_exception&) _NOEXCEPT = default;85_LIBCPP_HIDE_FROM_ABI bad_exception& operator=(const bad_exception&) _NOEXCEPT = default;86~bad_exception() _NOEXCEPT override;87const char* what() const _NOEXCEPT override;88};89#endif // !_LIBCPP_ABI_VCRUNTIME9091} // namespace std9293#endif // _LIBCPP___EXCEPTION_EXCEPTION_H949596