libsemigroups  v3.1.2
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
guard.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2025 James D. Mitchell
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19#ifndef LIBSEMIGROUPS_DETAIL_GUARD_HPP_
20#define LIBSEMIGROUPS_DETAIL_GUARD_HPP_
21
22namespace libsemigroups {
23 namespace detail {
24 // A simple class for ensuring that a value is set back to its value at the
25 // time of the creation of the Guard. Probably should only be used with POD
26 // types for Thing.
27 template <typename Thing>
28 class Guard {
29 Thing _old_value;
30 Thing& _value;
31
32 public:
33 Guard() = delete;
34 Guard(Guard const&) = delete;
35 Guard(Guard&&) = delete;
36 Guard& operator=(Guard const&) = delete;
37 Guard& operator=(Guard&&) = delete;
38
39 Guard(Thing& value, Thing new_value) : _old_value(value), _value(value) {
40 _value = new_value;
41 }
42
43 explicit Guard(Thing& value) : _old_value(value), _value(value) {}
44
45 ~Guard() {
46 _value = _old_value;
47 }
48 };
49
50 } // namespace detail
51} // namespace libsemigroups
52#endif // LIBSEMIGROUPS_DETAIL_GUARD_HPP_
Namespace for everything in the libsemigroups library.
Definition action.hpp:44