diff options
author | Carl Hetherington <carl@carlh.net> | 2010-02-10 01:38:20 +0000 |
---|---|---|
committer | Carl Hetherington <carl@carlh.net> | 2010-02-10 01:38:20 +0000 |
commit | 3c00a7ca2ae34cb65c8d3394d9a012f20c69ee77 (patch) | |
tree | 54257f7655152fab7fbe97a75ce24faf15485d49 /libs/pbd/pbd/stateful.h | |
parent | c9d433d9b3f166e761bfc1b4765cc51b0a521e7d (diff) |
Move ARDOUR::Change into PBD so that Stateful can be aware of
what Change a State reflects. Hence allow Stateful to do some
of the work of set/get_state in Region.
git-svn-id: svn://localhost/ardour2/branches/3.0@6671 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd/pbd/stateful.h')
-rw-r--r-- | libs/pbd/pbd/stateful.h | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/libs/pbd/pbd/stateful.h b/libs/pbd/pbd/stateful.h index 60c1c6b23c..80d7aa796a 100644 --- a/libs/pbd/pbd/stateful.h +++ b/libs/pbd/pbd/stateful.h @@ -33,13 +33,20 @@ namespace sys { class path; } +enum Change { + range_guarantee = ~0 +}; + +Change new_change (); + /** Base (non template) part of State */ class StateBase { public: - StateBase (std::string const & p) + StateBase (std::string const & p, Change c) : _have_old (false) , _xml_property_name (p) + , _change (c) { } @@ -47,6 +54,7 @@ public: StateBase (StateBase const & s) : _have_old (s._have_old) , _xml_property_name (s._xml_property_name) + , _change (s._change) { } @@ -57,10 +65,13 @@ public: } virtual void diff (XMLNode *, XMLNode *) const = 0; + virtual Change set_state (XMLNode const &) = 0; + virtual void add_state (XMLNode &) const = 0; protected: bool _have_old; std::string _xml_property_name; + Change _change; }; /** Class to represent a single piece of state in a Stateful object */ @@ -68,8 +79,8 @@ template <class T> class State : public StateBase { public: - State (std::string const & p, T const & v) - : StateBase (p) + State (std::string const & p, Change c, T const & v) + : StateBase (p, c) , _current (v) { @@ -86,6 +97,7 @@ public: /* XXX: isn't there a nicer place to do this? */ _have_old = s._have_old; _xml_property_name = s._xml_property_name; + _change = s._change; _current = s._current; _old = s._old; @@ -112,13 +124,36 @@ public: void diff (XMLNode* old, XMLNode* current) const { if (_have_old) { - std::stringstream o; - o << _old; - old->add_property (_xml_property_name.c_str(), o.str().c_str()); - std::stringstream c; - c << _current; - current->add_property (_xml_property_name.c_str(), c.str().c_str()); + old->add_property (_xml_property_name.c_str(), to_string (_old)); + current->add_property (_xml_property_name.c_str(), to_string (_current)); + } + } + + /** Try to set state from the property of an XML node. + * @param node XML node. + * @return Change effected, or 0. + */ + Change set_state (XMLNode const & node) { + XMLProperty const * p = node.property (_xml_property_name.c_str()); + + if (p) { + std::stringstream s (p->value ()); + T v; + s >> v; + + if (v == _current) { + return Change (0); + } + + set (v); + return _change; } + + return Change (0); + } + + void add_state (XMLNode & node) const { + node.add_property (_xml_property_name.c_str(), to_string (_current)); } private: @@ -127,6 +162,12 @@ private: _have_old = true; _current = v; } + + std::string to_string (T const & v) const { + std::stringstream s; + s << v; + return s.str (); + } T _current; T _old; @@ -163,6 +204,8 @@ class Stateful { void add_instant_xml (XMLNode&, const sys::path& directory_path); XMLNode *instant_xml (const std::string& str, const sys::path& directory_path); + Change set_state_using_states (XMLNode const &); + void add_states (XMLNode &); XMLNode *_extra_xml; XMLNode *_instant_xml; |