-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from 5cript/fix/scope_exit
Fixed ScopeExit class.
- Loading branch information
Showing
6 changed files
with
39 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,52 @@ | ||
#pragma once | ||
|
||
#include <boost/preprocessor/cat.hpp> | ||
|
||
#include <functional> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
#define ROAR_SE_UNIQUE_IDENTIFIER BOOST_PP_CAT(roar_se_unique_identifier_, __COUNTER__) | ||
|
||
namespace Roar | ||
{ | ||
template <typename EF> | ||
class ScopeExit | ||
{ | ||
public: | ||
ScopeExit(EF&& func) | ||
: onExit_(std::forward<EF>(func)) | ||
template <typename FunctionT> | ||
requires std::is_nothrow_invocable_v<FunctionT> | ||
explicit ScopeExit(FunctionT&& func) | ||
: onExit_(std::forward<FunctionT>(func)) | ||
{} | ||
~ScopeExit() | ||
{ | ||
onExit_(); | ||
if (onExit_) | ||
onExit_(); | ||
} | ||
ScopeExit(ScopeExit&& other) = delete; | ||
ScopeExit& operator=(ScopeExit&& other) = delete; | ||
ScopeExit(const ScopeExit&) = delete; | ||
ScopeExit& operator=(const ScopeExit&) = delete; | ||
void disarm() | ||
{ | ||
onExit_ = {}; | ||
} | ||
|
||
private: | ||
std::function<void()> onExit_; | ||
}; | ||
template <typename T> | ||
ScopeExit(T) -> ScopeExit<T>; | ||
|
||
namespace Detail | ||
{ | ||
struct MakeScopeExitImpl | ||
{ | ||
template <typename FunctionT> | ||
auto operator->*(FunctionT&& fn) const | ||
{ | ||
return ScopeExit{std::forward<FunctionT>(fn)}; | ||
} | ||
}; | ||
} | ||
|
||
#define ROAR_ON_SCOPE_EXIT auto ROAR_SE_UNIQUE_IDENTIFIER = ::Roar::Detail::MakeScopeExitImpl{}->*[&]() noexcept | ||
} |