namespace PBD {
-/** Parent class for classes which represent a single scalar property in a Stateful object
- */
+/** Parent class for classes which represent a single scalar property in a Stateful object */
template<class T>
class PropertyTemplate : public PropertyBase
{
, _current (s._current)
{}
+
+ /* OPERATORS / ACCESSORS */
+
T & operator=(T const& v) {
set (v);
return _current;
return _current;
}
- void clear_changes () {
- _have_old = false;
- }
-
- void get_changes_as_xml (XMLNode* history_node) const {
- XMLNode* node = history_node->add_child (property_name());
- node->add_property ("from", to_string (_old));
- node->add_property ("to", to_string (_current));
- }
+ /* MANAGEMENT OF Stateful State */
+
bool set_value (XMLNode const & node) {
XMLProperty const* p = node.property (property_name());
node.add_property (property_name(), to_string (_current));
}
- bool changed () const { return _have_old; }
- void apply_changes (PropertyBase const * p) {
- T v = dynamic_cast<const PropertyTemplate<T>* > (p)->val ();
- if (v != _current) {
- set (v);
- }
+ /* MANAGEMENT OF HISTORY */
+
+ void clear_changes () {
+ _have_old = false;
}
+ bool changed () const { return _have_old; }
+
void invert () {
T const tmp = _current;
_current = _old;
_old = tmp;
}
- void get_changes_as_properties (PropertyList& changes, Command *) const {
- if (this->_have_old) {
+
+ /* TRANSFERRING HISTORY TO / FROM A StatefulDiffCommand */
+
+ void get_changes_as_xml (XMLNode* history_node) const {
+ XMLNode* node = history_node->add_child (property_name());
+ node->add_property ("from", to_string (_old));
+ node->add_property ("to", to_string (_current));
+ }
+
+ void get_changes_as_properties (PropertyList& changes, Command *) const {
+ if (this->_have_old) {
changes.add (clone ());
- }
- }
+ }
+ }
+
+
+ /* VARIOUS */
+
+ void apply_changes (PropertyBase const * p) {
+ T v = dynamic_cast<const PropertyTemplate<T>* > (p)->val ();
+ if (v != _current) {
+ set (v);
+ }
+ }
protected:
void set (T const& v) {
- if (v != _current) {
- if (!_have_old) {
- _old = _current;
- _have_old = true;
- } else {
- if (v == _old) {
- /* value has been reset to the value
- at the start of a history transaction,
- before clear_changes() is called.
- thus there is effectively no apparent
- history for this property.
- */
- _have_old = false;
- }
- }
-
- _current = v;
- }
+ if (v != _current) {
+ if (!_have_old) {
+ _old = _current;
+ _have_old = true;
+ } else {
+ if (v == _old) {
+ /* value has been reset to the value
+ at the start of a history transaction,
+ before clear_changes() is called.
+ thus there is effectively no apparent
+ history for this property.
+ */
+ _have_old = false;
+ }
+ }
+
+ _current = v;
+ }
}
virtual std::string to_string (T const& v) const = 0;
return new Property<T> (this->property_id(), this->_old, this->_current);
}
- Property<T>* clone_from_xml (const XMLNode& node) const {
+ Property<T>* clone_from_xml (const XMLNode& node) const {
XMLNodeList const & children = node.children ();
XMLNodeList::const_iterator i = children.begin();
while (i != children.end() && (*i)->name() != this->property_name()) {
}
return new Property<T> (this->property_id(), from_string (from->value()), from_string (to->value ()));
- }
+ }
T & operator=(T const& v) {
this->set (v);
private:
std::string to_string (std::string const& v) const {
- return _current;
+ return v;
}
std::string from_string (std::string const& s) const {