merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[ardour.git] / libs / ardour / ardour / configuration_variable.h
1 #ifndef __ardour_configuration_variable_h__
2 #define __ardour_configuration_variable_h__
3
4 #include <sstream>
5 #include <ostream>
6
7 #include <pbd/xml++.h>
8
9 namespace ARDOUR {
10
11 class ConfigVariableBase {
12   public:
13         enum Owner {
14                 Default = 0x1,
15                 System = 0x2,
16                 Config = 0x4,
17                 Session = 0x8,
18                 Interface = 0x10
19         };
20
21         ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
22         virtual ~ConfigVariableBase() {}
23
24         std::string name() const { return _name; }
25         Owner owner() const { return _owner; }
26
27         virtual void add_to_node (XMLNode& node) = 0;
28         virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
29
30         void show_stored_value (const std::string&);
31         static void set_show_stored_values (bool yn);
32
33   protected:
34         std::string _name;
35         Owner _owner;
36         static bool show_stores;
37
38         void notify ();
39         void miss ();
40 };
41
42 template<class T>
43 class ConfigVariable : public ConfigVariableBase
44 {
45   public:
46         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
47         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
48
49         virtual bool set (T val, Owner owner) {
50                 if (val == value) {
51                         miss ();
52                         return false;
53                 }
54                 value = val;
55                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
56                 notify ();
57                 return true;
58         }
59
60         T get() const {
61                 return value;
62         }
63
64         void add_to_node (XMLNode& node) {
65                 std::stringstream ss;
66                 ss << value;
67                 show_stored_value (ss.str());
68                 XMLNode* child = new XMLNode ("Option");
69                 child->add_property ("name", _name);
70                 child->add_property ("value", ss.str());
71                 node.add_child_nocopy (*child);
72         }
73
74         bool set_from_node (const XMLNode& node, Owner owner) {
75
76                 if (node.name() == "Config") {
77
78                         /* ardour.rc */
79
80                         const XMLProperty* prop;
81                         XMLNodeList nlist;
82                         XMLNodeConstIterator niter;
83                         XMLNode* child;
84                         
85                         nlist = node.children();
86                         
87                         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
88                                 
89                                 child = *niter;
90                                 
91                                 if (child->name() == "Option") {
92                                         if ((prop = child->property ("name")) != 0) {
93                                                 if (prop->value() == _name) {
94                                                         if ((prop = child->property ("value")) != 0) {
95                                                                 std::stringstream ss;
96                                                                 ss << prop->value();
97                                                                 ss >> value;
98                                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
99                                                                 return true;
100                                                         }
101                                                 }
102                                         }
103                                 }
104                         }
105                         
106                 } else if (node.name() == "Options") {
107
108                         /* session file */
109
110                         XMLNodeList olist;
111                         XMLNodeConstIterator oiter;
112                         XMLNode* option;
113                         const XMLProperty* opt_prop;
114                         
115                         olist = node.children();
116                         
117                         for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
118                                 
119                                 option = *oiter;
120                                 
121                                 if (option->name() == _name) {
122                                         if ((opt_prop = option->property ("val")) != 0) {
123                                                 std::stringstream ss;
124                                                 ss << opt_prop->value();
125                                                 ss >> value;
126                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
127                                                 return true;
128                                         }
129                                 }
130                         }
131                 }
132
133                 return false;
134         }
135
136   protected:
137         virtual T get_for_save() { return value; }
138         T value;
139 };
140
141 template<class T>
142 class ConfigVariableWithMutation : public ConfigVariable<T>
143 {
144   public:
145         ConfigVariableWithMutation (std::string name, T val, T (*m)(T)) 
146                 : ConfigVariable<T> (name, val), mutator (m) {}
147
148         bool set (T val, ConfigVariableBase::Owner owner) {
149                 if (unmutated_value != val) {
150                         unmutated_value = val;
151                         return ConfigVariable<T>::set (mutator (val), owner);
152                 } 
153                 return false;
154         }
155
156   protected:
157         virtual T get_for_save() { return unmutated_value; }
158         T unmutated_value;
159         T (*mutator)(T);
160 };
161
162 }
163
164 #endif /* __ardour_configuration_variable_h__ */