the mega-properties/SequenceProperty patch. split is broken at present (right hand...
[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 namespace PBD {
29
30 class PropertyList;
31 typedef GQuark PropertyID;
32
33 template<typename T>
34 struct PropertyDescriptor {
35     PropertyDescriptor () : property_id (0) {}
36     PropertyDescriptor (PropertyID pid) : property_id (pid) {}
37
38     PropertyID property_id;
39     typedef T value_type;
40 };
41
42 class PropertyChange : public std::set<PropertyID>
43 {
44 public:
45         PropertyChange() {}
46
47         template<typename T> PropertyChange(PropertyDescriptor<T> p);
48
49         PropertyChange(const PropertyChange& other) : std::set<PropertyID> (other) {}
50
51         PropertyChange operator=(const PropertyChange& other) {
52                 clear ();
53                 insert (other.begin (), other.end ());
54                 return *this;
55         }
56
57         template<typename T> PropertyChange operator=(PropertyDescriptor<T> p);
58         template<typename T> bool contains (PropertyDescriptor<T> p) const;
59
60         bool contains (const PropertyChange& other) const {
61                 for (const_iterator x = other.begin (); x != other.end (); ++x) {
62                         if (find (*x) != end ()) {
63                                 return true;
64                         }
65                 }
66                 return false;
67         }
68
69         void add (PropertyID id)               { insert (id); }
70         void add (const PropertyChange& other) { insert (other.begin (), other.end ()); }
71         template<typename T> void add (PropertyDescriptor<T> p);
72 };
73
74 /** Base (non template) part of Property */
75 class PropertyBase
76 {
77 public:
78         PropertyBase (PropertyID pid)
79                 : _property_id (pid)
80                 , _have_old (false)
81         {}
82
83         /** Forget about any old value for this state */
84         virtual void clear_history () {
85                 _have_old = false;
86         }
87
88         virtual void add_history_state (XMLNode*)        const = 0;
89         virtual void diff (PropertyList&, PropertyList&) const = 0;
90         
91         virtual PropertyBase* maybe_clone_self_if_found_in_history_node (const XMLNode&) const { return 0; }
92
93         virtual bool set_state_from_owner_state (XMLNode const&) = 0;
94         virtual void add_state_to_owner_state (XMLNode&) const   = 0;
95
96         const gchar*property_name () const { return g_quark_to_string (_property_id); }
97         PropertyID  property_id () const   { return _property_id; }
98
99         bool operator==(PropertyID pid) const {
100                 return _property_id == pid;
101         }
102
103 protected:
104         PropertyID _property_id;
105         bool       _have_old;
106 };
107
108 class PropertyFactory 
109 {
110   public:
111         static PropertyBase* create (const XMLNode&);
112 };
113
114 }
115
116 #endif /* __libpbd_property_basics_h__ */