dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
ExecOnOOS.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// C++ includes
12#include <utility>
13#include <type_traits>
14
15namespace dfx::Utils
16{
57template<typename F>
59{
66 ExecOnOOS(bool exec, F && f) noexcept
67 : cb { std::move(f) }
68 , shouldExec { exec }
69 {}
70
75 ExecOnOOS(F && f) noexcept
76 : ExecOnOOS(true, std::move(f))
77 {}
78
86 ~ExecOnOOS() noexcept(std::is_nothrow_invocable_v<F>)
87 { if (shouldExec) cb(); }
88
92 void cancel() noexcept { shouldExec = false; }
94 bool willExec() const noexcept { return shouldExec; }
95
97 F cb;
100};
101} // !namespace dfx::Utils
Definition SystemConfigCommandHandler.hpp:15
STL namespace.
ExecOnOOS(F &&f) noexcept
Construct an enabled scope guard.
Definition ExecOnOOS.hpp:75
F cb
Stored callable executed on destruction if shouldExec is true.
Definition ExecOnOOS.hpp:97
~ExecOnOOS() noexcept(std::is_nothrow_invocable_v< F >)
Destructor. Executes the stored callable if not cancelled.
Definition ExecOnOOS.hpp:86
void cancel() noexcept
Prevent execution on scope exit.
Definition ExecOnOOS.hpp:92
bool shouldExec
Whether the destructor should execute cb.
Definition ExecOnOOS.hpp:99
bool willExec() const noexcept
Return whether the callable will currently execute on scope exit.
Definition ExecOnOOS.hpp:94
ExecOnOOS(bool exec, F &&f) noexcept
Construct a scope guard.
Definition ExecOnOOS.hpp:66