1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_SetAny
21 #define TrenchBroom_SetAny
22 
23 #include <cassert>
24 #include <iostream>
25 
26 namespace TrenchBroom {
27     template <typename T>
28     class SetAny {
29     private:
30         T& m_value;
31         T m_oldValue;
32         T m_newValue;
33     public:
SetAny(T & value,T newValue)34         SetAny(T& value, T newValue) :
35         m_value(value),
36         m_oldValue(m_value) {
37             m_value = newValue;
38         }
39 
~SetAny()40         virtual ~SetAny() {
41             m_value = m_oldValue;
42         }
43     };
44 
45     template <typename T>
46     class SetLate {
47     private:
48         T& m_value;
49         T m_newValue;
50     public:
SetLate(T & value,T newValue)51         SetLate(T& value, T newValue) :
52         m_value(value) {}
53 
~SetLate()54         ~SetLate() {
55             m_value = m_newValue;
56         }
57     };
58 
59     class SetBool : public SetAny<bool> {
60     public:
61         SetBool(bool& value, bool newValue = true);
62     };
63 
64     template <typename R>
65     class SetBoolFun {
66     private:
67         typedef void (R::*F)(bool b);
68         R* m_receiver;
69         F m_function;
70         bool m_setTo;
71     public:
72         SetBoolFun(R* receiver, F function, bool setTo = true) :
m_receiver(receiver)73         m_receiver(receiver),
74         m_function(function),
75         m_setTo(setTo) {
76             assert(m_receiver != NULL);
77             (m_receiver->*m_function)(m_setTo);
78         }
79 
~SetBoolFun()80         ~SetBoolFun() {
81             (m_receiver->*m_function)(!m_setTo);
82         }
83     };
84 }
85 
86 #endif /* defined(TrenchBroom_SetAny) */
87