Ardour  8.12
configuration_variable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2015 Paul Davis <paul@linuxaudiosystems.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __libpbd_configuration_variable_h__
20 #define __libpbd_configuration_variable_h__
21 
22 #include <string>
23 
24 #include "pbd/xml++.h"
25 #include "pbd/string_convert.h"
26 #include "pbd/libpbd_visibility.h"
27 
28 namespace PBD {
29 
31  public:
32 
33  ConfigVariableBase (std::string str) : _name (str) {}
34  virtual ~ConfigVariableBase() {}
35 
36  std::string name () const { return _name; }
37  void add_to_node (XMLNode&) const;
38  bool set_from_node (XMLNode const &);
39 
40  virtual std::string get_as_string () const = 0;
41  virtual void set_from_string (std::string const &) = 0;
42 
43  protected:
44  std::string _name;
45 
46  void notify ();
47  void miss ();
48 };
49 
50 template<class T>
51 class /*LIBPBD_API*/ ConfigVariable : public ConfigVariableBase
52 {
53  public:
54 
55  ConfigVariable (std::string str) : ConfigVariableBase (str) {}
56  ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
57 
58  T get() const {
59  return value;
60  }
61 
62  std::string get_as_string () const {
63  return to_string<T>(value);
64  }
65 
66  virtual bool set (T val) {
67  if (val == value) {
68  miss ();
69  return false;
70  }
71  value = val;
72  notify ();
73  return true;
74  }
75 
76  virtual void set_from_string (std::string const & s) {
77  value = string_to<T>(s);
78  }
79 
80  protected:
81  virtual T get_for_save() { return value; }
82  T value;
83 };
84 
86 template<>
87 class /*LIBPBD_API*/ ConfigVariable<std::string> : public ConfigVariableBase
88 {
89  public:
90 
91  ConfigVariable (std::string str) : ConfigVariableBase (str) {}
92  ConfigVariable (std::string str, std::string val) : ConfigVariableBase (str), value (val) {}
93 
94  std::string get() const {
95  return value;
96  }
97 
98  std::string get_as_string () const {
99  return value;
100  }
101 
102  virtual bool set (std::string val) {
103  if (val == value) {
104  miss ();
105  return false;
106  }
107  value = val;
108  notify ();
109  return true;
110  }
111 
112  virtual void set_from_string (std::string const & s) {
113  value = s;
114  }
115 
116  protected:
117  virtual std::string get_for_save() { return value; }
118  std::string value;
119 };
120 
121 template<class T>
122 class /*LIBPBD_API*/ ConfigVariableWithMutation : public ConfigVariable<T>
123 {
124  public:
125  ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
126  : ConfigVariable<T> (name, m (val)), unmutated_value (val), mutator (m) {}
127 
128  bool set (T val) {
129  if (unmutated_value != val) {
130  unmutated_value = val;
131  return ConfigVariable<T>::set (mutator (val));
132  }
133  return false;
134  }
135 
136  void set_from_string (std::string const & s) {
137  set (string_to<T>(s));
138  }
139 
140  protected:
141  virtual T get_for_save() { return unmutated_value; }
143  T (*mutator)(T);
144 };
145 
146 template<>
147 class /*LIBPBD_API*/ ConfigVariableWithMutation<std::string> : public ConfigVariable<std::string>
148 {
149  public:
150  ConfigVariableWithMutation (std::string name, std::string val, std::string (*m)(std::string))
151  : ConfigVariable<std::string> (name, val), mutator (m) {}
152 
153  bool set (std::string val) {
154  if (unmutated_value != val) {
155  unmutated_value = val;
157  }
158  return false;
159  }
160 
161  void set_from_string (std::string const & s) {
162  set (s);
163  }
164 
165  protected:
166  virtual std::string get_for_save() { return unmutated_value; }
167  std::string unmutated_value;
168  std::string (*mutator)(std::string);
169 };
170 
171 }
172 
173 #endif /* __libpbd_configuration_variable_h__ */
void add_to_node(XMLNode &) const
bool set_from_node(XMLNode const &)
ConfigVariableBase(std::string str)
virtual std::string get_as_string() const =0
virtual void set_from_string(std::string const &)=0
ConfigVariableWithMutation(std::string name, std::string val, std::string(*m)(std::string))
void set_from_string(std::string const &s)
ConfigVariableWithMutation(std::string name, T val, T(*m)(T))
virtual void set_from_string(std::string const &s)
virtual bool set(std::string val)
ConfigVariable(std::string str, std::string val)
std::string get_as_string() const
ConfigVariable(std::string str, T val)
virtual bool set(T val)
ConfigVariable(std::string str)
virtual void set_from_string(std::string const &s)
Definition: xml++.h:114
#define LIBPBD_API
Definition: axis_view.h:42