From 88f9a3bf23fc629385978633a8764d74788e1fdc Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 31 Jul 2012 00:37:01 +0000 Subject: Clean up Resource. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4580 a436a847-0d15-0410-975c-d299462d15a1 --- ingen/Resource.hpp | 104 +++++++++++++++++++++++++++++--------------- src/Resource.cpp | 20 +++------ src/server/events/Delta.cpp | 6 ++- 3 files changed, 78 insertions(+), 52 deletions(-) diff --git a/ingen/Resource.hpp b/ingen/Resource.hpp index 1e94e1e8..feb905fb 100644 --- a/ingen/Resource.hpp +++ b/ingen/Resource.hpp @@ -49,34 +49,29 @@ public: static Raul::URI graph_to_uri(Graph g) { switch (g) { - default: - case DEFAULT: - return NS_INGEN "defaultContext"; - case EXTERNAL: - return NS_INGEN "externalContext"; - case INTERNAL: - return NS_INGEN "internalContext"; + case DEFAULT: return NS_INGEN "defaultContext"; + case EXTERNAL: return NS_INGEN "externalContext"; + case INTERNAL: return NS_INGEN "internalContext"; } } static Graph uri_to_graph(const char* uri) { + const char* suffix = uri + sizeof(NS_INGEN) - 1; if (strncmp(uri, NS_INGEN, sizeof(NS_INGEN) - 1)) { return DEFAULT; - } - - const char* suffix = uri + sizeof(NS_INGEN) - 1; - if (!strcmp(suffix, "defaultContext")) { + } else if (!strcmp(suffix, "defaultContext")) { return DEFAULT; } else if (!strcmp(suffix, "externalContext")) { return EXTERNAL; } else if (!strcmp(suffix, "internalContext")) { return INTERNAL; + } else { + Raul::error << "Unknown context URI " << uri << std::endl; + return DEFAULT; } - - Raul::error << "Unknown context URI " << uri << std::endl; - return DEFAULT; } + /** A property value (an Atom with a context). */ class Property : public Raul::Atom { public: Property(const Raul::Atom& atom, Graph ctx=DEFAULT) @@ -93,46 +88,73 @@ public: virtual ~Resource() {} - URIs& uris() const { return _uris; } - - virtual void set_uri(const Raul::URI& uri) { _uri = uri; } - virtual const Raul::URI& uri() const { return _uri; } - typedef std::multimap Properties; - static void set_context(Properties& props, Graph ctx) { - for (Properties::iterator i = props.begin(); i != props.end(); ++i) { - i->second.set_context(ctx); - } - } - - Properties properties(Resource::Graph ctx) const; + /** Get a single property value. + * + * This is only useful for properties with a single value. If the + * requested property has several values, the first will be returned. + */ + virtual const Raul::Atom& get_property(const Raul::URI& uri) const; - virtual const Properties& properties() const { return _properties; } - virtual Properties& properties() { return _properties; } - virtual const Raul::Atom& get_property(const Raul::URI& uri) const; + /** Set (replace) a property value. + * + * This will first erase any properties with the given @p uri, so after + * this call exactly one property with predicate @p uri will be set. + */ virtual const Raul::Atom& set_property(const Raul::URI& uri, const Raul::Atom& value, Graph ctx=DEFAULT); - virtual void add_property(const Raul::URI& uri, - const Raul::Atom& value, - Graph ctx=DEFAULT); - virtual void remove_property(const Raul::URI& uri, - const Raul::Atom& value); - virtual bool has_property(const Raul::URI& uri, - const Raul::Atom& value) const; + /** Add a property value. + * + * This will not remove any existing values, so if properties with + * predicate @p uri and values other than @p value exist, this will result + * in multiple values for the property. + */ + virtual void add_property(const Raul::URI& uri, + const Raul::Atom& value, + Graph ctx=DEFAULT); + + /** Remove a property. + * + * If @p value is ingen:wildcard then any property with @p uri for a + * predicate will be removed. + */ + virtual void remove_property(const Raul::URI& uri, + const Raul::Atom& value); + + /** Return true iff a property is set. */ + virtual bool has_property(const Raul::URI& uri, + const Raul::Atom& value) const; + + /** Set (replace) several properties at once. + * + * This will erase all properties with keys in @p p, though multiple values + * for one property may exist in @p and will all be set (unlike simply + * calling set_property in a loop which would only set one value). + */ void set_properties(const Properties& p); + + /** Add several properties at once. */ void add_properties(const Properties& p); + + /** Remove several properties at once. + * + * This removes all matching properties (both key and value), or all + * properties with a matching key if the value in @p is ingen:wildcard. + */ void remove_properties(const Properties& p); /** Hook called whenever a property is added. + * * This can be used by derived classes to implement special behaviour for * particular properties (e.g. ingen:value for ports). */ virtual void on_property(const Raul::URI& uri, const Raul::Atom& value) {} /** Get the ingen type from a set of Properties. + * * If some coherent ingen type is found, true is returned and the appropriate * output parameter set to true. Otherwise false is returned. */ @@ -143,6 +165,16 @@ public: bool& port, bool& is_output); + virtual void set_uri(const Raul::URI& uri) { _uri = uri; } + + /** Get all the properties with a given context. */ + Properties properties(Resource::Graph ctx) const; + + URIs& uris() const { return _uris; } + const Raul::URI& uri() const { return _uri; } + const Properties& properties() const { return _properties; } + Properties& properties() { return _properties; } + protected: const Raul::Atom& set_property(const Raul::URI& uri, const Raul::Atom& value) const; diff --git a/src/Resource.cpp b/src/Resource.cpp index d8659596..e7271634 100644 --- a/src/Resource.cpp +++ b/src/Resource.cpp @@ -71,8 +71,9 @@ Resource::remove_property(const Raul::URI& uri, const Raul::Atom& value) if (value == _uris.wildcard) { _properties.erase(uri); } else { - Properties::iterator i = _properties.find(uri); - for (; (i != _properties.end()) && (i->first == uri); ++i) { + for (Properties::iterator i = _properties.find(uri); + i != _properties.end() && (i->first == uri); + ++i) { if (i->second == value) { _properties.erase(i); return; @@ -180,19 +181,8 @@ void Resource::remove_properties(const Properties& p) { typedef Resource::Properties::const_iterator iterator; - for (iterator i = p.begin(); i != p.end(); ++i) { - if (i->second == _uris.wildcard) { - _properties.erase(i->first); - } else { - for (Properties::iterator j = _properties.find(i->first); - (j != _properties.end()) && (j->first == i->first); ++j) { - if (j->second == i->second) { - _properties.erase(j); - break; - } - } - } - } + for (iterator i = p.begin(); i != p.end(); ++i) + remove_property(i->first, i->second); } Resource::Properties diff --git a/src/server/events/Delta.cpp b/src/server/events/Delta.cpp index a497bb37..6892ccbc 100644 --- a/src/server/events/Delta.cpp +++ b/src/server/events/Delta.cpp @@ -67,7 +67,11 @@ Delta::Delta(Engine& engine, , _create(create) { if (context != Resource::DEFAULT) { - Resource::set_context(_properties, context); + for (Properties::iterator i = _properties.begin(); + i != _properties.end(); + ++i) { + i->second.set_context(context); + } } #ifdef DUMP -- cgit v1.2.1