summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-03-09 18:11:40 +0000
committerDavid Robillard <d@drobilla.net>2011-03-09 18:11:40 +0000
commit027ca551e53800ab7a2cd8375d1cacb980803d75 (patch)
tree5dcd8281dcc4f83ddc07fc047656b57b3d12b74e /src
parent97304fcdaa125bf1bfec30fa0d92b619faa41ea2 (diff)
downloadlilv-027ca551e53800ab7a2cd8375d1cacb980803d75.tar.gz
lilv-027ca551e53800ab7a2cd8375d1cacb980803d75.tar.bz2
lilv-027ca551e53800ab7a2cd8375d1cacb980803d75.zip
New collections and iterator API.
Deprecate slv2_*_get_at. Remove slv2_world_get_plugins_by_filter. git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@3057 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src')
-rw-r--r--src/collections.c170
-rw-r--r--src/plugin.c27
-rw-r--r--src/plugins.c74
-rw-r--r--src/pluginui.c2
-rw-r--r--src/pluginuiinstance.c2
-rw-r--r--src/port.c12
-rw-r--r--src/slv2_internal.h9
-rw-r--r--src/world.c23
8 files changed, 150 insertions, 169 deletions
diff --git a/src/collections.c b/src/collections.c
index 43c8af7..ee84f68 100644
--- a/src/collections.c
+++ b/src/collections.c
@@ -27,54 +27,85 @@
#include "slv2_internal.h"
-/* SEQUENCE */
-
-#define SLV2_SEQUENCE_IMPL(CollType, ElemType, prefix, free_func) \
-\
-CollType \
-prefix ## _new() \
-{ \
- return g_sequence_new((GDestroyNotify)free_func); \
-} \
-\
-SLV2_API \
-void \
-prefix ## _free(CollType coll) \
-{ \
- if (coll) \
- g_sequence_free((GSequence*)coll); \
-} \
-\
-SLV2_API \
-unsigned \
-prefix ## _size(CollType coll) \
-{ \
- return (coll ? g_sequence_get_length((GSequence*)coll) : 0); \
-} \
-\
-SLV2_API \
-ElemType \
-prefix ## _get_at(CollType coll, unsigned index) \
-{ \
- if (!coll || index >= prefix ## _size(coll)) { \
- return NULL; \
- } else { \
- GSequenceIter* i = g_sequence_get_iter_at_pos((GSequence*)coll, (int)index); \
- return (ElemType)g_sequence_get(i); \
- } \
-}
-
-SLV2_SEQUENCE_IMPL(SLV2ScalePoints, SLV2ScalePoint,
- slv2_scale_points, &slv2_scale_point_free)
-
-SLV2_SEQUENCE_IMPL(SLV2Values, SLV2Value,
- slv2_values, &slv2_value_free)
-
-SLV2_SEQUENCE_IMPL(SLV2PluginClasses, SLV2PluginClass,
- slv2_plugin_classes, &slv2_plugin_class_free)
-
-SLV2_SEQUENCE_IMPL(SLV2UIs, SLV2UI,
- slv2_uis, &slv2_ui_free)
+/* Generic collection functions */
+
+static inline SLV2Collection
+slv2_collection_new(GDestroyNotify destructor)
+{
+ return g_sequence_new(destructor);
+}
+
+SLV2_API
+void
+slv2_collection_free(SLV2Collection coll)
+{
+ if (coll)
+ g_sequence_free((GSequence*)coll);
+}
+
+SLV2_API
+unsigned
+slv2_collection_size(SLV2Collection coll)
+{
+ return (coll ? g_sequence_get_length((GSequence*)coll) : 0);
+}
+
+SLV2_API
+void*
+slv2_collection_get_at(SLV2Collection coll, unsigned index)
+{
+ if (!coll || index >= slv2_collection_size(coll)) {
+ return NULL;
+ } else {
+ GSequenceIter* i = g_sequence_get_iter_at_pos((GSequence*)coll, (int)index);
+ return g_sequence_get(i);
+ }
+}
+
+SLV2_API
+SLV2Iter
+slv2_collection_begin(SLV2Collection collection)
+{
+ return collection ? g_sequence_get_begin_iter(collection) : NULL;
+}
+
+SLV2_API
+void*
+slv2_collection_get(SLV2Collection collection,
+ SLV2Iter i)
+{
+ return g_sequence_get((GSequenceIter*)i);
+}
+
+/* Constructors */
+
+SLV2_API
+SLV2ScalePoints
+slv2_scale_points_new(void)
+{
+ return slv2_collection_new((GDestroyNotify)slv2_scale_point_free);
+}
+
+SLV2_API
+SLV2Values
+slv2_values_new(void)
+{
+ return slv2_collection_new((GDestroyNotify)slv2_value_free);
+}
+
+SLV2UIs
+slv2_uis_new(void)
+{
+ return slv2_collection_new((GDestroyNotify)slv2_ui_free);
+}
+
+SLV2PluginClasses
+slv2_plugin_classes_new(void)
+{
+ return slv2_collection_new((GDestroyNotify)slv2_plugin_class_free);
+}
+
+/* URI based accessors (for collections of things with URIs) */
SLV2_API
SLV2PluginClass
@@ -90,17 +121,54 @@ slv2_uis_get_by_uri(SLV2UIs coll, SLV2Value uri)
return (SLV2UIs)slv2_sequence_get_by_uri(coll, uri);
}
+/* Plugins */
-/* VALUES */
+SLV2Plugins
+slv2_plugins_new()
+{
+ return g_sequence_new(NULL);
+}
+
+void
+slv2_plugins_free(SLV2World world, SLV2Plugins list)
+{
+ if (list && list != world->plugins)
+ g_sequence_free(list);
+}
+
+SLV2_API
+SLV2Plugin
+slv2_plugins_get_by_uri(SLV2Plugins list, SLV2Value uri)
+{
+ return (SLV2Plugin)slv2_sequence_get_by_uri(list, uri);
+}
+
+
+/* Values */
SLV2_API
bool
slv2_values_contains(SLV2Values list, SLV2Value value)
{
- for (unsigned i = 0; i < slv2_values_size(list); ++i)
- if (slv2_value_equals(slv2_values_get_at(list, i), value))
+ SLV2_FOREACH(i, list)
+ if (slv2_value_equals(slv2_values_get(list, i), value))
return true;
return false;
}
+/* Iterator */
+
+SLV2_API
+SLV2Iter
+slv2_iter_next(SLV2Iter i)
+{
+ return g_sequence_iter_next((GSequenceIter*)i);
+}
+
+SLV2_API
+bool
+slv2_iter_end(SLV2Iter i)
+{
+ return !i || g_sequence_iter_is_end((GSequenceIter*)i);
+}
diff --git a/src/plugin.c b/src/plugin.c
index 4159cdf..e86b16a 100644
--- a/src/plugin.c
+++ b/src/plugin.c
@@ -131,7 +131,7 @@ slv2_plugin_get_unique(SLV2Plugin p, SLV2Node subject, SLV2Node predicate)
sord_node_get_string(predicate));
return NULL;
}
- SLV2Value ret = slv2_value_duplicate(slv2_values_get_at(values, 0));
+ SLV2Value ret = slv2_value_duplicate(slv2_values_get_first(values));
slv2_values_free(values);
return ret;
}
@@ -143,7 +143,7 @@ slv2_plugin_get_one(SLV2Plugin p, SLV2Node subject, SLV2Node predicate)
if (!values) {
return NULL;
}
- SLV2Value ret = slv2_value_duplicate(slv2_values_get_at(values, 0));
+ SLV2Value ret = slv2_value_duplicate(slv2_values_get_first(values));
slv2_values_free(values);
return ret;
}
@@ -152,8 +152,8 @@ static void
slv2_plugin_load(SLV2Plugin p)
{
// Parse all the plugin's data files into RDF model
- for (unsigned i = 0; i < slv2_values_size(p->data_uris); ++i) {
- SLV2Value data_uri_val = slv2_values_get_at(p->data_uris, i);
+ SLV2_FOREACH(i, p->data_uris) {
+ SLV2Value data_uri_val = slv2_values_get(p->data_uris, i);
sord_read_file(p->world->model,
sord_node_get_string(data_uri_val->val.uri_val),
p->bundle_uri->val.uri_val,
@@ -434,7 +434,7 @@ slv2_plugin_get_name(SLV2Plugin plugin)
SLV2Value ret = NULL;
if (results) {
- SLV2Value val = slv2_values_get_at(results, 0);
+ SLV2Value val = slv2_values_get_first(results);
if (slv2_value_is_string(val))
ret = slv2_value_duplicate(val);
slv2_values_free(results);
@@ -653,17 +653,16 @@ SLV2_API
SLV2Values
slv2_plugin_get_supported_features(SLV2Plugin p)
{
- SLV2Values optional = slv2_plugin_get_optional_features(p);
- SLV2Values required = slv2_plugin_get_required_features(p);
- SLV2Values result = slv2_values_new();
- unsigned n_optional = slv2_values_size(optional);
- unsigned n_required = slv2_values_size(required);
- for (unsigned i = 0 ; i < n_optional; ++i)
+ SLV2Values optional = slv2_plugin_get_optional_features(p);
+ SLV2Values required = slv2_plugin_get_required_features(p);
+ SLV2Values result = slv2_values_new();
+
+ SLV2_FOREACH(i, optional)
slv2_array_append(
- result, slv2_value_duplicate(slv2_values_get_at(optional, i)));
- for (unsigned i = 0 ; i < n_required; ++i)
+ result, slv2_value_duplicate(slv2_values_get(optional, i)));
+ SLV2_FOREACH(i, required)
slv2_array_append(
- result, slv2_value_duplicate(slv2_values_get_at(required, i)));
+ result, slv2_value_duplicate(slv2_values_get(required, i)));
slv2_values_free(optional);
slv2_values_free(required);
diff --git a/src/plugins.c b/src/plugins.c
deleted file mode 100644
index 0e62419..0000000
--- a/src/plugins.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- Copyright 2007-2011 David Robillard <http://drobilla.net>
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-#define _XOPEN_SOURCE 500
-
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "slv2_internal.h"
-
-SLV2Plugins
-slv2_plugins_new()
-{
- return g_sequence_new(NULL);
-}
-
-SLV2_API
-void
-slv2_plugins_free(SLV2World world, SLV2Plugins list)
-{
- if (list && list != world->plugins)
- g_sequence_free(list);
-}
-
-SLV2_API
-unsigned
-slv2_plugins_size(SLV2Plugins list)
-{
- return (list ? g_sequence_get_length((GSequence*)list) : 0); \
-}
-
-SLV2_API
-SLV2Plugin
-slv2_plugins_get_by_uri(SLV2Plugins list, SLV2Value uri)
-{
- return (SLV2Plugin)slv2_sequence_get_by_uri(list, uri);
-}
-
-SLV2_API
-SLV2Plugin
-slv2_plugins_get_at(SLV2Plugins list, unsigned index)
-{
- if (!list || index >= slv2_plugins_size(list)) {
- return NULL;
- } else {
- GSequenceIter* i = g_sequence_get_iter_at_pos((GSequence*)list, (int)index);
- return (SLV2Plugin)g_sequence_get(i);
- }
-}
-
diff --git a/src/pluginui.c b/src/pluginui.c
index e857e60..4d61690 100644
--- a/src/pluginui.c
+++ b/src/pluginui.c
@@ -97,7 +97,7 @@ slv2_ui_supported(SLV2UI ui,
#ifdef HAVE_SUIL
return suil_ui_type_supported(
slv2_value_as_uri(widget_type_uri),
- slv2_value_as_uri(slv2_values_get_at(ui->classes, 0)));
+ slv2_value_as_uri(slv2_values_get_first(ui->classes)));
#else
return false;
#endif
diff --git a/src/pluginuiinstance.c b/src/pluginuiinstance.c
index 2c7b60f..189ae6e 100644
--- a/src/pluginuiinstance.c
+++ b/src/pluginuiinstance.c
@@ -94,7 +94,7 @@ slv2_ui_instance_new(SLV2Plugin plugin,
return NULL;
}
- SLV2Value ui_type = slv2_values_get_at(ui->classes, 0);
+ SLV2Value ui_type = slv2_values_get_first(ui->classes);
if (!widget_type_uri) {
widget_type_uri = ui_type;
}
diff --git a/src/port.c b/src/port.c
index cb96bf9..6ae6a08 100644
--- a/src/port.c
+++ b/src/port.c
@@ -57,8 +57,8 @@ slv2_port_is_a(SLV2Plugin plugin,
SLV2Port port,
SLV2Value port_class)
{
- for (unsigned i = 0; i < slv2_values_size(port->classes); ++i)
- if (slv2_value_equals(slv2_values_get_at(port->classes, i), port_class))
+ SLV2_FOREACH(i, port->classes)
+ if (slv2_value_equals(slv2_values_get(port->classes, i), port_class))
return true;
return false;
@@ -209,7 +209,7 @@ slv2_port_get_name(SLV2Plugin p,
SLV2Value ret = NULL;
if (results) {
- SLV2Value val = slv2_values_get_at(results, 0);
+ SLV2Value val = slv2_values_get_first(results);
if (slv2_value_is_string(val))
ret = slv2_value_duplicate(val);
slv2_values_free(results);
@@ -242,7 +242,7 @@ slv2_port_get_range(SLV2Plugin p,
SLV2Values defaults = slv2_port_get_value_by_node(
p, port, p->world->lv2_default_node);
*def = defaults
- ? slv2_value_duplicate(slv2_values_get_at(defaults, 0))
+ ? slv2_value_duplicate(slv2_values_get_first(defaults))
: NULL;
slv2_values_free(defaults);
}
@@ -250,7 +250,7 @@ slv2_port_get_range(SLV2Plugin p,
SLV2Values minimums = slv2_port_get_value_by_node(
p, port, p->world->lv2_minimum_node);
*min = minimums
- ? slv2_value_duplicate(slv2_values_get_at(minimums, 0))
+ ? slv2_value_duplicate(slv2_values_get_first(minimums))
: NULL;
slv2_values_free(minimums);
}
@@ -258,7 +258,7 @@ slv2_port_get_range(SLV2Plugin p,
SLV2Values maximums = slv2_port_get_value_by_node(
p, port, p->world->lv2_maximum_node);
*max = maximums
- ? slv2_value_duplicate(slv2_values_get_at(maximums, 0))
+ ? slv2_value_duplicate(slv2_values_get_first(maximums))
: NULL;
slv2_values_free(maximums);
}
diff --git a/src/slv2_internal.h b/src/slv2_internal.h
index a3b4f1e..880f3c5 100644
--- a/src/slv2_internal.h
+++ b/src/slv2_internal.h
@@ -143,6 +143,15 @@ slv2_plugin_get_unique(SLV2Plugin p,
SLV2Plugins
slv2_plugins_new();
+/**
+ Free a plugin collection.
+ Freeing a plugin collection does not destroy the plugins it contains
+ (plugins are owned by the world). @a plugins is invalid after this call.
+*/
+void
+slv2_plugins_free(SLV2World world,
+ SLV2Plugins plugins);
+
/* ********* Instance ********* */
diff --git a/src/world.c b/src/world.c
index 3749f1f..97539c0 100644
--- a/src/world.c
+++ b/src/world.c
@@ -154,13 +154,8 @@ slv2_world_free(SLV2World world)
slv2_value_free(world->doap_name_val);
slv2_value_free(world->lv2_name_val);
-#define SLV2_FOREACH(iter, seq) \
- for (GSequenceIter* (iter) = g_sequence_get_begin_iter(seq); \
- (iter) != g_sequence_get_end_iter(seq); \
- (iter) = g_sequence_iter_next(iter))
-
SLV2_FOREACH(i, world->plugins) {
- SLV2Plugin p = g_sequence_get(i);
+ SLV2Plugin p = slv2_plugins_get(world->plugins, i);
slv2_plugin_free(p);
}
g_sequence_free(world->plugins);
@@ -692,22 +687,6 @@ slv2_world_get_all_plugins(SLV2World world)
}
SLV2_API
-SLV2Plugins
-slv2_world_get_plugins_by_filter(SLV2World world, bool (*include)(SLV2Plugin))
-{
- SLV2Plugins result = slv2_plugins_new();
-
- const unsigned n = slv2_plugins_size(world->plugins);
- for (unsigned i = 0; i < n; ++i) {
- SLV2Plugin p = slv2_plugins_get_at(world->plugins, i);
- if (include(p))
- slv2_sequence_insert(result, p);
- }
-
- return result;
-}
-
-SLV2_API
SLV2Plugin
slv2_world_get_plugin_by_uri_string(SLV2World world,
const char* uri)