mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
scope_cleanup.hpp
1#ifndef MRS_LIB_UTILITY_SCOPE_CLEANUP_HPP_
2#define MRS_LIB_UTILITY_SCOPE_CLEANUP_HPP_
3
4
5#include <functional>
6#include <utility>
7
8
9namespace mrs_lib
10{
11
15 template <typename T>
17 {
18 public:
24 explicit ScopeCleanup(T f) : cleanup_func_(std::move(f))
25 {
26 }
27
29 {
30 if (enabled_)
31 {
32 std::invoke(cleanup_func_);
33 }
34 }
35
36 ScopeCleanup(const ScopeCleanup&) = delete;
37 ScopeCleanup(ScopeCleanup&&) = delete;
38 ScopeCleanup& operator=(const ScopeCleanup&) = delete;
39 ScopeCleanup& operator=(ScopeCleanup&&) = delete;
40
44 void cancel()
45 {
46 enabled_ = false;
47 }
48
49 private:
50 T cleanup_func_;
51 bool enabled_ = true;
52 };
53
54} // namespace mrs_lib
55
56
57#endif // MRS_LIB_UTILITY_SCOPE_CLEANUP_HPP_
Utility class for running cleanup at the end of a scope.
Definition scope_cleanup.hpp:17
ScopeCleanup(T f)
Create a scope cleanup that runs the specified callable when destroyed.
Definition scope_cleanup.hpp:24
void cancel()
Disable running of the callback, when the cleanup object is destroyed.
Definition scope_cleanup.hpp:44
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24