add export visibility macros across libardour
[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 <iostream>
24 #include <sstream>
25
26 #include "pbd/xml++.h"
27 #include "pbd/convert.h"
28 #include "ardour/libardour_visibility.h"
29 #include "ardour/types.h"
30 #include "ardour/utils.h"
31
32 namespace ARDOUR {
33
34 class LIBARDOUR_API ConfigVariableBase {
35   public:
36
37         ConfigVariableBase (std::string str) : _name (str) {}
38         virtual ~ConfigVariableBase() {}
39
40         std::string name () const { return _name; }
41         void add_to_node (XMLNode&);
42         bool set_from_node (XMLNode const &);
43
44         virtual std::string get_as_string () const = 0;
45         virtual void set_from_string (std::string const &) = 0;
46
47   protected:
48         std::string _name;
49
50         void notify ();
51         void miss ();
52 };
53
54 template<class T>
55 class LIBARDOUR_API ConfigVariable : public ConfigVariableBase
56 {
57   public:
58
59         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
60         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
61
62         T get() const {
63                 return value;
64         }
65
66         std::string get_as_string () const {
67                 std::ostringstream ss;
68                 ss << value;
69                 return ss.str ();
70         }
71
72         virtual bool set (T val) {
73                 if (val == value) {
74                         miss ();
75                         return false;
76                 }
77                 value = val;
78                 notify ();
79                 return true;
80         }
81
82         virtual void set_from_string (std::string const & s) {
83                 std::stringstream ss;
84                 ss << s;
85                 ss >> value;
86         }
87
88   protected:
89         virtual T get_for_save() { return value; }
90         T value;
91 };
92
93 /** Specialisation of ConfigVariable for std::string to cope with whitespace properly */
94 template<>
95 class LIBARDOUR_API ConfigVariable<std::string> : public ConfigVariableBase
96 {
97   public:
98
99         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
100         ConfigVariable (std::string str, std::string val) : ConfigVariableBase (str), value (val) {}
101
102         std::string get() const {
103                 return value;
104         }
105
106         std::string get_as_string () const {
107                 return value;
108         }
109
110         virtual bool set (std::string val) {
111                 if (val == value) {
112                         miss ();
113                         return false;
114                 }
115                 value = val;
116                 notify ();
117                 return true;
118         }
119
120         virtual void set_from_string (std::string const & s) {
121                 value = s;
122         }
123
124   protected:
125         virtual std::string get_for_save() { return value; }
126         std::string value;
127 };
128
129 template<>
130 class LIBARDOUR_API ConfigVariable<bool> : public ConfigVariableBase
131 {
132   public:
133
134         ConfigVariable (std::string str) : ConfigVariableBase (str), value (false) {}
135         ConfigVariable (std::string str, bool val) : ConfigVariableBase (str), value (val) {}
136
137         bool get() const {
138                 return value;
139         }
140
141         std::string get_as_string () const {
142                 std::ostringstream ss;
143                 ss << value;
144                 return ss.str ();
145         }
146
147         virtual bool set (bool val) {
148                 if (val == value) {
149                         miss ();
150                         return false;
151                 }
152                 value = val;
153                 notify ();
154                 return true;
155         }
156
157         void set_from_string (std::string const & s) {
158                 value = PBD::string_is_affirmative (s);
159         }
160
161   protected:
162         virtual bool get_for_save() { return value; }
163         bool value;
164 };
165
166 template<class T>
167 class LIBARDOUR_API ConfigVariableWithMutation : public ConfigVariable<T>
168 {
169   public:
170         ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
171                 : ConfigVariable<T> (name, val), mutator (m) {}
172
173         bool set (T val) {
174                 if (unmutated_value != val) {
175                         unmutated_value = val;
176                         return ConfigVariable<T>::set (mutator (val));
177                 }
178                 return false;
179         }
180
181         void set_from_string (std::string const & s) {
182                 T v;
183                 std::stringstream ss;
184                 ss << s;
185                 ss >> v;
186                 set (v);
187         }
188
189   protected:
190         virtual T get_for_save() { return unmutated_value; }
191         T unmutated_value;
192         T (*mutator)(T);
193 };
194
195 template<>
196 class LIBARDOUR_API ConfigVariableWithMutation<std::string> : public ConfigVariable<std::string>
197 {
198   public:
199         ConfigVariableWithMutation (std::string name, std::string val, std::string (*m)(std::string))
200                 : ConfigVariable<std::string> (name, val), mutator (m) {}
201
202         bool set (std::string val) {
203                 if (unmutated_value != val) {
204                         unmutated_value = val;
205                         return ConfigVariable<std::string>::set (mutator (val));
206                 }
207                 return false;
208         }
209
210         void set_from_string (std::string const & s) {
211                 set (s);
212         }
213
214   protected:
215         virtual std::string get_for_save() { return unmutated_value; }
216         std::string unmutated_value;
217         std::string (*mutator)(std::string);
218 };
219
220 }
221
222 #endif /* __ardour_configuration_variable_h__ */