From 7e07d14de7a69000d58db5b4ede1b89869e7c38d Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 1 Mar 2024 09:28:11 +0100 Subject: [PATCH] scoped_executor: add Signed-off-by: Simon Rozman --- UnitTests/pch.hpp | 1 + include/stdex/scoped_executor.hpp | 43 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 include/stdex/scoped_executor.hpp diff --git a/UnitTests/pch.hpp b/UnitTests/pch.hpp index 786441db6..83b331acb 100644 --- a/UnitTests/pch.hpp +++ b/UnitTests/pch.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include diff --git a/include/stdex/scoped_executor.hpp b/include/stdex/scoped_executor.hpp new file mode 100644 index 000000000..7819367a2 --- /dev/null +++ b/include/stdex/scoped_executor.hpp @@ -0,0 +1,43 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2023-2024 Amebis +*/ + +#pragma once + +#include "compat.hpp" + +namespace stdex +{ + /// + /// Executes one lambda immediately, and another when exiting the scope + /// + /// \typeparam F_init Lambda to execute immediately + /// \typeparam F_done Lambda to execute when exiting the scope + /// + template + class scoped_executor + { + F_done&& m_done; + + public: + /// + /// Executes init immediately and saves done for destructor + /// + /// \param[in] init Lambda to execute immediately + /// \param[in] done Lambda to execute in destructor + /// + scoped_executor(_In_ F_init&& init, _In_ F_done&& done) : m_done(std::forward(done)) + { + std::forward(init)(); + } + + /// + /// Executes done lambda + /// + ~scoped_executor() + { + std::forward(m_done)(); + } + }; +}