Sen API
Sen Libraries
Loading...
Searching...
No Matches
scope_guard.h
Go to the documentation of this file.
1// === scope_guard.h ===================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_SCOPE_GUARD_H
9#define SEN_CORE_BASE_SCOPE_GUARD_H
10
12
13namespace sen
14{
15
18
20template <class F>
21[[nodiscard]] auto makeScopeGuard(F&& f);
22
24template <typename F>
25class [[nodiscard]] ScopeGuard
26{
27 SEN_NOCOPY_NOMOVE(ScopeGuard)
28
29public: // special members
30 ScopeGuard() = delete;
31
32public:
34 ~ScopeGuard() noexcept { f_(); }
35
36private:
37 explicit ScopeGuard(F&& f): f_(std::move(f)) {}
38
39 template <class InputF>
40 friend auto makeScopeGuard(InputF&& f);
41
42private:
43 F f_;
44};
45
46template <class F>
48
49// Deleted overloads for making scope guards.
50// The function must be called on a rvalue reference
51
52template <class F>
53auto makeScopeGuard(const F&& f) = delete;
54
55template <class F>
56auto makeScopeGuard(F& f) = delete;
57
58template <class F>
59auto makeScopeGuard(const F& f) = delete;
60
62
63template <typename InputF>
64auto makeScopeGuard(InputF&& f)
65{
66 return ScopeGuard {std::forward<InputF>(f)};
67}
68
69} // namespace sen
70
71#endif // SEN_CORE_BASE_SCOPE_GUARD_H
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
Runs the function object F on destruction.
Definition scope_guard.h:26
ScopeGuard()=delete
~ScopeGuard() noexcept
Will trigger the given callback.
Definition scope_guard.h:34
auto makeScopeGuard(F &&f)
Makes scope guard from a callable taking no arguments.
ScopeGuard(F) -> ScopeGuard< F >
Definition assert.h:17
STL namespace.