2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #ifndef __ardour_configuration_variable_h__
21 #define __ardour_configuration_variable_h__
26 #include "pbd/xml++.h"
27 #include "pbd/convert.h"
28 #include "ardour/types.h"
29 #include "ardour/utils.h"
33 class ConfigVariableBase {
36 ConfigVariableBase (std::string str) : _name (str) {}
37 virtual ~ConfigVariableBase() {}
39 std::string name () const { return _name; }
40 void add_to_node (XMLNode&);
41 bool set_from_node (XMLNode const &);
43 virtual std::string get_as_string () const = 0;
44 virtual void set_from_string (std::string const &) = 0;
54 class ConfigVariable : public ConfigVariableBase
58 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
59 ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
65 std::string get_as_string () const {
66 std::ostringstream ss;
71 virtual bool set (T val) {
81 virtual void set_from_string (std::string const & s) {
88 virtual T get_for_save() { return value; }
92 /** Specialisation of ConfigVariable for std::string to cope with whitespace properly */
94 class ConfigVariable<std::string> : public ConfigVariableBase
98 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
99 ConfigVariable (std::string str, std::string val) : ConfigVariableBase (str), value (val) {}
101 std::string get() const {
105 std::string get_as_string () const {
109 virtual bool set (std::string val) {
119 virtual void set_from_string (std::string const & s) {
124 virtual std::string get_for_save() { return value; }
129 class ConfigVariable<bool> : public ConfigVariableBase
133 ConfigVariable (std::string str) : ConfigVariableBase (str), value (false) {}
134 ConfigVariable (std::string str, bool val) : ConfigVariableBase (str), value (val) {}
140 std::string get_as_string () const {
141 std::ostringstream ss;
146 virtual bool set (bool val) {
156 void set_from_string (std::string const & s) {
157 value = PBD::string_is_affirmative (s);
161 virtual bool get_for_save() { return value; }
166 class ConfigVariableWithMutation : public ConfigVariable<T>
169 ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
170 : ConfigVariable<T> (name, val), mutator (m) {}
173 if (unmutated_value != val) {
174 unmutated_value = val;
175 return ConfigVariable<T>::set (mutator (val));
180 void set_from_string (std::string const & s) {
182 std::stringstream ss;
189 virtual T get_for_save() { return unmutated_value; }
195 class ConfigVariableWithMutation<std::string> : public ConfigVariable<std::string>
198 ConfigVariableWithMutation (std::string name, std::string val, std::string (*m)(std::string))
199 : ConfigVariable<std::string> (name, val), mutator (m) {}
201 bool set (std::string val) {
202 if (unmutated_value != val) {
203 unmutated_value = val;
204 return ConfigVariable<std::string>::set (mutator (val));
209 void set_from_string (std::string const & s) {
214 virtual std::string get_for_save() { return unmutated_value; }
215 std::string unmutated_value;
216 std::string (*mutator)(std::string);
221 #endif /* __ardour_configuration_variable_h__ */