Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
scope_guard.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
5namespace pulp::runtime {
6
7// RAII scope guard — executes a callable on destruction
8template<typename F>
9class ScopeGuard {
10public:
11 explicit ScopeGuard(F&& fn) : fn_(std::move(fn)), active_(true) {}
12 ~ScopeGuard() { if (active_) fn_(); }
13
15 : fn_(std::move(other.fn_)), active_(other.active_) {
16 other.dismiss();
17 }
18
19 ScopeGuard(const ScopeGuard&) = delete;
20 ScopeGuard& operator=(const ScopeGuard&) = delete;
22
23 void dismiss() { active_ = false; }
24
25private:
26 F fn_;
27 bool active_;
28};
29
30template<typename F>
32 return ScopeGuard<F>(std::forward<F>(fn));
33}
34
35} // namespace pulp::runtime
36
37// Macro for anonymous scope guards
38#define PULP_CONCAT_IMPL(a, b) a##b
39#define PULP_CONCAT(a, b) PULP_CONCAT_IMPL(a, b)
40#define PULP_ON_SCOPE_EXIT(code) \
41 auto PULP_CONCAT(_scope_guard_, __LINE__) = \
42 ::pulp::runtime::make_scope_guard([&]() { code; })
Definition scope_guard.hpp:9
ScopeGuard & operator=(ScopeGuard &&)=delete
ScopeGuard(F &&fn)
Definition scope_guard.hpp:11
~ScopeGuard()
Definition scope_guard.hpp:12
ScopeGuard & operator=(const ScopeGuard &)=delete
ScopeGuard(const ScopeGuard &)=delete
ScopeGuard(ScopeGuard &&other) noexcept
Definition scope_guard.hpp:14
void dismiss()
Definition scope_guard.hpp:23
Definition generated_event_source.hpp:33
ScopeGuard< F > make_scope_guard(F &&fn)
Definition scope_guard.hpp:31
@ other
Definition udev_monitor.hpp:26
STL namespace.