resolve merge with master (?)
[ardour.git] / libs / pbd / pbd / stateful.h
1 /*
2     Copyright (C) 2000-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 __pbd_stateful_h__
21 #define __pbd_stateful_h__
22
23 #include <string>
24 #include <list>
25 #include <cassert>
26
27 #include "pbd/id.h"
28 #include "pbd/xml++.h"
29 #include "pbd/property_basics.h"
30 #include "pbd/signals.h"
31
32 class XMLNode;
33
34 namespace PBD {
35
36 namespace sys {
37         class path;
38 }
39
40 class PropertyList;
41 class OwnedPropertyList;
42
43 /** Base class for objects with saveable and undoable state */
44 class Stateful {
45   public:
46         Stateful ();
47         virtual ~Stateful();
48
49         virtual XMLNode& get_state (void) = 0;
50         virtual int set_state (const XMLNode&, int version) = 0;
51
52         virtual bool apply_changes (PropertyBase const &);
53         PropertyChange apply_changes (PropertyList const &);
54         
55         const OwnedPropertyList& properties() const { return *_properties; }
56
57         void add_property (PropertyBase& s);
58
59         /* Extra XML node: so that 3rd parties can attach state to the XMLNode
60            representing the state of this object.
61          */
62
63         void add_extra_xml (XMLNode&);
64         XMLNode *extra_xml (const std::string& str, bool add_if_missing = false);
65         void save_extra_xml (const XMLNode&);
66
67         const PBD::ID& id() const { return _id; }
68         bool set_id (const XMLNode&);
69         void set_id (const std::string&);
70         void reset_id ();
71
72         /* history management */
73
74         void clear_changes ();
75         virtual void clear_owned_changes ();
76         PropertyList* get_changes_as_properties (Command *) const;
77         virtual void rdiff (std::vector<Command*> &) const;
78         bool changed() const;
79
80         /* create a property list from an XMLNode
81          */
82         virtual PropertyList* property_factory (const XMLNode&) const;
83
84         /* How stateful's notify of changes to their properties
85          */
86         PBD::Signal1<void,const PropertyChange&> PropertyChanged;
87
88         static int current_state_version;
89         static int loading_state_version;
90
91         virtual void suspend_property_changes ();
92         virtual void resume_property_changes ();
93
94         bool property_changes_suspended() const { return g_atomic_int_get (const_cast<gint*>(&_stateful_frozen)) > 0; }
95         
96   protected:
97
98         void add_instant_xml (XMLNode&, const std::string& directory_path);
99         XMLNode *instant_xml (const std::string& str, const std::string& directory_path);
100         void add_properties (XMLNode &);
101
102         PropertyChange set_values (XMLNode const &);
103         
104         /* derived classes can implement this to do cross-checking
105            of property values after either a PropertyList or XML 
106            driven property change.
107         */
108         virtual void post_set (const PropertyChange&) { };
109
110         XMLNode *_extra_xml;
111         XMLNode *_instant_xml;
112         PBD::PropertyChange     _pending_changed;
113         Glib::Threads::Mutex _lock;
114
115         std::string _xml_node_name; ///< name of node to use for this object in XML
116         OwnedPropertyList* _properties;
117
118         virtual void send_change (const PropertyChange&);
119         /** derived classes can implement this in order to process a property change
120             within thaw() just before send_change() is called.
121         */
122         virtual void mid_thaw (const PropertyChange&) { }
123
124   private:
125         PBD::ID  _id;
126         gint     _stateful_frozen;
127 };
128
129 } // namespace PBD
130
131 #endif /* __pbd_stateful_h__ */
132