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__
27 #include <pbd/xml++.h>
31 class ConfigVariableBase {
41 ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
42 virtual ~ConfigVariableBase() {}
44 std::string name() const { return _name; }
45 Owner owner() const { return _owner; }
47 virtual void add_to_node (XMLNode& node) = 0;
48 virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
50 void show_stored_value (const std::string&);
51 static void set_show_stored_values (bool yn);
56 static bool show_stores;
63 class ConfigVariable : public ConfigVariableBase
66 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
67 ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
69 virtual bool set (T val, Owner owner = ARDOUR::ConfigVariableBase::Config) {
75 _owner = (ConfigVariableBase::Owner)(_owner |owner);
84 void add_to_node (XMLNode& node) {
87 show_stored_value (ss.str());
88 XMLNode* child = new XMLNode ("Option");
89 child->add_property ("name", _name);
90 child->add_property ("value", ss.str());
91 node.add_child_nocopy (*child);
94 bool set_from_node (const XMLNode& node, Owner owner) {
96 if (node.name() == "Config") {
100 const XMLProperty* prop;
102 XMLNodeConstIterator niter;
105 nlist = node.children();
107 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
111 if (child->name() == "Option") {
112 if ((prop = child->property ("name")) != 0) {
113 if (prop->value() == _name) {
114 if ((prop = child->property ("value")) != 0) {
115 std::stringstream ss;
118 _owner = (ConfigVariableBase::Owner)(_owner |owner);
126 } else if (node.name() == "Options") {
131 XMLNodeConstIterator oiter;
133 const XMLProperty* opt_prop;
135 olist = node.children();
137 for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
141 if (option->name() == _name) {
142 if ((opt_prop = option->property ("val")) != 0) {
143 std::stringstream ss;
144 ss << opt_prop->value();
146 _owner = (ConfigVariableBase::Owner)(_owner |owner);
157 virtual T get_for_save() { return value; }
162 class ConfigVariableWithMutation : public ConfigVariable<T>
165 ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
166 : ConfigVariable<T> (name, val), mutator (m) {}
168 bool set (T val, ConfigVariableBase::Owner owner) {
169 if (unmutated_value != val) {
170 unmutated_value = val;
171 return ConfigVariable<T>::set (mutator (val), owner);
177 virtual T get_for_save() { return unmutated_value; }
184 #endif /* __ardour_configuration_variable_h__ */