Merged with trunk R992.
[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   protected:
31         std::string _name;
32         Owner _owner;
33 };
34
35 template<class T>
36 class ConfigVariable : public ConfigVariableBase
37 {
38   public:
39         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
40         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
41
42         virtual bool set (T val, Owner owner) {
43                 if (val == value) {
44                         return false;
45                 }
46                 value = val;
47                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
48                 return true;
49         }
50
51         T get() const {
52                 return value;
53         }
54
55         void add_to_node (XMLNode& node) {
56                 std::stringstream ss;
57                 ss << value;
58                 XMLNode* child = new XMLNode ("Option");
59                 child->add_property ("name", _name);
60                 child->add_property ("value", ss.str());
61                 node.add_child_nocopy (*child);
62         }
63
64         bool set_from_node (const XMLNode& node, Owner owner) {
65
66                 if (node.name() == "Config") {
67
68                         /* ardour.rc */
69
70                         const XMLProperty* prop;
71                         XMLNodeList nlist;
72                         XMLNodeConstIterator niter;
73                         XMLNode* child;
74                         
75                         nlist = node.children();
76                         
77                         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
78                                 
79                                 child = *niter;
80                                 
81                                 if (child->name() == "Option") {
82                                         if ((prop = child->property ("name")) != 0) {
83                                                 if (prop->value() == _name) {
84                                                         if ((prop = child->property ("value")) != 0) {
85                                                                 std::stringstream ss;
86                                                                 ss << prop->value();
87                                                                 ss >> value;
88                                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
89                                                                 return true;
90                                                         }
91                                                 }
92                                         }
93                                 }
94                         }
95                         
96                 } else if (node.name() == "Options") {
97
98                         /* session file */
99
100                         XMLNodeList olist;
101                         XMLNodeConstIterator oiter;
102                         XMLNode* option;
103                         const XMLProperty* opt_prop;
104                         
105                         olist = node.children();
106                         
107                         for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
108                                 
109                                 option = *oiter;
110                                 
111                                 if (option->name() == _name) {
112                                         if ((opt_prop = option->property ("val")) != 0) {
113                                                 std::stringstream ss;
114                                                 ss << opt_prop->value();
115                                                 ss >> value;
116                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
117                                                 return true;
118                                         }
119                                 }
120                         }
121                 }
122
123                 return false;
124         }
125
126   protected:
127         virtual T get_for_save() { return value; }
128         T value;
129 };
130
131 template<class T>
132 class ConfigVariableWithMutation : public ConfigVariable<T>
133 {
134   public:
135         ConfigVariableWithMutation (std::string name, T val, T (*m)(T)) 
136                 : ConfigVariable<T> (name, val), mutator (m) {}
137
138         bool set (T val, ConfigVariableBase::Owner owner) {
139                 if (unmutated_value != val) {
140                         unmutated_value = val;
141                         return ConfigVariable<T>::set (mutator (val), owner);
142                 } 
143                 return false;
144         }
145
146   protected:
147         virtual T get_for_save() { return unmutated_value; }
148         T unmutated_value;
149         T (*mutator)(T);
150 };
151
152 }
153
154 #endif /* __ardour_configuration_variable_h__ */