MIDI region forking, plus Playlist::regions_to_read() fix forward ported from 2.X...
[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 "ardour/types.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   protected:
45         std::string _name;
46
47         void notify ();
48         void miss ();
49 };
50
51 template<class T>
52 class ConfigVariable : public ConfigVariableBase
53 {
54   public:
55
56         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
57         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
58
59         T get() const {
60                 return value;
61         }
62
63         std::string get_as_string () const {
64                 std::ostringstream ss;
65                 ss << value;
66                 return ss.str ();
67         }
68
69         virtual bool set (T val) {
70                 if (val == value) {
71                         miss ();
72                         return false;
73                 }
74                 value = val;
75                 notify ();
76                 return true;
77         }
78
79         void set_from_string (std::string const & s) {
80                 std::stringstream ss;
81                 ss << s;
82                 ss >> value;
83         }
84
85   protected:
86         virtual T get_for_save() { return value; }
87         T value;
88 };
89
90 template<class T>
91 class ConfigVariableWithMutation : public ConfigVariable<T>
92 {
93   public:
94         ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
95                 : ConfigVariable<T> (name, val), mutator (m) {}
96
97         bool set (T val) {
98                 if (unmutated_value != val) {
99                         unmutated_value = val;
100                         return ConfigVariable<T>::set (mutator (val));
101                 }
102                 return false;
103         }
104
105   protected:
106         virtual T get_for_save() { return unmutated_value; }
107         T unmutated_value;
108         T (*mutator)(T);
109 };
110
111 }
112
113 #endif /* __ardour_configuration_variable_h__ */