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