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