90859b72b15989de83e7bcae9b50cafcaf536b83
[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
34         ConfigVariableBase (std::string str) : _name (str) {}
35         virtual ~ConfigVariableBase() {}
36
37         std::string name () const { return _name; }
38         void add_to_node (XMLNode&);
39         bool set_from_node (XMLNode const &);
40
41         virtual std::string get_as_string () const = 0;
42         virtual void set_from_string (std::string const &) = 0;
43
44         void show_stored_value (const std::string&);
45
46         static void set_show_stored_values (bool);
47
48   protected:
49         std::string _name;
50         static bool show_stores;
51
52         void notify ();
53         void miss ();
54 };
55
56 template<class T>
57 class ConfigVariable : public ConfigVariableBase
58 {
59   public:
60
61         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
62         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
63
64         T get() const {
65                 return value;
66         }
67
68         std::string get_as_string () const {
69                 std::stringstream ss;
70                 ss << value;
71                 return ss.str ();
72         }
73
74         virtual bool set (T val) {
75                 if (val == value) {
76                         miss ();
77                         return false;
78                 }
79                 value = val;
80                 notify ();
81                 return true;
82         }
83
84         void set_from_string (std::string const & s) {
85                 std::stringstream ss;
86                 ss << s;
87                 ss >> value;
88         }
89
90   protected:
91         virtual T get_for_save() { return value; }
92         T value;
93 };
94
95 template<class T>
96 class ConfigVariableWithMutation : public ConfigVariable<T>
97 {
98   public:
99         ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
100                 : ConfigVariable<T> (name, val), mutator (m) {}
101
102         bool set (T val) {
103                 if (unmutated_value != val) {
104                         unmutated_value = val;
105                         return ConfigVariable<T>::set (mutator (val));
106                 }
107                 return false;
108         }
109
110   protected:
111         virtual T get_for_save() { return unmutated_value; }
112         T unmutated_value;
113         T (*mutator)(T);
114 };
115
116 }
117
118 #endif /* __ardour_configuration_variable_h__ */