summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/properties.h
blob: 10c661102dfe14c43518bac4f256b29124a00ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
    Copyright (C) 2010 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __pbd_properties_h__
#define __pbd_properties_h__

#include <string>
#include <sstream>
#include <list>
#include <glib.h>

#include "pbd/xml++.h"

namespace PBD {

enum PropertyChange {
	range_guarantee = ~0ULL
};

PropertyChange new_change ();

typedef GQuark PropertyID;

template<typename T> 
struct PropertyDescriptor {
    PropertyID id;
    typedef T value_type;
};

/** Base (non template) part of Property */	
class PropertyBase
{
public:
	PropertyBase (PropertyID pid, PropertyChange c)
		: _have_old (false)
		, _property_id (pid)
		, _change (c)
	{

	}

	/** Forget about any old value for this state */
	void clear_history () {
		_have_old = false;
	}

	virtual void diff (XMLNode *, XMLNode *) const = 0;
	virtual void diff (PropertyChange&) const = 0;
	virtual PropertyChange set_state (XMLNode const &) = 0;
	virtual void add_state (XMLNode &) const = 0;

	const gchar* property_name() const { return g_quark_to_string (_property_id); }
	PropertyChange change() const { return _change; }
	PropertyID id() const { return _property_id; }

	bool operator== (PropertyID pid) const {
		return _property_id == pid;
	}

protected:
	bool _have_old;
	PropertyID _property_id;
	PropertyChange _change;
};

/** Parent class for classes which represent a single property in a Stateful object */
template <class T>
class PropertyTemplate : public PropertyBase
{
public:
	PropertyTemplate (PropertyDescriptor<T> p, PropertyChange c, T const & v)
		: PropertyBase (p.id, c)
		, _current (v)
	{

	}
	PropertyTemplate<T> & operator= (PropertyTemplate<T> const & s) {
		/* XXX: isn't there a nicer place to do this? */
		_have_old = s._have_old;
		_property_id = s._property_id;
		_change = s._change;
		
		_current = s._current;
		_old = s._old;
		return *this;
	}

	T & operator= (T const & v) {
		set (v);
		return _current;
	}

	T & operator+= (T const & v) {
		set (_current + v);
		return _current;
	}
	
	bool operator== (const T& other) const {
		return _current == other;
	}

	bool operator!= (const T& other) const {
		return _current != other;
	}

	operator T const & () const {
		return _current;
	}

	T const & val () const {
		return _current;
	}

	void diff (XMLNode* old, XMLNode* current) const {
		if (_have_old) {
			old->add_property (g_quark_to_string (_property_id), to_string (_old));
			current->add_property (g_quark_to_string (_property_id), to_string (_current));
		}
	}
	
	void diff (PropertyChange& c) const {
		if (_have_old && _change) {
			c = PropertyChange (c | _change);
		}
	}

	/** Try to set state from the property of an XML node.
	 *  @param node XML node.
	 *  @return PropertyChange effected, or 0.
	 */
	PropertyChange set_state (XMLNode const & node) {
		XMLProperty const * p = node.property (g_quark_to_string (_property_id));
		PropertyChange c = PropertyChange (0);

		if (p) {
			T const v = from_string (p->value ());

			if (v != _current) {
				set (v);
				c = _change;
			}
		}

		return c;
	}

	void add_state (XMLNode & node) const {
		node.add_property (g_quark_to_string (_property_id), to_string (_current));
	}

protected:
	void set (T const & v) {
		_old = _current;
		_have_old = true;
		_current = v;
	}

	virtual std::string to_string (T const & v) const = 0;
	virtual T from_string (std::string const & s) const = 0;
		
	T _current;
	T _old;
};

template<class T>	
std::ostream& operator<< (std::ostream& os, PropertyTemplate<T> const & s)
{
	return os << s.val();
}

/** Representation of a single piece of state in a Stateful; for use
 *  with types that can be written to / read from stringstreams.
 */
template <class T>
class Property : public PropertyTemplate<T>
{
public:
	Property (PropertyDescriptor<T> q, PropertyChange c, T const & v)
		: PropertyTemplate<T> (q, c, v)
	{

	}

	Property (PropertyDescriptor<T> q, T const & v)
		: PropertyTemplate<T> (q, PropertyChange (0), v)
	{

	}
	
	T & operator= (T const & v) {
		this->set (v);
		return this->_current;
	}
	
private:	
        /* note that we do not set a locale for the streams used
	   in to_string() or from_string(), because we want the
	   format to be portable across locales (i.e. C or
	   POSIX). Also, there is the small matter of
	   std::locale aborting on OS X if used with anything
	   other than C or POSIX locales.
	*/
        std::string to_string (T const & v) const {
		std::stringstream s;
		s.precision (12); // in case its floating point
		s << v;
		return s.str ();
	}

	T from_string (std::string const & s) const {
		std::stringstream t (s);
		T v;
		t >> v;
		return v;
	}
};

class PropertyList : public std::map<PropertyID,PropertyBase*>
{
public:
    PropertyList() : property_owner (true) {}
    virtual ~PropertyList() {
	    if (property_owner)
		    for (std::map<PropertyID,PropertyBase*>::iterator i = begin(); i != end(); ++i) {
			    delete i->second;
		    }
    }
    /* classes that own property lists use this to add their
       property members to their plists.
    */
    bool add (PropertyBase& p) {
	    return insert (value_type (p.id(), &p)).second;
    }

    /* code that is constructing a property list for use 
       in setting the state of an object uses this.
    */
    template<typename T, typename V> bool add (PropertyDescriptor<T> pid, const V& v) {
	    return insert (value_type (pid.id, new Property<T> (pid, (T) v))).second;
    }

protected:
    bool property_owner;
};

/** A variant of PropertyList that does not delete its
    property list in its destructor. Objects with their
    own Properties store them in an OwnedPropertyList
    to avoid having them deleted at the wrong time.
*/

class OwnedPropertyList : public PropertyList
{
public:
    OwnedPropertyList() { property_owner = false; }
};
	    
} /* namespace PBD */

#endif /* __pbd_properties_h__ */