2 Copyright (C) 2000-2010 Paul Davis
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.
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.
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.
20 #ifndef __pbd_stateful_h__
21 #define __pbd_stateful_h__
28 #include "pbd/xml++.h"
29 #include "pbd/property_basics.h"
30 #include "pbd/signals.h"
41 class OwnedPropertyList;
43 /** Base class for objects with saveable and undoable state */
49 virtual XMLNode& get_state (void) = 0;
50 virtual int set_state (const XMLNode&, int version) = 0;
52 virtual bool apply_changes (PropertyBase const &);
53 PropertyChange apply_changes (PropertyList const &);
55 const OwnedPropertyList& properties() const { return *_properties; }
57 void add_property (PropertyBase& s);
59 /* Extra XML node: so that 3rd parties can attach state to the XMLNode
60 representing the state of this object.
63 void add_extra_xml (XMLNode&);
64 XMLNode *extra_xml (const std::string& str);
66 const PBD::ID& id() const { return _id; }
68 /* history management */
70 void clear_changes ();
71 virtual void clear_owned_changes ();
72 PropertyList* get_changes_as_properties (Command *) const;
73 virtual void rdiff (std::vector<Command*> &) const;
76 /* create a property list from an XMLNode
78 virtual PropertyList* property_factory (const XMLNode&) const;
80 /* How stateful's notify of changes to their properties
82 PBD::Signal1<void,const PropertyChange&> PropertyChanged;
84 static int current_state_version;
85 static int loading_state_version;
87 virtual void suspend_property_changes ();
88 virtual void resume_property_changes ();
90 virtual bool frozen() const { return _frozen; }
94 void add_instant_xml (XMLNode&, const sys::path& directory_path);
95 XMLNode *instant_xml (const std::string& str, const sys::path& directory_path);
96 void add_properties (XMLNode &);
98 PropertyChange set_values (XMLNode const &);
100 /* derived classes can implement this to do cross-checking
101 of property values after either a PropertyList or XML
102 driven property change.
104 virtual void post_set () { };
107 XMLNode *_instant_xml;
110 PBD::PropertyChange _pending_changed;
113 std::string _xml_node_name; ///< name of node to use for this object in XML
114 OwnedPropertyList* _properties;
116 virtual void send_change (const PropertyChange&);
117 /** derived classes can implement this in order to process a property change
118 within thaw() just before send_change() is called.
120 virtual void mid_thaw (const PropertyChange&) { }
121 bool property_changes_suspended() const { return g_atomic_int_get (&_frozen) > 0; }
126 #endif /* __pbd_stateful_h__ */