2 Copyright (C) 2010 Paul Davis
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.
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.
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.
20 #ifndef __pbd_properties_h__
21 #define __pbd_properties_h__
29 #include "pbd/xml++.h"
30 #include "pbd/property_basics.h"
31 #include "pbd/property_list.h"
32 #include "pbd/enumwriter.h"
36 /** Parent class for classes which represent a single scalar property in a Stateful object
39 class PropertyTemplate : public PropertyBase
42 PropertyTemplate (PropertyDescriptor<T> p, T const& v)
43 : PropertyBase (p.property_id)
48 PropertyTemplate (PropertyDescriptor<T> p, T const& o, T const& c)
49 : PropertyBase (p.property_id)
55 PropertyTemplate<T>& operator=(PropertyTemplate<T> const& s) {
56 /* XXX: isn't there a nicer place to do this? */
57 _have_old = s._have_old;
58 _property_id = s._property_id;
60 _current = s._current;
65 T & operator=(T const& v) {
70 T & operator+=(T const& v) {
75 bool operator==(const T& other) const {
76 return _current == other;
79 bool operator!=(const T& other) const {
80 return _current != other;
83 operator T const &() const {
87 T const& val () const {
91 void clear_changes () {
95 void get_changes_as_xml (XMLNode* history_node) const {
96 XMLNode* node = history_node->add_child (property_name());
97 node->add_property ("from", to_string (_old));
98 node->add_property ("to", to_string (_current));
101 bool set_value (XMLNode const & node) {
103 XMLProperty const* p = node.property (property_name());
106 T const v = from_string (p->value ());
117 void get_value (XMLNode & node) const {
118 node.add_property (property_name(), to_string (_current));
121 bool changed () const { return _have_old; }
123 void apply_changes (PropertyBase const * p) {
124 T v = dynamic_cast<const PropertyTemplate<T>* > (p)->val ();
131 T const tmp = _current;
136 void get_changes_as_properties (PropertyList& changes, Command *) const {
137 if (this->_have_old) {
138 changes.add (clone ());
143 /** Constructs a PropertyTemplate with a default
144 value for _old and _current.
147 PropertyTemplate (PropertyDescriptor<T> p)
148 : PropertyBase (p.property_id)
151 void set (T const& v) {
158 /* value has been reset to the value
159 at the start of a history transaction,
160 before clear_changes() is called.
161 thus there is effectively no apparent
162 history for this property.
172 virtual std::string to_string (T const& v) const = 0;
173 virtual T from_string (std::string const& s) const = 0;
181 std::ostream & operator<<(std::ostream& os, PropertyTemplate<T> const& s)
183 return os << s.val ();
186 /** Representation of a single piece of scalar state in a Stateful; for use
187 * with types that can be written to / read from stringstreams.
190 class Property : public PropertyTemplate<T>
193 Property (PropertyDescriptor<T> q, T const& v)
194 : PropertyTemplate<T> (q, v)
197 Property (PropertyDescriptor<T> q, T const& o, T const& c)
198 : PropertyTemplate<T> (q, o, c)
201 Property<T>* clone () const {
202 return new Property<T> (*this);
205 Property<T>* clone_from_xml (const XMLNode& node) const {
206 XMLNodeList const & children = node.children ();
207 XMLNodeList::const_iterator i = children.begin();
208 while (i != children.end() && (*i)->name() != this->property_name()) {
212 if (i == children.end()) {
215 XMLProperty* from = (*i)->property ("from");
216 XMLProperty* to = (*i)->property ("to");
222 return new Property<T> (this->property_id(), from_string (from->value()), from_string (to->value ()));
225 T & operator=(T const& v) {
227 return this->_current;
231 friend class PropertyFactory;
233 Property (PropertyDescriptor<T> q)
234 : PropertyTemplate<T> (q)
237 /* Note that we do not set a locale for the streams used
238 * in to_string() or from_string(), because we want the
239 * format to be portable across locales (i.e. C or
240 * POSIX). Also, there is the small matter of
241 * std::locale aborting on OS X if used with anything
242 * other than C or POSIX locales.
244 virtual std::string to_string (T const& v) const {
246 s.precision (12); // in case its floating point
251 virtual T from_string (std::string const& s) const {
252 std::stringstream t (s);
260 /** Specialization, for std::string which is common and special (see to_string() and from_string()
261 * Using stringstream to read from a std::string is easy to get wrong because of whitespace
265 class Property<std::string> : public PropertyTemplate<std::string>
268 Property (PropertyDescriptor<std::string> q, std::string const& v)
269 : PropertyTemplate<std::string> (q, v)
272 Property<std::string>* clone () const {
273 return new Property<std::string> (*this);
276 std::string & operator=(std::string const& v) {
278 return this->_current;
282 std::string to_string (std::string const& v) const {
286 std::string from_string (std::string const& s) const {
293 class EnumProperty : public Property<T>
296 EnumProperty (PropertyDescriptor<T> q, T const& v)
300 T & operator=(T const& v) {
302 return this->_current;
306 std::string to_string (T const & v) const {
307 return enum_2_string (v);
310 T from_string (std::string const & s) const {
311 return static_cast<T> (string_2_enum (s, this->_current));
315 } /* namespace PBD */
317 #include "pbd/property_list_impl.h"
318 #include "pbd/property_basics_impl.h"
320 #endif /* __pbd_properties_h__ */