Uncrustify. Sorry. :)

git-svn-id: svn://localhost/ardour2/branches/3.0@6704 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
David Robillard 2010-02-19 23:46:00 +00:00
parent 29015dc5df
commit 5fe37dbc53

View file

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2010 Paul Davis Copyright (C) 2010 Paul Davis
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -32,134 +32,137 @@ namespace PBD {
typedef GQuark PropertyID; typedef GQuark PropertyID;
template<typename T> template<typename T>
struct PropertyDescriptor { struct PropertyDescriptor {
PropertyID id; PropertyID id;
typedef T value_type; typedef T value_type;
}; };
class PropertyChange : public std::set<PropertyID> class PropertyChange : public std::set<PropertyID>
{ {
public: public:
PropertyChange() { } PropertyChange() {}
template<typename T> PropertyChange(PropertyDescriptor<T> p) { insert (p.id); }
PropertyChange(const PropertyChange& other) : std::set<PropertyID> (other) { } template<typename T>
PropertyChange(PropertyDescriptor<T> p) { insert (p.id); }
PropertyChange operator= (const PropertyChange& other) {
PropertyChange(const PropertyChange& other) : std::set<PropertyID> (other) {}
PropertyChange operator=(const PropertyChange& other) {
clear (); clear ();
insert (other.begin(), other.end()); insert (other.begin (), other.end ());
return *this; return *this;
} }
template<typename T> PropertyChange operator= (PropertyDescriptor<T> p) { template<typename T>
PropertyChange operator=(PropertyDescriptor<T> p) {
clear (); clear ();
insert (p.id); insert (p.id);
return *this; return *this;
} }
template<typename T> bool contains (PropertyDescriptor<T> p) const { return find (p.id) != end(); } template<typename T>
bool contains (PropertyDescriptor<T> p) const { return find (p.id) != end (); }
bool contains (const PropertyChange& other) const { bool contains (const PropertyChange& other) const {
for (const_iterator x = other.begin(); x != other.end(); ++x) { for (const_iterator x = other.begin (); x != other.end (); ++x) {
if (find (*x) != end()) { if (find (*x) != end ()) {
return true; return true;
} }
} }
return false; return false;
} }
void add (PropertyID id) { (void) insert (id); } void add (PropertyID id) { (void) insert (id); }
void add (const PropertyChange& other) { (void) insert (other.begin(), other.end()); } void add (const PropertyChange& other) { (void) insert (other.begin (), other.end ()); }
template<typename T> void add (PropertyDescriptor<T> p) { (void) insert (p.id); } template<typename T>
void add (PropertyDescriptor<T> p) { (void)insert (p.id); }
}; };
/** Base (non template) part of Property */ /** Base (non template) part of Property */
class PropertyBase class PropertyBase
{ {
public: public:
PropertyBase (PropertyID pid) PropertyBase (PropertyID pid)
: _have_old (false) : _property_id (pid)
, _property_id (pid) , _have_old (false)
{ {}
}
/** Forget about any old value for this state */ /** Forget about any old value for this state */
void clear_history () { void clear_history () {
_have_old = false; _have_old = false;
} }
virtual void diff (XMLNode *, XMLNode *) const = 0; virtual void diff (XMLNode*, XMLNode*) const = 0;
virtual void diff (PropertyChange&) const = 0; virtual void diff (PropertyChange&) const = 0;
virtual bool set_state (XMLNode const &) = 0; virtual bool set_state (XMLNode const&) = 0;
virtual void add_state (XMLNode &) const = 0; virtual void add_state (XMLNode&) const = 0;
const gchar* property_name() const { return g_quark_to_string (_property_id); } const gchar*property_name () const { return g_quark_to_string (_property_id); }
PropertyID id() const { return _property_id; } PropertyID id () const { return _property_id; }
bool operator== (PropertyID pid) const { bool operator==(PropertyID pid) const {
return _property_id == pid; return _property_id == pid;
} }
protected: protected:
bool _have_old;
PropertyID _property_id; PropertyID _property_id;
bool _have_old;
}; };
/** Parent class for classes which represent a single property in a Stateful object */ /** Parent class for classes which represent a single property in a Stateful object */
template <class T> template<class T>
class PropertyTemplate : public PropertyBase class PropertyTemplate : public PropertyBase
{ {
public: public:
PropertyTemplate (PropertyDescriptor<T> p, T const & v) PropertyTemplate (PropertyDescriptor<T> p, T const& v)
: PropertyBase (p.id) : PropertyBase (p.id)
, _current (v) , _current (v)
{ {}
} PropertyTemplate<T>& operator=(PropertyTemplate<T> const& s) {
PropertyTemplate<T> & operator= (PropertyTemplate<T> const & s) {
/* XXX: isn't there a nicer place to do this? */ /* XXX: isn't there a nicer place to do this? */
_have_old = s._have_old; _have_old = s._have_old;
_property_id = s._property_id; _property_id = s._property_id;
_current = s._current; _current = s._current;
_old = s._old; _old = s._old;
return *this; return *this;
} }
T & operator= (T const & v) { T & operator=(T const& v) {
set (v); set (v);
return _current; return _current;
} }
T & operator+= (T const & v) { T & operator+=(T const& v) {
set (_current + v); set (_current + v);
return _current; return _current;
} }
bool operator== (const T& other) const { bool operator==(const T& other) const {
return _current == other; return _current == other;
} }
bool operator!= (const T& other) const { bool operator!=(const T& other) const {
return _current != other; return _current != other;
} }
operator T const & () const { operator T const &() const {
return _current; return _current;
} }
T const & val () const { T const& val () const {
return _current; return _current;
} }
void diff (XMLNode* old, XMLNode* current) const { void diff (XMLNode* old, XMLNode* current) const {
if (_have_old) { if (_have_old) {
old->add_property (property_name(), to_string (_old)); old->add_property (property_name (), to_string (_old));
current->add_property (property_name(), to_string (_current)); current->add_property (property_name (), to_string (_current));
} }
} }
void diff (PropertyChange& c) const { void diff (PropertyChange& c) const {
if (_have_old) { if (_have_old) {
c.add (_property_id); c.add (_property_id);
@ -170,8 +173,8 @@ public:
* @param node XML node. * @param node XML node.
* @return true if the value of the property is changed * @return true if the value of the property is changed
*/ */
bool set_state (XMLNode const & node) { bool set_state (XMLNode const& node) {
XMLProperty const * p = node.property (property_name()); XMLProperty const* p = node.property (property_name ());
if (p) { if (p) {
T const v = from_string (p->value ()); T const v = from_string (p->value ());
@ -180,145 +183,145 @@ public:
set (v); set (v);
return true; return true;
} }
} }
return false; return false;
} }
void add_state (XMLNode & node) const { void add_state (XMLNode& node) const {
node.add_property (property_name(), to_string (_current)); node.add_property (property_name (), to_string (_current));
} }
protected: protected:
void set (T const & v) { void set (T const& v) {
_old = _current; _old = _current;
_have_old = true; _have_old = true;
_current = v; _current = v;
} }
virtual std::string to_string (T const & v) const = 0; virtual std::string to_string (T const& v) const = 0;
virtual T from_string (std::string const & s) const = 0; virtual T from_string (std::string const& s) const = 0;
T _current; T _current;
T _old; T _old;
}; };
template<class T> template<class T>
std::ostream& operator<< (std::ostream& os, PropertyTemplate<T> const & s) std::ostream & operator<<(std::ostream& os, PropertyTemplate<T> const& s)
{ {
return os << s.val(); return os << s.val ();
} }
/** Representation of a single piece of state in a Stateful; for use /** Representation of a single piece of state in a Stateful; for use
* with types that can be written to / read from stringstreams. * with types that can be written to / read from stringstreams.
*/ */
template <class T> template<class T>
class Property : public PropertyTemplate<T> class Property : public PropertyTemplate<T>
{ {
public: public:
Property (PropertyDescriptor<T> q, T const & v) Property (PropertyDescriptor<T> q, T const& v)
: PropertyTemplate<T> (q, v) : PropertyTemplate<T> (q, v)
{ {}
} T & operator=(T const& v) {
T & operator= (T const & v) {
this->set (v); this->set (v);
return this->_current; return this->_current;
} }
private: private:
/* note that we do not set a locale for the streams used /* Note that we do not set a locale for the streams used
in to_string() or from_string(), because we want the * in to_string() or from_string(), because we want the
format to be portable across locales (i.e. C or * format to be portable across locales (i.e. C or
POSIX). Also, there is the small matter of * POSIX). Also, there is the small matter of
std::locale aborting on OS X if used with anything * std::locale aborting on OS X if used with anything
other than C or POSIX locales. * other than C or POSIX locales.
*/ */
std::string to_string (T const & v) const { std::string to_string (T const& v) const {
std::stringstream s; std::stringstream s;
s.precision (12); // in case its floating point s.precision (12); // in case its floating point
s << v; s << v;
return s.str (); return s.str ();
} }
T from_string (std::string const & s) const { T from_string (std::string const& s) const {
std::stringstream t (s); std::stringstream t (s);
T v; T v;
t >> v; t >> v;
return v; return v;
} }
}; };
/** Specialization, for std::string which is common and special (see to_string() and from_string() /** Specialization, for std::string which is common and special (see to_string() and from_string()
Using stringstream to read from a std::string is easy to get wrong because of whitespace * Using stringstream to read from a std::string is easy to get wrong because of whitespace
delineation, etc. * delineation, etc.
*/ */
template <> template<>
class Property<std::string> : public PropertyTemplate<std::string> class Property<std::string> : public PropertyTemplate<std::string>
{ {
public: public:
Property (PropertyDescriptor<std::string> q, std::string const & v) Property (PropertyDescriptor<std::string> q, std::string const& v)
: PropertyTemplate<std::string> (q, v) : PropertyTemplate<std::string> (q, v)
{ {}
} std::string & operator=(std::string const& v) {
std::string & operator= (std::string const & v) {
this->set (v); this->set (v);
return this->_current; return this->_current;
} }
private: private:
std::string to_string (std::string const & v) const { std::string to_string (std::string const& v) const {
return _current; return _current;
} }
std::string from_string (std::string const & s) const { std::string from_string (std::string const& s) const {
return s; return s;
} }
}; };
class PropertyList : public std::map<PropertyID,PropertyBase*> class PropertyList : public std::map<PropertyID, PropertyBase*>
{ {
public: public:
PropertyList() : property_owner (true) {} PropertyList() : _property_owner (true) {}
virtual ~PropertyList() { virtual ~PropertyList() {
if (property_owner) if (_property_owner) {
for (std::map<PropertyID,PropertyBase*>::iterator i = begin(); i != end(); ++i) { for (std::map<PropertyID, PropertyBase*>::iterator i = begin (); i != end (); ++i) {
delete i->second; delete i->second;
} }
} }
/* classes that own property lists use this to add their }
property members to their plists.
*/
bool add (PropertyBase& p) {
return insert (value_type (p.id(), &p)).second;
}
/* code that is constructing a property list for use /* Classes that own property lists use this to add their
in setting the state of an object uses this. * property members to their plists.
*/ */
template<typename T, typename V> bool add (PropertyDescriptor<T> pid, const V& v) { bool add (PropertyBase& p) {
return insert (value_type (pid.id, new Property<T> (pid, (T) v))).second; return insert (value_type (p.id (), &p)).second;
} }
/* Code that is constructing a property list for use
* in setting the state of an object uses this.
*/
template<typename T, typename V>
bool add (PropertyDescriptor<T> pid, const V& v) {
return insert (value_type (pid.id, new Property<T> (pid, (T)v))).second;
}
protected: protected:
bool property_owner; bool _property_owner;
}; };
/** A variant of PropertyList that does not delete its /** A variant of PropertyList that does not delete its
property list in its destructor. Objects with their * property list in its destructor. Objects with their
own Properties store them in an OwnedPropertyList * own Properties store them in an OwnedPropertyList
to avoid having them deleted at the wrong time. * to avoid having them deleted at the wrong time.
*/ */
class OwnedPropertyList : public PropertyList class OwnedPropertyList : public PropertyList
{ {
public: public:
OwnedPropertyList() { property_owner = false; } OwnedPropertyList() { _property_owner = false; }
}; };
} /* namespace PBD */ } /* namespace PBD */
#endif /* __pbd_properties_h__ */ #endif /* __pbd_properties_h__ */