1 /*****************************************************************************
2 
3 Copyright (c) 2019, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 
27 /** @file include/ut0bool_scope_guard.h
28  The ut_bool_scope_guard class which sets boolean to true for
29  the duration of scope. */
30 
31 #ifndef ut0bool_scope_guard_h
32 #define ut0bool_scope_guard_h
33 
34 namespace ut {
35 /** A RAII-style class, which sets a given boolean to true in constructor, and
36 to false in destructor, effectively making sure that it is true for the duration
37 of the object lifetime/scope. */
38 class bool_scope_guard_t {
39   /** boolean to be manipulated, or nullptr if the object was moved from, or
40   already destructed */
41   bool *m_active;
42 
43  public:
44   /** Creates the RAII guard which sets `active` to true for the duration of
45   its lifetime.
46   @param[in,out] active  the boolean which is to be manipulated */
bool_scope_guard_t(bool & active)47   explicit bool_scope_guard_t(bool &active) : m_active(&active) {
48     *m_active = true;
49   }
~bool_scope_guard_t()50   ~bool_scope_guard_t() {
51     if (m_active != nullptr) {
52       *m_active = false;
53       m_active = nullptr;
54     }
55   }
56   bool_scope_guard_t(bool_scope_guard_t const &) = delete;
57   bool_scope_guard_t &operator=(bool_scope_guard_t const &) = delete;
58   bool_scope_guard_t &operator=(bool_scope_guard_t &&) = delete;
bool_scope_guard_t(bool_scope_guard_t && old)59   bool_scope_guard_t(bool_scope_guard_t &&old) {
60     m_active = old.m_active;
61     old.m_active = nullptr;
62   }
63 };
64 }  // namespace ut
65 #endif /* ut0bool_scope_guard_h */
66