forward port ConfigVariable<bool> fix from 2.X
[ardour.git] / libs / ardour / ardour / configuration_variable.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_configuration_variable_h__
21 #define __ardour_configuration_variable_h__
22
23 #include <iostream>
24 #include <sstream>
25
26 #include "pbd/xml++.h"
27 #include "ardour/types.h"
28 #include "ardour/utils.h"
29
30 namespace ARDOUR {
31
32 class ConfigVariableBase {
33   public:
34
35         ConfigVariableBase (std::string str) : _name (str) {}
36         virtual ~ConfigVariableBase() {}
37
38         std::string name () const { return _name; }
39         void add_to_node (XMLNode&);
40         bool set_from_node (XMLNode const &);
41
42         virtual std::string get_as_string () const = 0;
43         virtual void set_from_string (std::string const &) = 0;
44
45   protected:
46         std::string _name;
47
48         void notify ();
49         void miss ();
50 };
51
52 template<class T>
53 class ConfigVariable : public ConfigVariableBase
54 {
55   public:
56
57         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
58         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
59
60         T get() const {
61                 return value;
62         }
63
64         std::string get_as_string () const {
65                 std::ostringstream ss;
66                 ss << value;
67                 return ss.str ();
68         }
69
70         virtual bool set (T val) {
71                 if (val == value) {
72                         miss ();
73                         return false;
74                 }
75                 value = val;
76                 notify ();
77                 return true;
78         }
79
80         void set_from_string (std::string const & s) {
81                 std::stringstream ss;
82                 ss << s;
83                 ss >> value;
84         }
85
86   protected:
87         virtual T get_for_save() { return value; }
88         T value;
89 };
90
91 template<>
92 class ConfigVariable<bool> : public ConfigVariableBase
93 {
94   public:
95
96         ConfigVariable (std::string str) : ConfigVariableBase (str), value (false) {}
97         ConfigVariable (std::string str, bool val) : ConfigVariableBase (str), value (val) {}
98
99         bool get() const {
100                 return value;
101         }
102
103         std::string get_as_string () const {
104                 std::ostringstream ss;
105                 ss << value;
106                 return ss.str ();
107         }
108
109         virtual bool set (bool val) {
110                 if (val == value) {
111                         miss ();
112                         return false;
113                 }
114                 value = val;
115                 notify ();
116                 return true;
117         }
118
119         void set_from_string (std::string const & s) {
120                 value = string_is_affirmative (s);
121         }
122
123   protected:
124         virtual bool get_for_save() { return value; }
125         bool value;
126 };
127
128 template<class T>
129 class ConfigVariableWithMutation : public ConfigVariable<T>
130 {
131   public:
132         ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
133                 : ConfigVariable<T> (name, val), mutator (m) {}
134
135         bool set (T val) {
136                 if (unmutated_value != val) {
137                         unmutated_value = val;
138                         return ConfigVariable<T>::set (mutator (val));
139                 }
140                 return false;
141         }
142
143   protected:
144         virtual T get_for_save() { return unmutated_value; }
145         T unmutated_value;
146         T (*mutator)(T);
147 };
148
149 }
150
151 #endif /* __ardour_configuration_variable_h__ */