MySQL 9.1.0
Source Code Documentation
scope_guard.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SCOPE_GUARD_H
25#define SCOPE_GUARD_H
26
27/**
28 @brief A Lambda to be called at scope exit.
29
30 Used as std::scope_exit of sorts.
31 Useful if you can't use unique_ptr to install a
32 specific deleter but still want to do automatic
33 cleanup at scope exit.
34
35 @note Always use @ref create_scope_guard() instead of this template directly!
36
37 Typical use is:
38 @code
39 ...
40 foo_init();
41 auto cleanup_foo = create_scope_guard([&] {
42 foo_deinit();
43 }
44 ...
45 if (some_error)
46 return; // cleanup_foo calls foo_deinit()
47 ...
48 // foo_deinit is not going to be called past this point
49 cleanup_foo.release();
50 return; // return with foo initialized.
51 ...
52 @endcode
53*/
54template <typename TLambda>
56 public:
57 Scope_guard(const TLambda &cleanup_lambda)
58 : m_is_released(false), m_cleanup_lambda(cleanup_lambda) {}
63 /* Set moved guard to "invalid" state, the one in which the cleanup lambda
64 will not be executed. */
65 moved.m_is_released = true;
66 }
68 if (!m_is_released) {
70 }
71 }
72
73 /**
74 @brief Releases the scope guard
75
76 Makes sure that when scope guard goes out of scope the
77 cleanup lambda is not going to be called.
78 */
79 inline void release() { m_is_released = true; }
80
81 /**
82 @brief Calls the cleanup lambda and releases the scope guard
83
84 Useful if you want to explicitly provoke the cleanup earlier
85 than when going out of scope.
86 */
87 inline void reset() {
88 if (!m_is_released) {
90 m_is_released = true;
91 }
92 }
93
94 private:
95 /** If true the cleanup is not going to be called */
97 /** The cleanup to be called */
98 const TLambda m_cleanup_lambda;
99};
100
101/**
102 @brief Create a scope guard object
103
104 Always use this instead of the @ref Scope_guard template itself!
105 @sa @ref Scope_guard
106
107 @tparam TLambda The type of the lambda. Inferred, never specify it.
108 @param rollback_lambda The lambda to execute.
109 @return Scope_guard<TLambda> An specialization of the @ref Scope_guard
110 template.
111*/
112template <typename TLambda>
113Scope_guard<TLambda> create_scope_guard(const TLambda rollback_lambda) {
114 return Scope_guard<TLambda>(rollback_lambda);
115}
116
117/**
118 Template class to scope guard variables.
119*/
120template <typename T>
122 public:
125
128
131
133
134 private:
137};
138#endif /* SCOPE_GUARD_H */
A Lambda to be called at scope exit.
Definition: scope_guard.h:55
bool m_is_released
If true the cleanup is not going to be called.
Definition: scope_guard.h:96
Scope_guard(Scope_guard< TLambda > &&moved)
Definition: scope_guard.h:60
Scope_guard(const Scope_guard< TLambda > &)=delete
void release()
Releases the scope guard.
Definition: scope_guard.h:79
void reset()
Calls the cleanup lambda and releases the scope guard.
Definition: scope_guard.h:87
Scope_guard(const TLambda &cleanup_lambda)
Definition: scope_guard.h:57
~Scope_guard()
Definition: scope_guard.h:67
const TLambda m_cleanup_lambda
The cleanup to be called.
Definition: scope_guard.h:98
Template class to scope guard variables.
Definition: scope_guard.h:121
T & m_var_ref
Definition: scope_guard.h:135
Variable_scope_guard(Variable_scope_guard &&)=delete
Variable_scope_guard & operator=(const Variable_scope_guard &)=delete
Variable_scope_guard & operator=(Variable_scope_guard &&)=delete
T m_var_val
Definition: scope_guard.h:136
Variable_scope_guard(const Variable_scope_guard &)=delete
Variable_scope_guard(T &var)
Definition: scope_guard.h:124
Variable_scope_guard()=delete
~Variable_scope_guard()
Definition: scope_guard.h:132
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Create a scope guard object.
Definition: scope_guard.h:113