Ardour  8.12
stateful.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000-2015 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006 Hans Fugal <hans@fugal.net>
4  * Copyright (C) 2007-2015 Tim Mayberry <mojofunk@gmail.com>
5  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef __pbd_stateful_h__
24 #define __pbd_stateful_h__
25 
26 #include <atomic>
27 #include <string>
28 #include <list>
29 #include <cassert>
30 
31 #include "pbd/libpbd_visibility.h"
32 #include "pbd/id.h"
33 #include "pbd/xml++.h"
34 #include "pbd/property_basics.h"
35 #include "pbd/signals.h"
36 
37 class XMLNode;
38 
39 namespace PBD {
40 
41 namespace sys {
42  class path;
43 }
44 
45 class PropertyList;
46 class OwnedPropertyList;
47 
50  public:
52  virtual ~Stateful();
53 
54  virtual XMLNode& get_state () const = 0;
55  virtual int set_state (const XMLNode&, int version) = 0;
56 
57  virtual bool apply_change (PropertyBase const &);
59 
60  const OwnedPropertyList& properties() const { return *_properties; }
61 
63 
64  /* Extra XML node: so that 3rd parties can attach state to the XMLNode
65  representing the state of this object.
66  */
67 
69  XMLNode *extra_xml (const std::string& str, bool add_if_missing = false);
70  void save_extra_xml (const XMLNode&);
71 
72  const PBD::ID& id() const { return _id; }
73  bool set_id (const XMLNode&);
74  void set_id (const std::string&);
75  void reset_id ();
76 
77  /* RAII structure to manage thread-local ID regeneration.
78  */
81  set_regenerate_xml_and_string_ids_in_this_thread (true);
82  }
84  set_regenerate_xml_and_string_ids_in_this_thread (false);
85  }
86  };
87 
88  /* history management */
89 
90  void clear_changes ();
91  virtual void clear_owned_changes ();
93  virtual void rdiff (std::vector<PBD::Command*> &) const;
94  bool changed() const;
95 
96  /* create a property list from an XMLNode */
97  virtual PropertyList* property_factory (const XMLNode&) const;
98 
99  /* How stateful's notify of changes to their properties */
100  PBD::Signal1<void,const PropertyChange&> PropertyChanged;
101 
104 
105  virtual void suspend_property_changes ();
106  virtual void resume_property_changes ();
107 
108  bool property_changes_suspended() const { return _stateful_frozen.load() > 0; }
109 
110  protected:
111 
112  void add_instant_xml (XMLNode&, const std::string& directory_path);
113  XMLNode *instant_xml (const std::string& str, const std::string& directory_path);
114  void add_properties (XMLNode &) const;
115 
117 
118  /* derived classes can implement this to do cross-checking
119  of property values after either a PropertyList or XML
120  driven property change.
121  */
122  virtual void post_set (const PropertyChange&) { };
123 
127  Glib::Threads::Mutex _lock;
128 
129  std::string _xml_node_name;
131 
132  virtual void send_change (const PropertyChange&);
136  virtual void mid_thaw (const PropertyChange&) { }
137 
139 
140  private:
141  friend struct ForceIDRegeneration;
142  static Glib::Threads::Private<bool> _regenerate_xml_or_string_ids;
143 
145  std::atomic<int> _stateful_frozen;
146 
148 };
149 
150 } // namespace PBD
151 
152 #endif /* __pbd_stateful_h__ */
Definition: id.h:35
static void set_regenerate_xml_and_string_ids_in_this_thread(bool yn)
void add_property(PropertyBase &s)
const PBD::ID & id() const
Definition: stateful.h:72
bool set_id(const XMLNode &)
virtual PropertyList * property_factory(const XMLNode &) const
std::atomic< int > _stateful_frozen
Definition: stateful.h:145
virtual XMLNode & get_state() const =0
PropertyChange set_values(XMLNode const &)
PropertyChange apply_changes(PropertyList const &)
virtual void send_change(const PropertyChange &)
virtual ~Stateful()
void clear_changes()
virtual void suspend_property_changes()
virtual void resume_property_changes()
virtual int set_state(const XMLNode &, int version)=0
const OwnedPropertyList & properties() const
Definition: stateful.h:60
PBD::Signal1< void, const PropertyChange & > PropertyChanged
Definition: stateful.h:100
static Glib::Threads::Private< bool > _regenerate_xml_or_string_ids
Definition: stateful.h:142
XMLNode * _instant_xml
Definition: stateful.h:125
static int loading_state_version
Definition: stateful.h:103
virtual bool apply_change(PropertyBase const &)
bool regenerate_xml_or_string_ids() const
XMLNode * _extra_xml
Definition: stateful.h:122
std::string _xml_node_name
name of node to use for this object in XML
Definition: stateful.h:129
virtual void post_set(const PropertyChange &)
Definition: stateful.h:122
void save_extra_xml(const XMLNode &)
PropertyList * get_changes_as_properties(PBD::Command *) const
void add_instant_xml(XMLNode &, const std::string &directory_path)
virtual void rdiff(std::vector< PBD::Command * > &) const
bool property_changes_suspended() const
Definition: stateful.h:108
virtual void clear_owned_changes()
bool changed() const
virtual void mid_thaw(const PropertyChange &)
Definition: stateful.h:136
void set_id(const std::string &)
XMLNode * instant_xml(const std::string &str, const std::string &directory_path)
PBD::PropertyChange _pending_changed
Definition: stateful.h:126
void add_extra_xml(XMLNode &)
static int current_state_version
Definition: stateful.h:102
void add_properties(XMLNode &) const
XMLNode * extra_xml(const std::string &str, bool add_if_missing=false)
PBD::ID _id
Definition: stateful.h:144
OwnedPropertyList * _properties
Definition: stateful.h:130
Glib::Threads::Mutex _lock
Definition: stateful.h:127
Definition: xml++.h:114
#define LIBPBD_API
Definition: axis_view.h:42