|
4 | 4 |
|
5 | 5 | namespace REX |
6 | 6 | { |
7 | | - template <class EF> |
8 | | - requires(std::invocable<std::remove_reference_t<EF>>) |
9 | | - class TScopeExit |
10 | | - { |
11 | | - public: |
12 | | - template <class Fn> |
13 | | - explicit TScopeExit(Fn&& a_fn) |
14 | | - noexcept(std::is_nothrow_constructible_v<EF, Fn> || |
15 | | - std::is_nothrow_constructible_v<EF, Fn&>) |
16 | | - requires(!std::is_same_v<std::remove_cvref_t<Fn>, TScopeExit> && |
17 | | - std::is_constructible_v<EF, Fn>) |
18 | | - { |
19 | | - static_assert(std::invocable<Fn>); |
| 7 | + template <class EF> |
| 8 | + requires(std::invocable<std::remove_reference_t<EF>>) |
| 9 | + class TScopeExit |
| 10 | + { |
| 11 | + public: |
| 12 | + template <class Fn> |
| 13 | + explicit TScopeExit(Fn&& a_fn) noexcept(std::is_nothrow_constructible_v<EF, Fn> || |
| 14 | + std::is_nothrow_constructible_v<EF, Fn&>) |
| 15 | + requires(!std::is_same_v<std::remove_cvref_t<Fn>, TScopeExit> && |
| 16 | + std::is_constructible_v<EF, Fn>) |
| 17 | + { |
| 18 | + static_assert(std::invocable<Fn>); |
20 | 19 |
|
21 | | - if constexpr (!std::is_lvalue_reference_v<Fn> && |
22 | | - std::is_nothrow_constructible_v<EF, Fn>) { |
23 | | - _fn.emplace(std::forward<Fn>(a_fn)); |
24 | | - } else { |
25 | | - _fn.emplace(a_fn); |
26 | | - } |
27 | | - } |
| 20 | + if constexpr (!std::is_lvalue_reference_v<Fn> && |
| 21 | + std::is_nothrow_constructible_v<EF, Fn>) { |
| 22 | + _fn.emplace(std::forward<Fn>(a_fn)); |
| 23 | + } else { |
| 24 | + _fn.emplace(a_fn); |
| 25 | + } |
| 26 | + } |
28 | 27 |
|
29 | | - TScopeExit(TScopeExit&& a_rhs) |
30 | | - noexcept(std::is_nothrow_move_constructible_v<EF> || |
31 | | - std::is_nothrow_copy_constructible_v<EF>) |
32 | | - requires(std::is_nothrow_move_constructible_v<EF> || |
33 | | - std::is_copy_constructible_v<EF>) |
34 | | - { |
35 | | - static_assert(!(std::is_nothrow_move_constructible_v<EF> && !std::is_move_constructible_v<EF>)); |
36 | | - static_assert(!(!std::is_nothrow_move_constructible_v<EF> && !std::is_copy_constructible_v<EF>)); |
| 28 | + TScopeExit(TScopeExit&& a_rhs) noexcept(std::is_nothrow_move_constructible_v<EF> || |
| 29 | + std::is_nothrow_copy_constructible_v<EF>) |
| 30 | + requires(std::is_nothrow_move_constructible_v<EF> || |
| 31 | + std::is_copy_constructible_v<EF>) |
| 32 | + { |
| 33 | + static_assert(!(std::is_nothrow_move_constructible_v<EF> && !std::is_move_constructible_v<EF>)); |
| 34 | + static_assert(!(!std::is_nothrow_move_constructible_v<EF> && !std::is_copy_constructible_v<EF>)); |
37 | 35 |
|
38 | | - if (a_rhs.active()) { |
39 | | - if constexpr (std::is_nothrow_move_constructible_v<EF>) { |
40 | | - _fn.emplace(std::forward<EF>(*a_rhs._fn)); |
41 | | - } else { |
42 | | - _fn.emplace(a_rhs._fn); |
43 | | - } |
44 | | - a_rhs.release(); |
45 | | - } |
46 | | - } |
| 36 | + if (a_rhs.active()) { |
| 37 | + if constexpr (std::is_nothrow_move_constructible_v<EF>) { |
| 38 | + _fn.emplace(std::forward<EF>(*a_rhs._fn)); |
| 39 | + } else { |
| 40 | + _fn.emplace(a_rhs._fn); |
| 41 | + } |
| 42 | + a_rhs.release(); |
| 43 | + } |
| 44 | + } |
47 | 45 |
|
48 | | - TScopeExit(const TScopeExit&) = delete; |
| 46 | + TScopeExit(const TScopeExit&) = delete; |
49 | 47 |
|
50 | | - ~TScopeExit() noexcept |
51 | | - { |
52 | | - if (_fn.has_value()) { |
53 | | - (*_fn)(); |
54 | | - } |
55 | | - } |
| 48 | + ~TScopeExit() noexcept |
| 49 | + { |
| 50 | + if (_fn.has_value()) { |
| 51 | + (*_fn)(); |
| 52 | + } |
| 53 | + } |
56 | 54 |
|
57 | | - void release() noexcept { _fn.reset(); } |
| 55 | + void release() noexcept { _fn.reset(); } |
58 | 56 |
|
59 | | - private: |
60 | | - [[nodiscard]] bool active() const noexcept { return _fn.has_value(); } |
| 57 | + private: |
| 58 | + [[nodiscard]] bool active() const noexcept { return _fn.has_value(); } |
61 | 59 |
|
62 | | - std::optional<std::remove_reference_t<EF>> _fn; |
63 | | - }; |
| 60 | + std::optional<std::remove_reference_t<EF>> _fn; |
| 61 | + }; |
64 | 62 |
|
65 | | - template <class EF> |
66 | | - TScopeExit(EF) -> TScopeExit<EF>; |
| 63 | + template <class EF> |
| 64 | + TScopeExit(EF) -> TScopeExit<EF>; |
67 | 65 | } |
0 commit comments