summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/properties.h
blob: c97b4722aee6b6110b3539c7ae8357d312d88313 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
    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 <set>
#include <iostream>

#include "pbd/libpbd_visibility.h"
#include "pbd/xml++.h"
#include "pbd/property_basics.h"
#include "pbd/property_list.h"
#include "pbd/enumwriter.h"
#include "pbd/stateful.h"

namespace PBD {

/** Parent class for classes which represent a single scalar property in a Stateful object */
template<class T>
class /*LIBPBD_API*/ PropertyTemplate : public PropertyBase
{
public:
	PropertyTemplate (PropertyDescriptor<T> p, T const& v)
		: PropertyBase (p.property_id)
		, _have_old (false)
		, _current (v)
	{}

	PropertyTemplate (PropertyDescriptor<T> p, T const& o, T const& c)
		: PropertyBase (p.property_id)
		, _have_old (true)
		, _current (c)
		, _old (o)
	{}

	PropertyTemplate (PropertyDescriptor<T> p, PropertyTemplate<T> const & s)
		: PropertyBase (p.property_id)
		, _have_old (false)
		, _current (s._current)
	{}


	/* OPERATORS / ACCESSORS */

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

	/* This will mean that, if fred and jim are both PropertyTemplates,
	 * fred = jim will result in fred taking on jim's current value,
	 * but NOT jim's property ID.
	 */
	PropertyTemplate<T> & operator= (PropertyTemplate<T> const & p) {
		set (p._current);
		return *this;
	}

	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;
	}


	/* MANAGEMENT OF Stateful State */

	bool set_value (XMLNode const & node) {

		XMLProperty const* p = node.property (property_name());

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

			if (v != _current) {
				set (v);
				return true;
			}
		}

		return false;
	}

	void get_value (XMLNode & node) const {
                node.add_property (property_name(), to_string (_current));
	}


	/* MANAGEMENT OF HISTORY */

	void clear_changes () {
		_have_old = false;
	}

	bool changed () const { return _have_old; }

	void invert () {
		T const tmp = _current;
		_current = _old;
		_old = tmp;
	}


	/* TRANSFERRING HISTORY TO / FROM A StatefulDiffCommand */

	void get_changes_as_xml (XMLNode* history_node) const {
		XMLNode* node = history_node->add_child (property_name());
                node->add_property ("from", to_string (_old));
                node->add_property ("to", to_string (_current));
	}

	void get_changes_as_properties (PropertyList& changes, Command *) const {
		if (this->_have_old) {
			changes.add (clone ());
		}
	}


	/* VARIOUS */

	void apply_changes (PropertyBase const * p) {
		T v = dynamic_cast<const PropertyTemplate<T>* > (p)->val ();
		if (v != _current) {
			set (v);
		}
	}

protected:

	void set (T const& v) {
		if (v != _current) {
			if (!_have_old) {
				_old = _current;
				_have_old = true;
			} else {
				if (v == _old) {
					/* value has been reset to the value
					   at the start of a history transaction,
					   before clear_changes() is called.
					   thus there is effectively no apparent
					   history for this property.
					*/
					_have_old = false;
				}
			}

			_current  = v;
		}
	}

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

	bool _have_old;
	T _current;
	T _old;

private:
	/* disallow copy-construction; it's not obvious whether it should mean
	   a copy of just the value, or the value and property ID.
	*/
	PropertyTemplate (PropertyTemplate<T> const &);
};

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

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

	Property (PropertyDescriptor<T> q, T const& o, T const& c)
		: PropertyTemplate<T> (q, o, c)
	{}

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

	Property<T>* clone () const {
		return new Property<T> (this->property_id(), this->_old, this->_current);
	}

	Property<T>* clone_from_xml (const XMLNode& node) const {
		XMLNodeList const & children = node.children ();
		XMLNodeList::const_iterator i = children.begin();
		while (i != children.end() && (*i)->name() != this->property_name()) {
			++i;
		}

		if (i == children.end()) {
			return 0;
		}
		XMLProperty const * from = (*i)->property ("from");
		XMLProperty const * to = (*i)->property ("to");

		if (!from || !to) {
			return 0;
		}

		return new Property<T> (this->property_id(), from_string (from->value()), from_string (to->value ()));
	}

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

private:
        friend class PropertyFactory;

	/* no copy-construction */
	Property (Property<T> const &);

	/* 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.
	 */
	virtual std::string to_string (T const& v) const {
		std::stringstream s;
		s.precision (12); // in case its floating point
		s << v;
		return s.str ();
	}

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

};

/** Specialization, for std::string which is common and special (see to_string() and from_string()
 *  Using stringstream to read from a std::string is easy to get wrong because of whitespace
 *  separators, etc.
 */
template<>
class /*LIBPBD_API*/ Property<std::string> : public PropertyTemplate<std::string>
{
public:
	Property (PropertyDescriptor<std::string> d, std::string const & v)
		: PropertyTemplate<std::string> (d, v)
	{}

	Property (PropertyDescriptor<std::string> d, std::string const & o, std::string const & c)
		: PropertyTemplate<std::string> (d, o, c)
	{}

	Property<std::string>* clone () const {
		return new Property<std::string> (this->property_id(), _old, _current);
	}

	std::string & operator= (std::string const& v) {
		this->set (v);
		return this->_current;
	}

private:
	std::string to_string (std::string const& v) const {
		return v;
	}

	std::string from_string (std::string const& s) const {
		return s;
	}

	/* no copy-construction */
	Property (Property<std::string> const &);
};

template<class T>
class /*LIBPBD_API*/ EnumProperty : public Property<T>
{
public:
	EnumProperty (PropertyDescriptor<T> q, T const& v)
		: Property<T> (q, v)
	{}

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

private:
	std::string to_string (T const & v) const {
		return enum_2_string (v);
	}

	T from_string (std::string const & s) const {
		return static_cast<T> (string_2_enum (s, this->_current));
	}

	/* no copy-construction */
	EnumProperty (EnumProperty const &);
};

/** A Property which holds a shared_ptr to a Stateful object,
 *  and handles undo using the somewhat inefficient approach
 *  of saving the complete XML state of its object before and
 *  after changes.  A sort of half-way house between the old
 *  complete-state undo system and the new difference-based
 *  one.
 */
template <class T>
class /*LIBPBD_API*/ SharedStatefulProperty : public PropertyBase
{
public:
	typedef boost::shared_ptr<T> Ptr;

	SharedStatefulProperty (PropertyID d, Ptr p)
		: PropertyBase (d)
		, _current (p)
	{

	}

	SharedStatefulProperty (PropertyID d, Ptr o, Ptr c)
		: PropertyBase (d)
		, _old (o)
		, _current (c)
	{

	}

	bool set_value (XMLNode const & node) {

		/* Look for our node */
		XMLNode* n = node.child (property_name ());
		if (!n) {
			return false;
		}

		/* And there should be one child which is the state of our T */
		XMLNodeList const & children = n->children ();
		if (children.size() != 1) {
			return false;
		}

		_current->set_state (*children.front (), Stateful::current_state_version);
		return true;
	}

	void get_value (XMLNode & node) const {
		XMLNode* n = node.add_child (property_name ());
		n->add_child_nocopy (_current->get_state ());
	}

	void clear_changes () {
		/* We are starting to change things, so _old gets set up
		   with the current state.
		*/
		_old.reset (new T (*_current.get()));
	}

	bool changed () const {
		/* Expensive, but, hey; this requires operator!= in
		   our T
		*/
		return (*_old != *_current);
	}

	void invert () {
		_current.swap (_old);
	}

	void get_changes_as_xml (XMLNode* history_node) const {
		/* We express the diff as before and after state, just
		   as MementoCommand does.
		*/
		XMLNode* p = history_node->add_child (property_name ());
		XMLNode* from = p->add_child ("from");
		from->add_child_nocopy (_old->get_state ());
		XMLNode* to = p->add_child ("to");
		to->add_child_nocopy (_current->get_state ());
	}

	void get_changes_as_properties (PropertyList& changes, Command *) const {
		if (changed ()) {
			changes.add (clone ());
		}
	}

	void apply_changes (PropertyBase const * p) {
		*_current = *(dynamic_cast<SharedStatefulProperty const *> (p))->val ();
	}

	Ptr val () const {
		return _current;
	}

	T* operator-> () const {
		return _current.operator-> ();
	}

	operator bool () const {
		return _current ? true : false;
	}

protected:

	Ptr _old;
	Ptr _current;

private:

	/* No copy-construction nor assignment */
	SharedStatefulProperty (SharedStatefulProperty<T> const &);
	SharedStatefulProperty<T>& operator= (SharedStatefulProperty<T> const &);
};

} /* namespace PBD */

#include "pbd/property_list_impl.h"
#include "pbd/property_basics_impl.h"

#endif /* __pbd_properties_h__ */