const correctness.
[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
26 #include "pbd/xml++.h"
27
28 class Command;
29
30 namespace PBD {
31
32 class PropertyList;
33
34 /** A unique identifier for a property of a Stateful object */
35 typedef GQuark PropertyID;
36
37 template<typename T>
38 struct PropertyDescriptor {
39     PropertyDescriptor () : property_id (0) {}
40     PropertyDescriptor (PropertyID pid) : property_id (pid) {}
41
42     PropertyID property_id;
43     typedef T value_type;
44 };
45
46 /** A list of IDs of Properties that have changed in some situation or other */
47 class PropertyChange : public std::set<PropertyID>
48 {
49 public:
50         PropertyChange() {}
51
52         template<typename T> PropertyChange(PropertyDescriptor<T> p);
53
54         PropertyChange(const PropertyChange& other) : std::set<PropertyID> (other) {}
55
56         PropertyChange operator=(const PropertyChange& other) {
57                 clear ();
58                 insert (other.begin (), other.end ());
59                 return *this;
60         }
61
62         template<typename T> PropertyChange operator=(PropertyDescriptor<T> p);
63         template<typename T> bool contains (PropertyDescriptor<T> p) const;
64
65         bool contains (const PropertyChange& other) const {
66                 for (const_iterator x = other.begin (); x != other.end (); ++x) {
67                         if (find (*x) != end ()) {
68                                 return true;
69                         }
70                 }
71                 return false;
72         }
73
74         void add (PropertyID id)               { insert (id); }
75         void add (const PropertyChange& other) { insert (other.begin (), other.end ()); }
76         template<typename T> void add (PropertyDescriptor<T> p);
77 };
78
79 /** Base (non template) part of Property */
80 class PropertyBase
81 {
82 public:
83         PropertyBase (PropertyID pid)
84                 : _property_id (pid)
85         {}
86
87         virtual ~PropertyBase () {}
88         
89         /** Forget about any old value for this state */
90         virtual void clear_history () = 0;
91
92         /** Make XML that allows us to get from some previous state to the current state
93          *  of this property, and add it to @param history_node
94          */
95         virtual void add_history_state (XMLNode* history_node) const = 0;
96
97         /** Add information to two property lists: one that allows
98          *  undo of the changes in this property's state betwen now and
99          *  the last call to clear_history, and one that allows redo
100          *  of those changes.
101          */
102         virtual void diff (PropertyList& undo, PropertyList& redo, Command*) const = 0;
103         
104         virtual PropertyBase* maybe_clone_self_if_found_in_history_node (const XMLNode&) const { return 0; }
105
106         /** Set state from an XML node previously generated by add_history_state */
107         virtual bool set_state_from_owner_state (XMLNode const&) = 0;
108
109         /** Add complete current state in XML form to an existing XML node @param node */
110         virtual void add_state_to_owner_state (XMLNode& node) const = 0;
111
112         /** @return true if this property has changed in value since construction or since
113          *  the last call to clear_history(), whichever was more recent.
114          */
115         virtual bool changed() const = 0;
116
117         /** Set the value of this property from another */
118         virtual void set_state_from_property (PropertyBase const *) = 0;
119
120         const gchar*property_name () const { return g_quark_to_string (_property_id); }
121         PropertyID  property_id () const   { return _property_id; }
122
123         bool operator==(PropertyID pid) const {
124                 return _property_id == pid;
125         }
126
127 protected:
128         PropertyID _property_id;
129 };
130
131 }
132
133 #endif /* __libpbd_property_basics_h__ */