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 __libpbd_property_basics_h__
21 #define __libpbd_property_basics_h__
27 #include "pbd/xml++.h"
34 class StatefulDiffCommand;
36 /** A unique identifier for a property of a Stateful object */
37 typedef GQuark PropertyID;
40 struct PropertyDescriptor {
41 PropertyDescriptor () : property_id (0) {}
42 PropertyDescriptor (PropertyID pid) : property_id (pid) {}
44 PropertyID property_id;
48 /** A list of IDs of Properties that have changed in some situation or other */
49 class PropertyChange : public std::set<PropertyID>
54 template<typename T> PropertyChange(PropertyDescriptor<T> p);
56 PropertyChange(const PropertyChange& other) : std::set<PropertyID> (other) {}
58 PropertyChange operator=(const PropertyChange& other) {
60 insert (other.begin (), other.end ());
64 template<typename T> PropertyChange operator=(PropertyDescriptor<T> p);
65 template<typename T> bool contains (PropertyDescriptor<T> p) const;
67 bool contains (const PropertyChange& other) const {
68 for (const_iterator x = other.begin (); x != other.end (); ++x) {
69 if (find (*x) != end ()) {
76 void add (PropertyID id) { insert (id); }
77 void add (const PropertyChange& other) { insert (other.begin (), other.end ()); }
78 template<typename T> void add (PropertyDescriptor<T> p);
81 /** Base (non template) part of Property */
85 PropertyBase (PropertyID pid)
89 virtual ~PropertyBase () {}
91 /** Forget about any old value for this state */
92 virtual void clear_history () = 0;
94 /** Tell any things we own to forget about their old values */
95 virtual void clear_owned_history () {}
97 /** Get any change in this property as XML and add it to a node */
98 virtual void get_change (XMLNode *) const = 0;
100 /** Add information to two property lists: one that allows
101 * undo of the changes in this property's state betwen now and
102 * the last call to clear_history, and one that allows redo
105 virtual void diff (PropertyList& undo, PropertyList& redo, Command*) const = 0;
107 /** Collect StatefulDiffCommands for changes to anything that we own */
108 virtual void rdiff (std::vector<StatefulDiffCommand*> &) const {}
110 virtual PropertyBase* maybe_clone_self_if_found_in_history_node (const XMLNode&) const { return 0; }
112 /** Set our value from an XML node.
113 * @return true if the value was set.
115 virtual bool set_value (XMLNode const &) = 0;
117 /** Get our value and put it into an XML node */
118 virtual void get_value (XMLNode& node) const = 0;
120 /** @return true if this property has changed in value since construction or since
121 * the last call to clear_history(), whichever was more recent.
123 virtual bool changed() const = 0;
125 /** Apply a change contained in another Property to this one */
126 virtual void apply_change (PropertyBase const *) = 0;
128 const gchar*property_name () const { return g_quark_to_string (_property_id); }
129 PropertyID property_id () const { return _property_id; }
131 bool operator==(PropertyID pid) const {
132 return _property_id == pid;
136 PropertyID _property_id;
141 #endif /* __libpbd_property_basics_h__ */