Skip to content

Commit

Permalink
Fixed scope exit class.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Nov 30, 2024
1 parent 7ac7d97 commit 0953e50
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions nui/include/nui/utility/scope_exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,25 @@

#include <functional>
#include <utility>
#include <concepts>

namespace Nui
{
template <typename EF>
class ScopeExit
{
public:
ScopeExit(EF&& func)
: onExit_(std::forward<EF>(func))
template <typename FunctionT>
requires std::invocable<FunctionT>
explicit ScopeExit(FunctionT&& func)
: onExit_(std::forward<FunctionT>(func))
{}
~ScopeExit()
{
if (onExit_)
onExit_();
}
ScopeExit(ScopeExit&& other)
: onExit_(std::move(other.onExit_))
{
other.onExit_ = {};
}
ScopeExit& operator=(ScopeExit&& other)
{
if (this != &other)
{
onExit_ = std::move(other.onExit_);
other.onExit_ = {};
}
return *this;
}
ScopeExit(ScopeExit&& other) = delete;
ScopeExit& operator=(ScopeExit&& other) = delete;
ScopeExit(const ScopeExit&) = delete;
ScopeExit& operator=(const ScopeExit&) = delete;
void disarm()
Expand All @@ -43,8 +33,6 @@ namespace Nui
private:
std::function<void()> onExit_;
};
template <typename T>
ScopeExit(T) -> ScopeExit<T>;

namespace Detail
{
Expand All @@ -53,7 +41,7 @@ namespace Nui
template <typename FunctionT>
auto operator->*(FunctionT&& fn) const
{
return ScopeExit<FunctionT>(std::forward<FunctionT>(fn));
return ScopeExit{std::forward<FunctionT>(fn)};
}
};
}
Expand Down

0 comments on commit 0953e50

Please sign in to comment.