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>
30 class ConfigVariableBase {
40 ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
41 virtual ~ConfigVariableBase() {}
43 std::string name() const { return _name; }
44 Owner owner() const { return _owner; }
46 virtual void add_to_node (XMLNode& node) = 0;
47 virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
49 void show_stored_value (const std::string&);
50 static void set_show_stored_values (bool yn);
55 static bool show_stores;
62 class ConfigVariable : public ConfigVariableBase
65 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
66 ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
68 virtual bool set (T val, Owner owner = ARDOUR::ConfigVariableBase::Config) {
74 _owner = (ConfigVariableBase::Owner)(_owner |owner);
83 void add_to_node (XMLNode& node) {
85 if (node.name() == "Canvas") {
91 show_stored_value (ss.str());
92 XMLNode* child = new XMLNode ("Option");
93 child->add_property ("name", _name);
94 child->add_property ("value", ss.str());
95 node.add_child_nocopy (*child);
98 bool set_from_node (const XMLNode& node, Owner owner) {
100 if (node.name() == "Config" || node.name() == "Canvas") {
104 const XMLProperty* prop;
106 XMLNodeConstIterator niter;
109 nlist = node.children();
111 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
115 if (child->name() == "Option") {
116 if ((prop = child->property ("name")) != 0) {
117 if (prop->value() == _name) {
118 if ((prop = child->property ("value")) != 0) {
119 std::stringstream ss;
120 if (node.name() == "Canvas")
124 _owner = (ConfigVariableBase::Owner)(_owner |owner);
132 } else if (node.name() == "Options") {
137 XMLNodeConstIterator oiter;
139 const XMLProperty* opt_prop;
141 olist = node.children();
143 for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
147 if (option->name() == _name) {
148 if ((opt_prop = option->property ("val")) != 0) {
149 std::stringstream ss;
150 ss << opt_prop->value();
152 _owner = (ConfigVariableBase::Owner)(_owner |owner);
163 virtual T get_for_save() { return value; }
168 class ConfigVariableWithMutation : public ConfigVariable<T>
171 ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
172 : ConfigVariable<T> (name, val), mutator (m) {}
174 bool set (T val, ConfigVariableBase::Owner owner) {
175 if (unmutated_value != val) {
176 unmutated_value = val;
177 return ConfigVariable<T>::set (mutator (val), owner);
183 virtual T get_for_save() { return unmutated_value; }
190 #endif /* __ardour_configuration_variable_h__ */