-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joseph Schuchart <[email protected]>
- Loading branch information
Showing
6 changed files
with
77 additions
and
61 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef TTG_UTIL_SCOPE_EXIT_H | ||
#define TTG_UTIL_SCOPE_EXIT_H | ||
|
||
// | ||
// N4189: Scoped Resource - Generic RAII Wrapper for the Standard Library | ||
// Peter Sommerlad and Andrew L. Sandoval | ||
// Adopted from https://github.com/tandasat/ScopedResource/tree/master | ||
// | ||
|
||
#include <type_traits> | ||
|
||
namespace ttg::detail { | ||
template <typename EF> | ||
struct scope_exit | ||
{ | ||
// construction | ||
explicit | ||
scope_exit(EF &&f) | ||
: exit_function(std::move(f)) | ||
, execute_on_destruction{ true } | ||
{ } | ||
|
||
// move | ||
scope_exit(scope_exit &&rhs) | ||
: exit_function(std::move(rhs.exit_function)) | ||
, execute_on_destruction{ rhs.execute_on_destruction } | ||
{ | ||
rhs.release(); | ||
} | ||
|
||
// release | ||
~scope_exit() | ||
{ | ||
if (execute_on_destruction) this->exit_function(); | ||
} | ||
|
||
void release() | ||
{ | ||
this->execute_on_destruction = false; | ||
} | ||
|
||
private: | ||
scope_exit(scope_exit const &) = delete; | ||
void operator=(scope_exit const &) = delete; | ||
scope_exit& operator=(scope_exit &&) = delete; | ||
EF exit_function; | ||
bool execute_on_destruction; // exposition only | ||
}; | ||
|
||
template <typename EF> | ||
auto make_scope_exit(EF &&exit_function) | ||
{ | ||
return scope_exit<std::remove_reference_t<EF>>(std::forward<EF>(exit_function)); | ||
} | ||
|
||
} // namespace ttg::detail | ||
|
||
#endif // TTG_UTIL_SCOPE_EXIT_H |