-
Notifications
You must be signed in to change notification settings - Fork 182
/
exception.hpp
91 lines (79 loc) · 2.52 KB
/
exception.hpp
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <exception>
#include <type_traits>
#include <utility>
#include <unifex/std_concepts.hpp>
#include <unifex/detail/prologue.hpp>
// Microsoft's STL has a fast implementation of std::make_exception_ptr.
// So does libc++ when targetting Microsoft's ABI. On other stdlibs,
// move as much exception machinery into a cpp file as possible to avoid
// template code bloat.
#if !defined(_LIBCPP_ABI_MICROSOFT) && !defined(_MSVC_STL_VERSION)
# define UNIFEX_HAS_FAST_MAKE_EXCEPTION_PTR 0
#else
# define UNIFEX_HAS_FAST_MAKE_EXCEPTION_PTR 1
#endif
namespace unifex {
namespace _throw {
struct _fn {
template <typename Exception>
[[noreturn]] UNIFEX_ALWAYS_INLINE void
operator()([[maybe_unused]] Exception&& ex) const {
#if !UNIFEX_NO_EXCEPTIONS
throw(Exception &&) ex;
#else
std::terminate();
#endif
}
};
} // namespace _throw
inline constexpr _throw::_fn throw_{};
namespace _except_ptr {
#if !UNIFEX_HAS_FAST_MAKE_EXCEPTION_PTR
// If std::make_exeption_ptr() is slow, then move it into a cpp
// file to generate less code elsewhere.
struct _ref {
template(typename Obj) //
(requires(!same_as<remove_cvref_t<Obj>, _ref>)) //
_ref(Obj&& obj) noexcept
: p_((void*)std::addressof(obj))
, throw_([](void* p) {
unifex::throw_((Obj &&) * (std::add_pointer_t<Obj>)p);
}) {}
_ref(_ref&&) = delete;
[[noreturn]] void rethrow() const;
private:
void* p_;
void (*throw_)(void*);
};
struct _fn {
[[nodiscard]] std::exception_ptr operator()(_ref) const noexcept;
};
#else // !UNIFEX_HAS_FAST_MAKE_EXCEPTION_PTR
struct _fn {
template <typename Obj>
[[nodiscard]] UNIFEX_ALWAYS_INLINE std::exception_ptr
operator()(Obj&& obj) const noexcept {
return std::make_exception_ptr((Obj &&) obj);
}
};
#endif
} // namespace _except_ptr
inline constexpr _except_ptr::_fn make_exception_ptr{};
} // namespace unifex
#include <unifex/detail/epilogue.hpp>