diff --git a/pe_scope_guard/pe_scope_guard.hpp b/pe_scope_guard/pe_scope_guard.hpp new file mode 100644 index 0000000..7bf4dca --- /dev/null +++ b/pe_scope_guard/pe_scope_guard.hpp @@ -0,0 +1,31 @@ +#ifndef PE_SCOPE_GUARD_HPP +#define PE_SCOPE_GUARD_HPP + +#include +#include +#include +#include + +namespace peach +{ + + template< typename... Tys > class ScopeGuard + { + public: + constexpr ScopeGuard( Tys... callables ) requires( ( std::same_as< std::invoke_result_t< Tys >, void > && ... ) ) + : m_callables( std::make_tuple( callables... ) ) + { + } + + ~ScopeGuard( ) noexcept + { + std::apply( []( auto&&... args ) { ( ( args( ) ), ... ); }, m_callables ); + } + + private: + std::tuple< Tys... > m_callables; + }; + +} // namespace peach + +#endif // PE_SCOPE_GUARD_HPP