Created
February 17, 2022 21:47
-
-
Save hare1039/b61726db096c0d19babdd9be26a3519b to your computer and use it in GitHub Desktop.
scope exit
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
#pragma once | |
#ifndef SCOPE_EXIT_HPP__ | |
#define SCOPE_EXIT_HPP__ | |
#include <functional> | |
#define BASIC_SCOPE_EXIT_CONCAT_IMPL(x, y) x##y | |
#define BASIC_SCOPE_EXIT_CONCAT(x, y) BASIC_SCOPE_EXIT_CONCAT_IMPL(x, y) | |
#define SCOPE_DEFER ::basic_scope_exit::scope_exit BASIC_SCOPE_EXIT_CONCAT(SCOPE_EXIT_UNIQUE_VAR_, __LINE__) = \ | |
::basic_scope_exit::make_scope_exit | |
// usage: | |
// int * p = new int; | |
// SCOPE_DEFER([&p]{ delete p; }); | |
namespace basic_scope_exit | |
{ | |
template <typename Callable> | |
class scope_exit | |
{ | |
Callable c_; | |
public: | |
scope_exit(Callable && func): c_{std::forward<Callable>(func)} {} | |
~scope_exit() { std::invoke(c_); } | |
}; | |
template <typename Callable> | |
auto make_scope_exit(Callable && f) -> scope_exit<Callable> | |
{ | |
return scope_exit<Callable>(std::forward<Callable>(f)); | |
} | |
} // namespace basic_scope_exit | |
#endif // SCOPE_EXIT_HPP__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment