Skip to content

Commit

Permalink
feat: remove stl::scope_exit
Browse files Browse the repository at this point in the history
  • Loading branch information
qudix committed Sep 22, 2024
1 parent ec59b6f commit 9a1db5a
Showing 1 changed file with 0 additions and 59 deletions.
59 changes: 0 additions & 59 deletions include/SFSE/Impl/PCH.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,65 +205,6 @@ namespace SFSE
string(const CharT (&)[N]) -> string<CharT, N - 1>;
}

template <class EF>
requires(std::invocable<std::remove_reference_t<EF>>)
class scope_exit
{
public:
// 1)
template <class Fn>
explicit scope_exit(Fn&& a_fn) //
noexcept(std::is_nothrow_constructible_v<EF, Fn> || std::is_nothrow_constructible_v<EF, Fn&>)
requires(!std::is_same_v<std::remove_cvref_t<Fn>, scope_exit> && std::is_constructible_v<EF, Fn>)
{
static_assert(std::invocable<Fn>);

if constexpr (!std::is_lvalue_reference_v<Fn> && std::is_nothrow_constructible_v<EF, Fn>) {
_fn.emplace(std::forward<Fn>(a_fn));
} else {
_fn.emplace(a_fn);
}
}

// 2)
scope_exit(scope_exit&& a_rhs) //
noexcept(std::is_nothrow_move_constructible_v<EF> || std::is_nothrow_copy_constructible_v<EF>)
requires(std::is_nothrow_move_constructible_v<EF> || std::is_copy_constructible_v<EF>)
{
static_assert(!(std::is_nothrow_move_constructible_v<EF> && !std::is_move_constructible_v<EF>));
static_assert(!(!std::is_nothrow_move_constructible_v<EF> && !std::is_copy_constructible_v<EF>));

if (a_rhs.active()) {
if constexpr (std::is_nothrow_move_constructible_v<EF>) {
_fn.emplace(std::forward<EF>(*a_rhs._fn));
} else {
_fn.emplace(a_rhs._fn);
}
a_rhs.release();
}
}

// 3)
scope_exit(const scope_exit&) = delete;

~scope_exit() noexcept
{
if (_fn.has_value()) {
(*_fn)();
}
}

void release() noexcept { _fn.reset(); }

private:
[[nodiscard]] bool active() const noexcept { return _fn.has_value(); }

std::optional<std::remove_reference_t<EF>> _fn;
};

template <class EF>
scope_exit(EF) -> scope_exit<EF>;

// backwards compat
template <
class E,
Expand Down

0 comments on commit 9a1db5a

Please sign in to comment.