MySQL 8.0.30
Source Code Documentation
scope_guard.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2022, 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 also distributed 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 included with MySQL.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License, version 2.0, for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef SCOPE_GUARD_H
24#define SCOPE_GUARD_H
25
26template <typename TLambda>
28 public:
29 Scope_guard(const TLambda &rollback_lambda)
30 : m_committed(false), m_rollback_lambda(rollback_lambda) {}
33 : m_committed(moved.m_committed),
35 /* Set moved guard to "invalid" state, the one in which the rollback lambda
36 will not be executed. */
37 moved.m_committed = true;
38 }
40 if (!m_committed) {
42 }
43 }
44
45 inline void commit() { m_committed = true; }
46
47 inline void rollback() {
48 if (!m_committed) {
50 m_committed = true;
51 }
52 }
53
54 private:
56 const TLambda m_rollback_lambda;
57};
58
59template <typename TLambda>
60Scope_guard<TLambda> create_scope_guard(const TLambda rollback_lambda) {
61 return Scope_guard<TLambda>(rollback_lambda);
62}
63
64/**
65 Template class to scope guard variables.
66*/
67template <typename T>
69 public:
72
75
78
80
81 private:
84};
85#endif /* SCOPE_GUARD_H */
Definition: scope_guard.h:27
void rollback()
Definition: scope_guard.h:47
void commit()
Definition: scope_guard.h:45
Scope_guard(Scope_guard< TLambda > &&moved)
Definition: scope_guard.h:32
bool m_committed
Definition: scope_guard.h:55
const TLambda m_rollback_lambda
Definition: scope_guard.h:56
Scope_guard(const Scope_guard< TLambda > &)=delete
~Scope_guard()
Definition: scope_guard.h:39
Scope_guard(const TLambda &rollback_lambda)
Definition: scope_guard.h:29
Template class to scope guard variables.
Definition: scope_guard.h:68
T & m_var_ref
Definition: scope_guard.h:82
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:83
Variable_scope_guard(const Variable_scope_guard &)=delete
Variable_scope_guard(T &var)
Definition: scope_guard.h:71
Variable_scope_guard()=delete
~Variable_scope_guard()
Definition: scope_guard.h:79
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Definition: scope_guard.h:60