promote Playlist::RegionList to ARDOUR::RegionList; fix timefx on multiple regions...
[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         virtual 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 /** Specialisation of ConfigVariable for std::string to cope with whitespace properly */
92 template<>
93 class ConfigVariable<std::string> : public ConfigVariableBase
94 {
95   public:
96
97         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
98         ConfigVariable (std::string str, std::string val) : ConfigVariableBase (str), value (val) {}
99
100         std::string get() const {
101                 return value;
102         }
103
104         std::string get_as_string () const {
105                 return value;
106         }
107
108         virtual bool set (std::string val) {
109                 if (val == value) {
110                         miss ();
111                         return false;
112                 }
113                 value = val;
114                 notify ();
115                 return true;
116         }
117
118         virtual void set_from_string (std::string const & s) {
119                 value = s;
120         }
121
122   protected:
123         virtual std::string get_for_save() { return value; }
124         std::string value;
125 };
126
127 template<>
128 class ConfigVariable<bool> : public ConfigVariableBase
129 {
130   public:
131
132         ConfigVariable (std::string str) : ConfigVariableBase (str), value (false) {}
133         ConfigVariable (std::string str, bool val) : ConfigVariableBase (str), value (val) {}
134
135         bool get() const {
136                 return value;
137         }
138
139         std::string get_as_string () const {
140                 std::ostringstream ss;
141                 ss << value;
142                 return ss.str ();
143         }
144
145         virtual bool set (bool val) {
146                 if (val == value) {
147                         miss ();
148                         return false;
149                 }
150                 value = val;
151                 notify ();
152                 return true;
153         }
154
155         void set_from_string (std::string const & s) {
156                 value = string_is_affirmative (s);
157         }
158
159   protected:
160         virtual bool get_for_save() { return value; }
161         bool value;
162 };
163
164 template<class T>
165 class ConfigVariableWithMutation : public ConfigVariable<T>
166 {
167   public:
168         ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
169                 : ConfigVariable<T> (name, val), mutator (m) {}
170
171         bool set (T val) {
172                 if (unmutated_value != val) {
173                         unmutated_value = val;
174                         return ConfigVariable<T>::set (mutator (val));
175                 }
176                 return false;
177         }
178
179         void set_from_string (std::string const & s) {
180                 T v;
181                 std::stringstream ss;
182                 ss << s;
183                 ss >> v;
184                 set (v);
185         }
186
187   protected:
188         virtual T get_for_save() { return unmutated_value; }
189         T unmutated_value;
190         T (*mutator)(T);
191 };
192
193 template<>
194 class ConfigVariableWithMutation<std::string> : public ConfigVariable<std::string>
195 {
196   public:
197         ConfigVariableWithMutation (std::string name, std::string val, std::string (*m)(std::string))
198                 : ConfigVariable<std::string> (name, val), mutator (m) {}
199
200         bool set (std::string val) {
201                 if (unmutated_value != val) {
202                         unmutated_value = val;
203                         return ConfigVariable<std::string>::set (mutator (val));
204                 }
205                 return false;
206         }
207
208         void set_from_string (std::string const & s) {
209                 set (s);
210         }
211
212   protected:
213         virtual std::string get_for_save() { return unmutated_value; }
214         std::string unmutated_value;
215         std::string (*mutator)(std::string);
216 };
217
218 }
219
220 #endif /* __ardour_configuration_variable_h__ */