new tempo handling from drmoore; don't follow playhead when doing requested_return...
[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 <sstream>
24 #include <ostream>
25
26 #include <pbd/xml++.h>
27
28 namespace ARDOUR {
29
30 class ConfigVariableBase {
31   public:
32         enum Owner {
33                 Default = 0x1,
34                 System = 0x2,
35                 Config = 0x4,
36                 Session = 0x8,
37                 Interface = 0x10
38         };
39
40         ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
41         virtual ~ConfigVariableBase() {}
42
43         std::string name() const { return _name; }
44         Owner owner() const { return _owner; }
45
46         virtual void add_to_node (XMLNode& node) = 0;
47         virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
48
49         void show_stored_value (const std::string&);
50         static void set_show_stored_values (bool yn);
51
52   protected:
53         std::string _name;
54         Owner _owner;
55         static bool show_stores;
56
57         void notify ();
58         void miss ();
59 };
60
61 template<class T>
62 class ConfigVariable : public ConfigVariableBase
63 {
64   public:
65         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
66         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
67
68         virtual bool set (T val, Owner owner) {
69                 if (val == value) {
70                         miss ();
71                         return false;
72                 }
73                 value = val;
74                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
75                 notify ();
76                 return true;
77         }
78
79         T get() const {
80                 return value;
81         }
82
83         void add_to_node (XMLNode& node) {
84                 std::stringstream ss;
85                 ss << value;
86                 show_stored_value (ss.str());
87                 XMLNode* child = new XMLNode ("Option");
88                 child->add_property ("name", _name);
89                 child->add_property ("value", ss.str());
90                 node.add_child_nocopy (*child);
91         }
92
93         bool set_from_node (const XMLNode& node, Owner owner) {
94
95                 if (node.name() == "Config") {
96
97                         /* ardour.rc */
98
99                         const XMLProperty* prop;
100                         XMLNodeList nlist;
101                         XMLNodeConstIterator niter;
102                         XMLNode* child;
103                         
104                         nlist = node.children();
105                         
106                         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
107                                 
108                                 child = *niter;
109                                 
110                                 if (child->name() == "Option") {
111                                         if ((prop = child->property ("name")) != 0) {
112                                                 if (prop->value() == _name) {
113                                                         if ((prop = child->property ("value")) != 0) {
114                                                                 std::stringstream ss;
115                                                                 ss << prop->value();
116                                                                 ss >> value;
117                                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
118                                                                 return true;
119                                                         }
120                                                 }
121                                         }
122                                 }
123                         }
124                         
125                 } else if (node.name() == "Options") {
126
127                         /* session file */
128
129                         XMLNodeList olist;
130                         XMLNodeConstIterator oiter;
131                         XMLNode* option;
132                         const XMLProperty* opt_prop;
133                         
134                         olist = node.children();
135                         
136                         for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
137                                 
138                                 option = *oiter;
139                                 
140                                 if (option->name() == _name) {
141                                         if ((opt_prop = option->property ("val")) != 0) {
142                                                 std::stringstream ss;
143                                                 ss << opt_prop->value();
144                                                 ss >> value;
145                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
146                                                 return true;
147                                         }
148                                 }
149                         }
150                 }
151
152                 return false;
153         }
154
155   protected:
156         virtual T get_for_save() { return value; }
157         T value;
158 };
159
160 template<class T>
161 class ConfigVariableWithMutation : public ConfigVariable<T>
162 {
163   public:
164         ConfigVariableWithMutation (std::string name, T val, T (*m)(T)) 
165                 : ConfigVariable<T> (name, val), mutator (m) {}
166
167         bool set (T val, ConfigVariableBase::Owner owner) {
168                 if (unmutated_value != val) {
169                         unmutated_value = val;
170                         return ConfigVariable<T>::set (mutator (val), owner);
171                 } 
172                 return false;
173         }
174
175   protected:
176         virtual T get_for_save() { return unmutated_value; }
177         T unmutated_value;
178         T (*mutator)(T);
179 };
180
181 }
182
183 #endif /* __ardour_configuration_variable_h__ */