MySQL 9.0.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
27template <typename TLambda>
29 public:
30 Scope_guard(const TLambda &rollback_lambda)
31 : m_committed(false), m_rollback_lambda(rollback_lambda) {}
34 : m_committed(moved.m_committed),
36 /* Set moved guard to "invalid" state, the one in which the rollback lambda
37 will not be executed. */
38 moved.m_committed = true;
39 }
41 if (!m_committed) {
43 }
44 }
45
46 inline void commit() { m_committed = true; }
47
48 inline void rollback() {
49 if (!m_committed) {
51 m_committed = true;
52 }
53 }
54
55 private:
57 const TLambda m_rollback_lambda;
58};
59
60template <typename TLambda>
61Scope_guard<TLambda> create_scope_guard(const TLambda rollback_lambda) {
62 return Scope_guard<TLambda>(rollback_lambda);
63}
64
65/**
66 Template class to scope guard variables.
67*/
68template <typename T>
70 public:
73
76
79
81
82 private:
85};
86#endif /* SCOPE_GUARD_H */
Definition: scope_guard.h:28
void rollback()
Definition: scope_guard.h:48
void commit()
Definition: scope_guard.h:46
Scope_guard(Scope_guard< TLambda > &&moved)
Definition: scope_guard.h:33
bool m_committed
Definition: scope_guard.h:56
const TLambda m_rollback_lambda
Definition: scope_guard.h:57
Scope_guard(const Scope_guard< TLambda > &)=delete
~Scope_guard()
Definition: scope_guard.h:40
Scope_guard(const TLambda &rollback_lambda)
Definition: scope_guard.h:30
Template class to scope guard variables.
Definition: scope_guard.h:69
T & m_var_ref
Definition: scope_guard.h:83
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:84
Variable_scope_guard(const Variable_scope_guard &)=delete
Variable_scope_guard(T &var)
Definition: scope_guard.h:72
Variable_scope_guard()=delete
~Variable_scope_guard()
Definition: scope_guard.h:80
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Definition: scope_guard.h:61