-/** Base (non template) part of State */
-class StateBase
-{
-public:
- StateBase (std::string const & p)
- : _have_old (false)
- , _xml_property_name (p)
- {
-
- }
-
- StateBase (StateBase const & s)
- : _have_old (s._have_old)
- , _xml_property_name (s._xml_property_name)
- {
-
- }
-
- /** Forget about any old value for this state */
- void clear_history () {
- _have_old = false;
- }
-
- virtual void diff (XMLNode *, XMLNode *) const = 0;
-
-protected:
- bool _have_old;
- std::string _xml_property_name;
-};
-
-/** Class to represent a single piece of state in a Stateful object */
-template <class T>
-class State : public StateBase
-{
-public:
- State (std::string const & p, T const & v)
- : StateBase (p)
- , _current (v)
- {
-
- }
-
- State (State<T> const & s)
- : StateBase (s)
- {
- _current = s._current;
- _old = s._old;
- }
-
- State<T> & operator= (State<T> const & s) {
- /* XXX: isn't there a nicer place to do this? */
- _have_old = s._have_old;
- _xml_property_name = s._xml_property_name;
-
- _current = s._current;
- _old = s._old;
- return *this;
- }
-
- T & operator= (T const & v) {
- set (v);
- return _current;
- }
-
- T & operator+= (T const & v) {
- set (_current + v);
- return _current;
- }
-
- operator T () const {
- return _current;
- }
-
- T const & get () const {
- return _current;
- }
-
- 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());
- }
- }
-
-private:
- void set (T const & v) {
- _old = _current;
- _have_old = true;
- _current = v;
- }
-
- T _current;
- T _old;
-};