TGUI 1.13
Loading...
Searching...
No Matches
ScopeExit.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_SCOPE_EXIT_HPP
26#define TGUI_SCOPE_EXIT_HPP
27
28#include <TGUI/Config.hpp>
29
30#include <type_traits>
31#include <utility>
32
33namespace tgui
34{
38 template <typename F>
39 class ScopeExit
40 {
41 public:
42 template <typename G, typename = typename std::enable_if<!std::is_same_v<typename std::decay<G>::type, ScopeExit>>::type>
43 explicit ScopeExit(G&& func) :
44 m_func(std::forward<G>(func)),
45 m_active(true)
46 {
47 }
48
49 ScopeExit(const ScopeExit&) = delete;
50
51 ScopeExit& operator=(const ScopeExit&) = delete;
52 ScopeExit& operator=(ScopeExit&&) = delete;
53
54 ScopeExit(ScopeExit&& other) noexcept(std::is_nothrow_move_constructible_v<F>) :
55 m_func(std::move(other.m_func)),
56 m_active(other.m_active)
57 {
58 other.m_active = false;
59 }
60
61 ~ScopeExit()
62 {
63 if (m_active)
64 m_func();
65 }
66
70 void release() noexcept
71 {
72 m_active = false;
73 }
74
75 private:
76 F m_func;
77 bool m_active;
78 };
79
83 template <typename F>
85 {
86 return ScopeExit<typename std::decay<F>::type>(std::forward<F>(func));
87 }
88} // namespace tgui
89
90#endif // TGUI_SCOPE_EXIT_HPP
Invokes a function when leaving scope (return or exception). Not included from TGUI....
Definition ScopeExit.hpp:40
void release() noexcept
Skip invoking the function when the guard is destroyed.
Definition ScopeExit.hpp:70
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
ScopeExit< typename std::decay< F >::type > makeScopeExit(F &&func)
Helper to create a ScopeExit without naming the lambda's type.
Definition ScopeExit.hpp:84