/src/WasmEdge/include/experimental/scope.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #include <optional> |
7 | | #include <type_traits> |
8 | | #include <utility> |
9 | | |
10 | | namespace cxx20 { |
11 | | |
12 | | template <class EF, typename = std::enable_if_t<std::is_invocable_v<EF>>> |
13 | | class scope_exit { |
14 | | scope_exit(const scope_exit &) = delete; |
15 | | scope_exit &operator=(const scope_exit &) = delete; |
16 | | scope_exit &operator=(scope_exit &&) = delete; |
17 | | |
18 | | public: |
19 | | template <class FN, |
20 | | typename = std::enable_if_t<!std::is_same_v< |
21 | | std::remove_cv_t<std::remove_reference_t<FN>>, scope_exit>>, |
22 | | typename = std::enable_if_t<std::is_constructible_v<EF, FN>>> |
23 | | constexpr explicit scope_exit(FN &&fn) noexcept( |
24 | | std::is_nothrow_constructible_v<EF, FN> || |
25 | | std::is_nothrow_constructible_v<EF, FN &>) |
26 | 0 | : ef(std::forward<FN>(fn)) {} Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<unsigned long>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, unsigned long, long)::{lambda()#1}, void>::scope_exit<{lambda()#1}, void, void>({lambda()#1}&&) Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<unsigned int>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, unsigned int, long)::{lambda()#1}, void>::scope_exit<{lambda()#1}, void, void>({lambda()#1}&&) Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<int>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, int, long)::{lambda()#1}, void>::scope_exit<{lambda()#1}, void, void>({lambda()#1}&&) Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<long>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, long, long)::{lambda()#1}, void>::scope_exit<{lambda()#1}, void, void>({lambda()#1}&&) |
27 | | template < |
28 | | typename = std::enable_if_t<std::is_nothrow_move_constructible_v<EF> || |
29 | | std::is_copy_constructible_v<EF>>> |
30 | | constexpr scope_exit(scope_exit &&other) noexcept( |
31 | | std::is_nothrow_move_constructible_v<EF> || |
32 | | std::is_nothrow_copy_constructible_v<EF>) |
33 | | : ef(std::move_if_noexcept(other.ef)) { |
34 | | other.release(); |
35 | | } |
36 | | |
37 | 0 | ~scope_exit() noexcept { |
38 | 0 | if (ef) { |
39 | 0 | (*ef)(); |
40 | 0 | ef.reset(); |
41 | 0 | } |
42 | 0 | } Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<unsigned long>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, unsigned long, long)::{lambda()#1}, void>::~scope_exit() Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<unsigned int>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, unsigned int, long)::{lambda()#1}, void>::~scope_exit() Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<int>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, int, long)::{lambda()#1}, void>::~scope_exit() Unexecuted instantiation: cxx20::scope_exit<WasmEdge::Executor::Executor::atomicWait<long>(WasmEdge::Runtime::Instance::MemoryInstance&, unsigned int, long, long)::{lambda()#1}, void>::~scope_exit() |
43 | | |
44 | | constexpr void release() noexcept { ef.reset(); } |
45 | | |
46 | | private: |
47 | | std::optional<EF> ef; |
48 | | }; |
49 | | |
50 | | template <class EF> scope_exit(EF) -> scope_exit<EF>; |
51 | | |
52 | | } // namespace cxx20 |