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/libardour_visibility.h"
29 #include "ardour/types.h"
30 #include "ardour/utils.h"
34 class LIBARDOUR_API ConfigVariableBase {
37 ConfigVariableBase (std::string str) : _name (str) {}
38 virtual ~ConfigVariableBase() {}
40 std::string name () const { return _name; }
41 void add_to_node (XMLNode&);
42 bool set_from_node (XMLNode const &);
44 virtual std::string get_as_string () const = 0;
45 virtual void set_from_string (std::string const &) = 0;
55 class LIBARDOUR_API ConfigVariable : public ConfigVariableBase
59 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
60 ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
66 std::string get_as_string () const {
67 std::ostringstream ss;
72 virtual bool set (T val) {
82 virtual void set_from_string (std::string const & s) {
89 virtual T get_for_save() { return value; }
93 /** Specialisation of ConfigVariable for std::string to cope with whitespace properly */
95 class LIBARDOUR_API ConfigVariable<std::string> : public ConfigVariableBase
99 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
100 ConfigVariable (std::string str, std::string val) : ConfigVariableBase (str), value (val) {}
102 std::string get() const {
106 std::string get_as_string () const {
110 virtual bool set (std::string val) {
120 virtual void set_from_string (std::string const & s) {
125 virtual std::string get_for_save() { return value; }
130 class LIBARDOUR_API ConfigVariable<bool> : public ConfigVariableBase
134 ConfigVariable (std::string str) : ConfigVariableBase (str), value (false) {}
135 ConfigVariable (std::string str, bool val) : ConfigVariableBase (str), value (val) {}
141 std::string get_as_string () const {
142 std::ostringstream ss;
147 virtual bool set (bool val) {
157 void set_from_string (std::string const & s) {
158 value = PBD::string_is_affirmative (s);
162 virtual bool get_for_save() { return value; }
167 class LIBARDOUR_API ConfigVariableWithMutation : public ConfigVariable<T>
170 ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
171 : ConfigVariable<T> (name, val), mutator (m) {}
174 if (unmutated_value != val) {
175 unmutated_value = val;
176 return ConfigVariable<T>::set (mutator (val));
181 void set_from_string (std::string const & s) {
183 std::stringstream ss;
190 virtual T get_for_save() { return unmutated_value; }
196 class LIBARDOUR_API ConfigVariableWithMutation<std::string> : public ConfigVariable<std::string>
199 ConfigVariableWithMutation (std::string name, std::string val, std::string (*m)(std::string))
200 : ConfigVariable<std::string> (name, val), mutator (m) {}
202 bool set (std::string val) {
203 if (unmutated_value != val) {
204 unmutated_value = val;
205 return ConfigVariable<std::string>::set (mutator (val));
210 void set_from_string (std::string const & s) {
215 virtual std::string get_for_save() { return unmutated_value; }
216 std::string unmutated_value;
217 std::string (*mutator)(std::string);
222 #endif /* __ardour_configuration_variable_h__ */