03e42aa4517cda520549969419a344d142ace027
[ardour.git] / libs / pbd / pbd / properties.h
1 /*
2     Copyright (C) 2010 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 __pbd_properties_h__
21 #define __pbd_properties_h__
22
23 #include <string>
24 #include <sstream>
25 #include <list>
26 #include <set>
27 #include <glib.h>
28
29 #include "pbd/xml++.h"
30 #include "pbd/property_basics.h"
31 #include "pbd/property_list.h"
32 #include "pbd/enumwriter.h"
33
34 namespace PBD {
35
36 /** Parent class for classes which represent a single scalar property in a Stateful object 
37  */
38 template<class T>
39 class PropertyTemplate : public PropertyBase
40 {
41 public:
42         PropertyTemplate (PropertyDescriptor<T> p, T const& v)
43                 : PropertyBase (p.property_id)
44                 , _have_old (false)
45                 , _current (v)
46         {}
47
48         PropertyTemplate<T>& operator=(PropertyTemplate<T> const& s) {
49                 /* XXX: isn't there a nicer place to do this? */
50                 _have_old    = s._have_old;
51                 _property_id = s._property_id;
52
53                 _current = s._current;
54                 _old     = s._old;
55                 return *this;
56         }
57
58         T & operator=(T const& v) {
59                 set (v);
60                 return _current;
61         }
62
63         T & operator+=(T const& v) {
64                 set (_current + v);
65                 return _current;
66         }
67
68         bool operator==(const T& other) const {
69                 return _current == other;
70         }
71
72         bool operator!=(const T& other) const {
73                 return _current != other;
74         }
75
76         operator T const &() const {
77                 return _current;
78         }
79
80         T const& val () const {
81                 return _current;
82         }
83
84         void clear_history () {
85                 _have_old = false;
86         }
87
88         void add_history_state (XMLNode* history_node) const {
89                 /* We can get to the current state of a scalar property like this one simply
90                    by knowing what the new state is.
91                 */
92                 history_node->add_property (property_name(), to_string (_current));
93         }
94
95         /** Try to set state from the property of an XML node.
96          *  @param node XML node.
97          *  @return true if the value of the property is changed
98          */
99         bool set_state_from_owner_state (XMLNode const& owner_state) {
100
101                 XMLProperty const* p = owner_state.property (property_name());
102
103                 if (p) {
104                         T const v = from_string (p->value ());
105
106                         if (v != _current) {
107                                 set (v);
108                                 return true;
109                         }
110                 }
111
112                 return false;
113         }
114
115         void add_state_to_owner_state (XMLNode& owner_state) const {
116                 owner_state.add_property (property_name(), to_string (_current));
117         }
118
119         bool changed () const { return _have_old; }
120         void set_state_from_property (PropertyBase const * p) {
121                 T v = dynamic_cast<const PropertyTemplate<T>* > (p)->val ();
122                 if (v != _current) {
123                         set (v);
124                 }
125         }
126
127 protected:
128         /** Constructs a PropertyTemplate with a default
129             value for _old and _current.
130         */
131
132         PropertyTemplate (PropertyDescriptor<T> p)
133                 : PropertyBase (p.property_id)
134         {}
135
136         void set (T const& v) {
137                 if (!_have_old) {
138                         _old      = _current;
139                         _have_old = true;
140                 }
141                 _current  = v;
142         }
143
144         virtual std::string to_string (T const& v) const             = 0;
145         virtual T           from_string (std::string const& s) const = 0;
146
147         bool _have_old;
148         T _current;
149         T _old;
150 };
151
152 template<class T>
153 std::ostream & operator<<(std::ostream& os, PropertyTemplate<T> const& s)
154 {
155         return os << s.val ();
156 }
157
158 /** Representation of a single piece of scalar state in a Stateful; for use
159  *  with types that can be written to / read from stringstreams.
160  */
161 template<class T>
162 class Property : public PropertyTemplate<T>
163 {
164 public:
165         Property (PropertyDescriptor<T> q, T const& v)
166                 : PropertyTemplate<T> (q, v)
167         {}
168         
169         void diff (PropertyList& undo, PropertyList& redo, Command* /*ignored*/) const {
170                 if (this->_have_old) {
171                         undo.add (new Property<T> (this->property_id(), this->_old));
172                         redo.add (new Property<T> (this->property_id(), this->_current));
173                 }
174         }
175
176         Property<T>* maybe_clone_self_if_found_in_history_node (const XMLNode& node) const {
177                 const XMLProperty* prop = node.property (this->property_name());
178                 if (!prop) {
179                         return 0;
180                 }
181                 return new Property<T> (this->property_id(), from_string (prop->value()));
182         }
183
184         T & operator=(T const& v) {
185                 this->set (v);
186                 return this->_current;
187         }
188
189 private:
190         friend class PropertyFactory;
191
192         Property (PropertyDescriptor<T> q)
193                 : PropertyTemplate<T> (q)
194         {}
195
196         /* Note that we do not set a locale for the streams used
197          * in to_string() or from_string(), because we want the
198          * format to be portable across locales (i.e. C or
199          * POSIX). Also, there is the small matter of
200          * std::locale aborting on OS X if used with anything
201          * other than C or POSIX locales.
202          */
203         virtual std::string to_string (T const& v) const {
204                 std::stringstream s;
205                 s.precision (12); // in case its floating point
206                 s << v;
207                 return s.str ();
208         }
209
210         virtual T from_string (std::string const& s) const {
211                 std::stringstream t (s);
212                 T                 v;
213                 t >> v;
214                 return v;
215         }
216
217 };
218
219 /** Specialization, for std::string which is common and special (see to_string() and from_string()
220  *  Using stringstream to read from a std::string is easy to get wrong because of whitespace
221  *  separators, etc.
222  */
223 template<>
224 class Property<std::string> : public PropertyTemplate<std::string>
225 {
226 public:
227         Property (PropertyDescriptor<std::string> q, std::string const& v)
228                 : PropertyTemplate<std::string> (q, v)
229         {}
230
231         void diff (PropertyList& before, PropertyList& after, Command* /*ignored*/) const {
232                 if (this->_have_old) {
233                         before.add (new Property<std::string> (PropertyDescriptor<std::string> (this->property_id()), this->_old));
234                         after.add (new Property<std::string> (PropertyDescriptor<std::string> (this->property_id()), this->_current));
235                 }
236         }
237
238         std::string & operator=(std::string const& v) {
239                 this->set (v);
240                 return this->_current;
241         }
242
243 private:
244         std::string to_string (std::string const& v) const {
245                 return _current;
246         }
247
248         std::string from_string (std::string const& s) const {
249                 return s;
250         }
251
252 };
253
254 template<class T>
255 class EnumProperty : public Property<T>
256 {
257 public:
258         EnumProperty (PropertyDescriptor<T> q, T const& v)
259                 : Property<T> (q, v)
260         {}
261
262         T & operator=(T const& v) {
263                 this->set (v);
264                 return this->_current;
265         }
266
267 private:
268         std::string to_string (T const & v) const {
269                 return enum_2_string (v);
270         }
271
272         T from_string (std::string const & s) const {
273                 return static_cast<T> (string_2_enum (s, this->_current));
274         }
275 };
276         
277 } /* namespace PBD */
278
279 #include "pbd/property_list_impl.h"
280 #include "pbd/property_basics_impl.h"
281
282 #endif /* __pbd_properties_h__ */