Ardour  9.0-pre0-582-g084a23a80d
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 #pragma once
20 
21 #include <string>
22 
23 #include "pbd/xml++.h"
24 #include "pbd/string_convert.h"
25 #include "pbd/libpbd_visibility.h"
26 
27 namespace PBD {
28 
30  public:
31 
32  ConfigVariableBase (std::string str) : _name (str) {}
33  virtual ~ConfigVariableBase() {}
34 
35  std::string name () const { return _name; }
36  void add_to_node (XMLNode&) const;
37  bool set_from_node (XMLNode const &);
38 
39  virtual std::string get_as_string () const = 0;
40  virtual void set_from_string (std::string const &) = 0;
41 
42  protected:
43  std::string _name;
44 
45  void notify ();
46  void miss ();
47 };
48 
49 template<class T>
50 class /*LIBPBD_API*/ ConfigVariable : public ConfigVariableBase
51 {
52  public:
53 
54  ConfigVariable (std::string str) : ConfigVariableBase (str) {}
55  ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
56 
57  T get() const {
58  return value;
59  }
60 
61  std::string get_as_string () const {
62  return to_string<T>(value);
63  }
64 
65  virtual bool set (T val) {
66  if (val == value) {
67  miss ();
68  return false;
69  }
70  value = val;
71  notify ();
72  return true;
73  }
74 
75  virtual void set_from_string (std::string const & s) {
76  value = string_to<T>(s);
77  }
78 
79  protected:
80  virtual T get_for_save() { return value; }
81  T value;
82 };
83 
85 template<>
86 class /*LIBPBD_API*/ ConfigVariable<std::string> : public ConfigVariableBase
87 {
88  public:
89 
90  ConfigVariable (std::string str) : ConfigVariableBase (str) {}
91  ConfigVariable (std::string str, std::string val) : ConfigVariableBase (str), value (val) {}
92 
93  std::string get() const {
94  return value;
95  }
96 
97  std::string get_as_string () const {
98  return value;
99  }
100 
101  virtual bool set (std::string val) {
102  if (val == value) {
103  miss ();
104  return false;
105  }
106  value = val;
107  notify ();
108  return true;
109  }
110 
111  virtual void set_from_string (std::string const & s) {
112  value = s;
113  }
114 
115  protected:
116  virtual std::string get_for_save() { return value; }
117  std::string value;
118 };
119 
120 template<class T>
121 class /*LIBPBD_API*/ ConfigVariableWithMutation : public ConfigVariable<T>
122 {
123  public:
124  ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
125  : ConfigVariable<T> (name, m (val)), unmutated_value (val), mutator (m) {}
126 
127  bool set (T val) {
128  if (unmutated_value != val) {
129  unmutated_value = val;
130  return ConfigVariable<T>::set (mutator (val));
131  }
132  return false;
133  }
134 
135  void set_from_string (std::string const & s) {
136  set (string_to<T>(s));
137  }
138 
139  protected:
140  virtual T get_for_save() { return unmutated_value; }
142  T (*mutator)(T);
143 };
144 
145 template<>
146 class /*LIBPBD_API*/ ConfigVariableWithMutation<std::string> : public ConfigVariable<std::string>
147 {
148  public:
149  ConfigVariableWithMutation (std::string name, std::string val, std::string (*m)(std::string))
150  : ConfigVariable<std::string> (name, val), mutator (m) {}
151 
152  bool set (std::string val) {
153  if (unmutated_value != val) {
154  unmutated_value = val;
156  }
157  return false;
158  }
159 
160  void set_from_string (std::string const & s) {
161  set (s);
162  }
163 
164  protected:
165  virtual std::string get_for_save() { return unmutated_value; }
166  std::string unmutated_value;
167  std::string (*mutator)(std::string);
168 };
169 
170 }
171 
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