tweak transport bar spacing
[ardour.git] / libs / pbd / pbd / property_basics.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 __libpbd_property_basics_h__
21 #define __libpbd_property_basics_h__
22
23 #include <glib.h>
24 #include <set>
25 #include <vector>
26
27 #include "pbd/xml++.h"
28
29 class Command;
30
31 namespace PBD {
32
33 class PropertyList;
34 class StatefulDiffCommand;      
35
36 /** A unique identifier for a property of a Stateful object */
37 typedef GQuark PropertyID;
38
39 template<typename T>
40 struct PropertyDescriptor {
41         PropertyDescriptor () : property_id (0) {}
42         PropertyDescriptor (PropertyID pid) : property_id (pid) {}
43         
44         PropertyID property_id;
45         typedef T value_type;
46 };
47
48 /** A list of IDs of Properties that have changed in some situation or other */
49 class PropertyChange : public std::set<PropertyID>
50 {
51 public:
52         PropertyChange() {}
53
54         template<typename T> PropertyChange(PropertyDescriptor<T> p);
55
56         PropertyChange(const PropertyChange& other) : std::set<PropertyID> (other) {}
57
58         PropertyChange operator=(const PropertyChange& other) {
59                 clear ();
60                 insert (other.begin (), other.end ());
61                 return *this;
62         }
63
64         template<typename T> PropertyChange operator=(PropertyDescriptor<T> p);
65         template<typename T> bool contains (PropertyDescriptor<T> p) const;
66
67         bool contains (const PropertyChange& other) const {
68                 for (const_iterator x = other.begin (); x != other.end (); ++x) {
69                         if (find (*x) != end ()) {
70                                 return true;
71                         }
72                 }
73                 return false;
74         }
75
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);
79 };
80
81 /** Base (non template) part of Property
82  *  Properties are used for two main reasons:
83  *    - to handle current state (when serializing Stateful objects)
84  *    - to handle history since some operation was started (when making StatefulDiffCommands for undo)
85  */
86 class PropertyBase
87 {
88 public:
89         PropertyBase (PropertyID pid)
90                 : _property_id (pid)
91         {}
92
93         virtual ~PropertyBase () {}
94
95
96         /* MANAGEMENT OF Stateful STATE */
97
98         /** Set the value of this property from a Stateful node.
99          *  @return true if the value was set.
100          */
101         virtual bool set_value (XMLNode const &) = 0;
102
103         /** Get this property's value and put it into a Stateful node */
104         virtual void get_value (XMLNode& node) const = 0;
105
106
107         /* MANAGEMENT OF HISTORY */
108
109         /** Forget about any old changes to this property's value */
110         virtual void clear_changes () = 0;
111
112         /** Tell any things we own to forget about their old values */
113         virtual void clear_owned_changes () {}
114
115         /** @return true if this property has changed in value since construction or since
116          *  the last call to clear_changes (), whichever was more recent.
117          */
118         virtual bool changed () const = 0;
119
120         /** Invert the changes in this property */
121         virtual void invert () = 0;
122         
123
124         /* TRANSFERRING HISTORY TO / FROM A StatefulDiffCommand */
125
126         /** Get any changes in this property as XML and add them to a
127          *  StatefulDiffCommand node.
128          */
129         virtual void get_changes_as_xml (XMLNode *) const = 0;
130         
131         /** If this Property has changed, clone it and add it to a given list.
132          *  Used for making StatefulDiffCommands.
133          */
134         virtual void get_changes_as_properties (PropertyList& changes, Command *) const = 0;
135
136         /** Collect StatefulDiffCommands for changes to anything that we own */
137         virtual void rdiff (std::vector<Command*> &) const {}
138
139         /** Look in an XML node written by get_changes_as_xml and, if XML from this property
140          *  is found, create a property with the changes from the XML.
141          */
142         virtual PropertyBase* clone_from_xml (const XMLNode &) const { return 0; }
143
144
145         /* VARIOUS */
146         
147         virtual PropertyBase* clone () const = 0;
148
149         /** Set this property's current state from another */
150         virtual void apply_changes (PropertyBase const *) = 0;
151
152         const gchar* property_name () const { return g_quark_to_string (_property_id); }
153         PropertyID   property_id () const   { return _property_id; }
154
155         bool operator==(PropertyID pid) const {
156                 return _property_id == pid;
157         }
158
159 protected:      
160         /* copy construction only by subclasses */
161         PropertyBase (PropertyBase const & b)
162                 : _property_id (b._property_id)
163         {}
164         
165 private:
166         PropertyID _property_id;
167
168 };
169
170 }
171
172 #endif /* __libpbd_property_basics_h__ */