Ardour  9.0-pre0-582-g084a23a80d
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 #pragma once
24 
25 #include <atomic>
26 #include <string>
27 #include <list>
28 #include <cassert>
29 
30 #include "pbd/libpbd_visibility.h"
31 #include "pbd/id.h"
32 #include "pbd/xml++.h"
33 #include "pbd/property_basics.h"
34 #include "pbd/signals.h"
35 
36 class XMLNode;
37 
38 namespace PBD {
39 
40 namespace sys {
41  class path;
42 }
43 
44 class PropertyList;
45 class OwnedPropertyList;
46 
49  public:
51  virtual ~Stateful();
52 
53  virtual XMLNode& get_state () const = 0;
54  virtual int set_state (const XMLNode&, int version) = 0;
55 
56  virtual bool apply_change (PropertyBase const &);
58 
59  const OwnedPropertyList& properties() const { return *_properties; }
60 
62 
63  /* Extra XML node: so that 3rd parties can attach state to the XMLNode
64  representing the state of this object.
65  */
66 
68  XMLNode *extra_xml (const std::string& str, bool add_if_missing = false);
69  void save_extra_xml (const XMLNode&);
70 
71  const PBD::ID& id() const { return _id; }
72  bool set_id (const XMLNode&);
73  void set_id (const std::string&);
74  void reset_id ();
75 
76  /* RAII structure to manage thread-local ID regeneration.
77  */
80  set_regenerate_xml_and_string_ids_in_this_thread (true);
81  }
83  set_regenerate_xml_and_string_ids_in_this_thread (false);
84  }
85  };
86 
87  /* history management */
88 
89  void clear_changes ();
90  virtual void clear_owned_changes ();
92  virtual void rdiff (std::vector<PBD::Command*> &) const;
93  bool changed() const;
94 
95  /* create a property list from an XMLNode */
96  virtual PropertyList* property_factory (const XMLNode&) const;
97 
98  /* How stateful's notify of changes to their properties */
100 
103 
104  virtual void suspend_property_changes ();
105  virtual void resume_property_changes ();
106 
107  bool property_changes_suspended() const { return _stateful_frozen.load() > 0; }
108 
109  protected:
110 
111  void add_instant_xml (XMLNode&, const std::string& directory_path);
112  XMLNode *instant_xml (const std::string& str, const std::string& directory_path);
113  void add_properties (XMLNode &) const;
114 
116 
117  /* derived classes can implement this to do cross-checking
118  of property values after either a PropertyList or XML
119  driven property change.
120  */
121  virtual void post_set (const PropertyChange&) { };
122 
126  Glib::Threads::Mutex _lock;
127 
128  std::string _xml_node_name;
130 
131  virtual void send_change (const PropertyChange&);
135  virtual void mid_thaw (const PropertyChange&) { }
136 
138 
139  private:
140  friend struct ForceIDRegeneration;
141  static Glib::Threads::Private<bool> _regenerate_xml_or_string_ids;
142 
144  std::atomic<int> _stateful_frozen;
145 
147 };
148 
149 } // namespace PBD
150 
Definition: id.h:34
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:71
bool set_id(const XMLNode &)
virtual PropertyList * property_factory(const XMLNode &) const
std::atomic< int > _stateful_frozen
Definition: stateful.h:144
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:59
PBD::Signal< void(const PropertyChange &)> PropertyChanged
Definition: stateful.h:99
static Glib::Threads::Private< bool > _regenerate_xml_or_string_ids
Definition: stateful.h:141
XMLNode * _instant_xml
Definition: stateful.h:124
static int loading_state_version
Definition: stateful.h:102
virtual bool apply_change(PropertyBase const &)
bool regenerate_xml_or_string_ids() const
XMLNode * _extra_xml
Definition: stateful.h:121
std::string _xml_node_name
name of node to use for this object in XML
Definition: stateful.h:128
virtual void post_set(const PropertyChange &)
Definition: stateful.h:121
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:107
virtual void clear_owned_changes()
bool changed() const
virtual void mid_thaw(const PropertyChange &)
Definition: stateful.h:135
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:125
void add_extra_xml(XMLNode &)
static int current_state_version
Definition: stateful.h:101
void add_properties(XMLNode &) const
XMLNode * extra_xml(const std::string &str, bool add_if_missing=false)
PBD::ID _id
Definition: stateful.h:143
OwnedPropertyList * _properties
Definition: stateful.h:129
Glib::Threads::Mutex _lock
Definition: stateful.h:126
Definition: xml++.h:114
#define LIBPBD_API
Definition: axis_view.h:42