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