MySQL  8.0.18
Source Code Documentation
scope_guard.h
Go to the documentation of this file.
1 /* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6 
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef SCOPE_GUARD_H
24 #define SCOPE_GUARD_H
25 
26 template <typename TLambda>
27 class Scope_guard {
28  public:
29  Scope_guard(const TLambda &rollback_lambda)
30  : m_committed(false), m_rollback_lambda(rollback_lambda) {}
31  Scope_guard(const Scope_guard<TLambda> &) = delete;
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 
59 template <typename TLambda>
60 Scope_guard<TLambda> create_scope_guard(const TLambda rollback_lambda) {
61  return Scope_guard<TLambda>(rollback_lambda);
62 }
63 
64 #endif /* SCOPE_GUARD_H */
void commit()
Definition: scope_guard.h:45
Definition: scope_guard.h:27
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Definition: scope_guard.h:60
const TLambda m_rollback_lambda
Definition: scope_guard.h:56
Scope_guard(const TLambda &rollback_lambda)
Definition: scope_guard.h:29
void rollback()
Definition: scope_guard.h:47
~Scope_guard()
Definition: scope_guard.h:39
bool m_committed
Definition: scope_guard.h:55
Scope_guard(Scope_guard< TLambda > &&moved)
Definition: scope_guard.h:32
#define false
Definition: config_static.h:43