scoped_executor: add
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
e0e1663c58
commit
7e07d14de7
@ -20,6 +20,7 @@
|
|||||||
#include <stdex/pool.hpp>
|
#include <stdex/pool.hpp>
|
||||||
#include <stdex/progress.hpp>
|
#include <stdex/progress.hpp>
|
||||||
#include <stdex/ring.hpp>
|
#include <stdex/ring.hpp>
|
||||||
|
#include <stdex/scoped_executor.hpp>
|
||||||
#include <stdex/sgml.hpp>
|
#include <stdex/sgml.hpp>
|
||||||
#include <stdex/socket.hpp>
|
#include <stdex/socket.hpp>
|
||||||
#include <stdex/spinlock.hpp>
|
#include <stdex/spinlock.hpp>
|
||||||
|
43
include/stdex/scoped_executor.hpp
Normal file
43
include/stdex/scoped_executor.hpp
Normal file
@ -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 <typename F_init, typename F_done>
|
||||||
|
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<F_done&&>(done))
|
||||||
|
{
|
||||||
|
std::forward<F_init&&>(init)();
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Executes done lambda
|
||||||
|
///
|
||||||
|
~scoped_executor()
|
||||||
|
{
|
||||||
|
std::forward<F_done&&>(m_done)();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user