summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2010-09-01 12:46:03 +0100
committerDaniel P. Berrange <berrange@redhat.com>2010-09-01 12:46:03 +0100
commit460e66e9acba924fdd49dd9b6fcc0db5228dac5b (patch)
tree36161bf3d6d30f36933965fa6fe62c6a56ed120a
parenta810921fed96fd0e0279067889ad6b86953e2417 (diff)
Remove list iterator & add API to retrieve a GList
The OsinfoList iterator method does not play nicely with gobject introspection bindings. Remove it, and replace with a method that returns a GList of elements. This trivially maps to the language bindings native list/array classes which are natively iterable. * docs/reference/Libosinfo-sections.txt: Remove osinfo_list_foreach and add osinfo_list_get_elements * osinfo/osinfo_list.c, osinfo/osinfo_list.h: Remove osinfo_list_foreach and add osinfo_list_get_elements * osinfo/osinfo_db.c: Rewrite to use g_list_foreach and fix some GList annotations to specify element type correctly. * test/test-list.c: Rewrite to use g_list_foreach
-rw-r--r--docs/reference/Libosinfo-sections.txt3
-rw-r--r--osinfo/libosinfo.syms2
-rw-r--r--osinfo/osinfo_db.c34
-rw-r--r--osinfo/osinfo_list.c37
-rw-r--r--osinfo/osinfo_list.h5
-rw-r--r--test/test-list.c35
6 files changed, 60 insertions, 56 deletions
diff --git a/docs/reference/Libosinfo-sections.txt b/docs/reference/Libosinfo-sections.txt
index ff24b1d..baa086f 100644
--- a/docs/reference/Libosinfo-sections.txt
+++ b/docs/reference/Libosinfo-sections.txt
@@ -43,8 +43,7 @@ osinfo_list_add_filtered
osinfo_list_add_intersection
osinfo_list_add_union
osinfo_list_add_all
-osinfo_list_iterator
-osinfo_list_foreach
+osinfo_list_get_elements
<SUBSECTION Standard>
OSINFO_LIST
OSINFO_IS_LIST
diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 494c29b..14aceac 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -58,7 +58,7 @@ LIBOSINFO_0.0.1 {
osinfo_list_add_intersection;
osinfo_list_add_union;
osinfo_list_add_all;
- osinfo_list_foreach;
+ osinfo_list_get_elements;
osinfo_osfilter_get_type;
osinfo_osfilter_new;
osinfo_osfilter_add_os_constraint;
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index 114c2b6..18bba0c 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -232,9 +232,10 @@ struct osinfo_db_populate_values_args {
const gchar *property;
};
-static gboolean osinfo_db_get_property_values_in_entity(OsinfoList *list, OsinfoEntity *entity, gpointer data)
+static void osinfo_db_get_property_values_in_entity(gpointer data, gpointer opaque)
{
- struct osinfo_db_populate_values_args *args = data;
+ OsinfoEntity *entity = OSINFO_ENTITY(data);
+ struct osinfo_db_populate_values_args *args = opaque;
GHashTable *newValues = args->values;
const gchar *property = args->property;
GList *values = osinfo_entity_get_param_value_list(entity, property);
@@ -253,8 +254,6 @@ static gboolean osinfo_db_get_property_values_in_entity(OsinfoList *list, Osinfo
}
g_list_free(values);
-
- return FALSE; // Continue iterating
}
static GList *osinfo_db_unique_values_for_property_in_entity(OsinfoList *entities, const gchar *propName)
@@ -263,8 +262,11 @@ static GList *osinfo_db_unique_values_for_property_in_entity(OsinfoList *entitie
GHashTable *values = g_hash_table_new(g_str_hash, g_str_equal);
GList *ret;
struct osinfo_db_populate_values_args args = { values, propName };
+ GList *elements;
- osinfo_list_foreach(entities, osinfo_db_get_property_values_in_entity, &args);
+ elements = osinfo_list_get_elements(entities);
+ g_list_foreach(elements, osinfo_db_get_property_values_in_entity, &args);
+ g_list_free(elements);
ret = g_hash_table_get_keys(values);
g_hash_table_unref(values);
@@ -279,7 +281,7 @@ static GList *osinfo_db_unique_values_for_property_in_entity(OsinfoList *entitie
* Get all unique values for a named property amongst all
* operating systems in the database
*
- * Returns: (transfer container): a list of strings
+ * Returns: (transfer container)(element-type utf8): a list of strings
*/
GList *osinfo_db_unique_values_for_property_in_os(OsinfoDb *db, const gchar *propName)
{
@@ -298,7 +300,7 @@ GList *osinfo_db_unique_values_for_property_in_os(OsinfoDb *db, const gchar *pro
* Get all unique values for a named property amongst all
* hypervisors in the database
*
- * Returns: (transfer container): a list of strings
+ * Returns: (transfer container)(element-type utf8): a list of strings
*/
GList *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *db, const gchar *propName)
{
@@ -310,14 +312,14 @@ GList *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *db, const gchar *pro
/**
- * osinfo_db_unique_values_for_property_in_hv:
+ * osinfo_db_unique_values_for_property_in_dev:
* @db: the database
* @propName: a property name
*
* Get all unique values for a named property amongst all
* devices in the database
*
- * Returns: (transfer container): a list of strings
+ * Returns: (transfer container)(element-type utf8): a list of strings
*/
GList *osinfo_db_unique_values_for_property_in_dev(OsinfoDb *db, const gchar *propName)
{
@@ -333,10 +335,10 @@ struct __osinfoOsCheckRelationshipArgs {
};
-static gboolean __osinfoAddOsIfRelationship(OsinfoList *list, OsinfoEntity *entity, gpointer data)
+static void __osinfoAddOsIfRelationship(gpointer data, gpointer opaque)
{
- struct __osinfoOsCheckRelationshipArgs *args = data;
- OsinfoOs *os = OSINFO_OS(entity);
+ struct __osinfoOsCheckRelationshipArgs *args = opaque;
+ OsinfoOs *os = OSINFO_OS(data);
OsinfoList *newList = args->list;
OsinfoOsList *thisList = osinfo_os_get_related(os, args->relshp);
int i;
@@ -347,8 +349,6 @@ static gboolean __osinfoAddOsIfRelationship(OsinfoList *list, OsinfoEntity *enti
}
g_object_unref(thisList);
-
- return FALSE;
}
/**
@@ -365,12 +365,12 @@ OsinfoOsList *osinfo_db_unique_values_for_os_relationship(OsinfoDb *db, OsinfoOs
{
g_return_val_if_fail(OSINFO_IS_DB(db), NULL);
- // Create list
OsinfoOsList *newList = osinfo_oslist_new();
-
struct __osinfoOsCheckRelationshipArgs args = {OSINFO_LIST (newList), relshp};
+ GList *entities = osinfo_list_get_elements(OSINFO_LIST(db->priv->oses));
- osinfo_list_foreach(OSINFO_LIST(db->priv->oses), __osinfoAddOsIfRelationship, &args);
+ g_list_foreach(entities, __osinfoAddOsIfRelationship, &args);
+ g_list_free(entities);
return newList;
}
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index b62f119..a4f4ed6 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -194,6 +194,26 @@ OsinfoEntity *osinfo_list_get_nth(OsinfoList *list, gint idx)
/**
+ * osinfo_list_get_elements:
+ * @list: the entity list
+ *
+ * Retrieve a linked list of all elements in the list.
+ *
+ * Returns: (transfer container) (element-type OsinfoEntity): the list elements
+ */
+GList *osinfo_list_get_elements(OsinfoList *list)
+{
+ GList *elements = NULL;
+ int i;
+
+ for (i = 0 ; i < list->priv->array->len ; i++) {
+ elements = g_list_prepend(elements,
+ g_ptr_array_index(list->priv->array, i));
+ }
+ return g_list_reverse(elements);
+}
+
+/**
* osinfo_list_find_by_id:
* @list: the entity list
* @id: the unique identifier
@@ -366,23 +386,6 @@ void osinfo_list_add_all(OsinfoList *list, OsinfoList *source)
}
-/**
- * osinfo_list_foreach:
- * @list: the entity list
- * @iter: the iterator callback
- * @data: opaque data value
- *
- * Iterate over all entities in the list, invoking @iter. The
- * iterator callback will be passed the opaque data value @data
- */
-void osinfo_list_foreach(OsinfoList *list, osinfo_list_iterator iter, gpointer data)
-{
- int i;
- for (i = 0 ; i < list->priv->array->len ; i++) {
- OsinfoEntity *ent = g_ptr_array_index(list->priv->array, i);
- iter(list, ent, data);
- }
-}
/*
* Local variables:
* indent-tabs-mode: nil
diff --git a/osinfo/osinfo_list.h b/osinfo/osinfo_list.h
index 8c5533d..49dfe54 100644
--- a/osinfo/osinfo_list.h
+++ b/osinfo/osinfo_list.h
@@ -67,15 +67,14 @@ gint osinfo_list_get_length(OsinfoList *list);
OsinfoEntity *osinfo_list_get_nth(OsinfoList *list, gint idx);
OsinfoEntity *osinfo_list_find_by_id(OsinfoList *list, const gchar *id);
+GList *osinfo_list_get_elements(OsinfoList *list);
+
void osinfo_list_add(OsinfoList *list, OsinfoEntity *entity);
void osinfo_list_add_filtered(OsinfoList *list, OsinfoList *source, OsinfoFilter *filter);
void osinfo_list_add_intersection(OsinfoList *list, OsinfoList *sourceOne, OsinfoList *sourceTwo);
void osinfo_list_add_union(OsinfoList *list, OsinfoList *sourceOne, OsinfoList *sourceTwo);
void osinfo_list_add_all(OsinfoList *list, OsinfoList *source);
-typedef gboolean (*osinfo_list_iterator)(OsinfoList *list, OsinfoEntity *entity, gpointer data);
-void osinfo_list_foreach(OsinfoList *list, osinfo_list_iterator iter, gpointer data);
-
#endif /* __OSINFO_LIST_H__ */
/*
* Local variables:
diff --git a/test/test-list.c b/test/test-list.c
index d3e626a..09cae5a 100644
--- a/test/test-list.c
+++ b/test/test-list.c
@@ -274,22 +274,21 @@ struct iterateData {
gboolean hasBad;
};
-static gboolean iterator(OsinfoList *list G_GNUC_UNUSED, OsinfoEntity *ent, gpointer opaque)
+static void iterator(gpointer data, gpointer opaque)
{
- struct iterateData *data = opaque;
-
- if (data->ent1 == ent)
- data->has1 = TRUE;
- else if (data->ent2 == ent)
- data->has2 = TRUE;
- else if (data->ent3 == ent)
- data->has3 = TRUE;
- else if (data->ent4 == ent)
- data->has4 = TRUE;
+ OsinfoEntity *ent = OSINFO_ENTITY(data);
+ struct iterateData *idata = opaque;
+
+ if (idata->ent1 == ent)
+ idata->has1 = TRUE;
+ else if (idata->ent2 == ent)
+ idata->has2 = TRUE;
+ else if (idata->ent3 == ent)
+ idata->has3 = TRUE;
+ else if (idata->ent4 == ent)
+ idata->has4 = TRUE;
else
- data->hasBad = TRUE;
-
- return TRUE;
+ idata->hasBad = TRUE;
}
START_TEST(test_iterate)
@@ -312,7 +311,9 @@ START_TEST(test_iterate)
ent1, ent2, ent3, ent4,
FALSE, FALSE, FALSE, FALSE, FALSE
};
- osinfo_list_foreach(list1, iterator, &data);
+ GList *elements = osinfo_list_get_elements(list1);
+ g_list_foreach(elements, iterator, &data);
+ g_list_free(elements);
fail_unless(data.has1, "List was missing entity 1");
fail_unless(data.has2, "List was missing entity 2");
fail_unless(data.has3, "List was missing entity 3");
@@ -321,7 +322,9 @@ START_TEST(test_iterate)
data.has1 = data.has2 = data.has3 = data.has4 = data.hasBad = FALSE;
- osinfo_list_foreach(list2, iterator, &data);
+ elements = osinfo_list_get_elements(list2);
+ g_list_foreach(elements, iterator, &data);
+ g_list_free(elements);
fail_unless(data.has1, "List was missing entity 1");
fail_unless(!data.has2, "List has unexpected entity 2");
fail_unless(!data.has3, "List has unexpected entity 3");