9ef1a9ef73761d79ede193de1ca57ceaebdd4a7e
[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 #include "pbd/id.h"
27 #include "pbd/xml++.h"
28 #include "pbd/enumwriter.h"
29 #include "pbd/properties.h"
30
31 class XMLNode;
32
33 namespace PBD {
34
35 namespace sys {
36         class path;
37 }
38
39 /** Base class for objects with saveable and undoable state */
40 class Stateful {
41   public:
42         Stateful ();
43         virtual ~Stateful();
44
45         virtual XMLNode& get_state (void) = 0;
46         virtual int set_state (const XMLNode&, int version) = 0;
47         /* derived types do not have to implement this, but probably should
48            give it serious attention.
49         */
50         virtual PropertyChange set_property (const PropertyBase&) { return PropertyChange (0); }
51
52         PropertyChange set_properties (const PropertyList&);
53
54         void add_property (PropertyBase& s) {
55                 _properties.add (s);
56         }
57
58         /* Extra XML node: so that 3rd parties can attach state to the XMLNode
59            representing the state of this object.
60          */
61
62         void add_extra_xml (XMLNode&);
63         XMLNode *extra_xml (const std::string& str);
64
65         const PBD::ID& id() const { return _id; }
66
67         void clear_history ();
68         std::pair<XMLNode *, XMLNode*> diff () const;
69         void changed (PropertyChange&) const;
70
71         static int current_state_version;
72         static int loading_state_version;
73
74   protected:
75
76         void add_instant_xml (XMLNode&, const sys::path& directory_path);
77         XMLNode *instant_xml (const std::string& str, const sys::path& directory_path);
78         void add_properties (XMLNode &);
79         /* derived types can call this from ::set_state() (or elsewhere)
80            to get basic property setting done.
81         */
82         PropertyChange set_properties (XMLNode const &);
83
84         
85         /* derived classes can implement this to do cross-checking
86            of property values after either a PropertyList or XML 
87            driven property change.
88         */
89         virtual void post_set () { };
90
91         XMLNode *_extra_xml;
92         XMLNode *_instant_xml;
93         PBD::ID _id;
94
95         std::string _xml_node_name; ///< name of node to use for this object in XML
96         OwnedPropertyList _properties;
97 };
98
99 } // namespace PBD
100
101 #endif /* __pbd_stateful_h__ */
102