WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
scope_exit.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7namespace slp {
8
9/// scope_exit is a general-purpose scope guard intended to call its exit
10/// function when a scope is exited.
11///
12/// @tparam F Function type.
13template <typename F>
15 public:
16 /// Constructs a scope_exit.
17 ///
18 /// @param f Function to call on scope exit.
19 explicit scope_exit(F&& f) noexcept : m_f{std::forward<F>(f)} {}
20
22 if (m_active) {
23 m_f();
24 }
25 }
26
27 /// Move constructor.
28 ///
29 /// @param rhs scope_exit from which to move.
30 scope_exit(scope_exit&& rhs) noexcept
31 : m_f{std::move(rhs.m_f)}, m_active{rhs.m_active} {
32 rhs.release();
33 }
34
35 scope_exit(const scope_exit&) = delete;
36 scope_exit& operator=(const scope_exit&) = delete;
37
38 /// Makes the scope_exit inactive.
39 void release() noexcept { m_active = false; }
40
41 private:
42 F m_f;
43 bool m_active = true;
44};
45
46} // namespace slp
void release() noexcept
Makes the scope_exit inactive.
Definition scope_exit.hpp:39
~scope_exit()
Definition scope_exit.hpp:21
scope_exit(const scope_exit &)=delete
scope_exit(F &&f) noexcept
Constructs a scope_exit.
Definition scope_exit.hpp:19
scope_exit & operator=(const scope_exit &)=delete
scope_exit(scope_exit &&rhs) noexcept
Move constructor.
Definition scope_exit.hpp:30
Definition expression_graph.hpp:11