[virt-tools-list] [PATCH 14/47] Remove many duplicate methods from device, hv & os list classes
Daniel P. Berrange
berrange at redhat.com
Wed Aug 25 19:37:09 UTC 2010
The OsinfoDeviceList, OsinfoHypervisorList and OsinfoOsList
classes don't need to provide accessors for list elements,
since those are all available via the superclass OsinfoList.
Re-work list intersection, union & filter APIs so that
they add to the current list. Add constructors that
allow creation of new lists with intersections, unions
and filtering.
* osinfo/osinfo_devicelist.c, osinfo/osinfo_devicelist.h,
osinfo/osinfo_hypervisorlist.c, osinfo/osinfo_hypervisorlist.h,
osinfo/osinfo_oslist.c, osinfo/osinfo_oslist.h: Remove all
list element accessors
* osinfo/osinfo_list.c, osinfo/osinfo_list.h: Add constructors
for union, filter & intersection operations.
---
osinfo/osinfo_devicelist.c | 71 ------------------------
osinfo/osinfo_devicelist.h | 5 --
osinfo/osinfo_hypervisorlist.c | 72 ------------------------
osinfo/osinfo_hypervisorlist.h | 5 --
osinfo/osinfo_list.c | 119 +++++++++++++++-------------------------
osinfo/osinfo_list.h | 14 +++-
osinfo/osinfo_oslist.c | 71 ------------------------
osinfo/osinfo_oslist.h | 5 --
8 files changed, 55 insertions(+), 307 deletions(-)
diff --git a/osinfo/osinfo_devicelist.c b/osinfo/osinfo_devicelist.c
index ed44f9f..f8a4661 100644
--- a/osinfo/osinfo_devicelist.c
+++ b/osinfo/osinfo_devicelist.c
@@ -36,74 +36,3 @@ osinfo_devicelist_init (OsinfoDeviceList *self)
}
-OsinfoDevice *osinfo_device_list_get_nth(OsinfoDeviceList *self, gint idx, GError **err)
-{
- if (!OSINFO_IS_DEVICELIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_DEVICELIST);
- return NULL;
- }
-
- OsinfoList *selfAsList = OSINFO_LIST (self);
- OsinfoEntity *entity = osinfo_list_get_nth(selfAsList, idx);
- return OSINFO_DEVICE (entity);
-}
-
-OsinfoDeviceList *osinfo_device_list_filter(OsinfoDeviceList *self, OsinfoFilter *filter, GError **err)
-{
- if (!OSINFO_IS_DEVICELIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_DEVICELIST);
- return NULL;
- }
-
- if (filter && !OSINFO_IS_FILTER(filter)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_FILTER);
- return NULL;
- }
-
- // For each element in self, if passes filter, add to new list.
- OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-
- __osinfo_list_filter(OSINFO_LIST (self), OSINFO_LIST (newList), filter);
- return newList;
-}
-
-OsinfoDeviceList *osinfo_device_list_intersect(OsinfoDeviceList *self, OsinfoDeviceList *otherList, GError **err)
-{
- if (!OSINFO_IS_DEVICELIST(self) || !OSINFO_IS_DEVICELIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_DEVICELIST);
- return NULL;
- }
-
- OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-
- int ret;
-
- ret = __osinfo_list_intersect(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
-
-OsinfoDeviceList *osinfo_device_list_union(OsinfoDeviceList *self, OsinfoDeviceList *otherList, GError **err)
-{
- if (!OSINFO_IS_DEVICELIST(self) || !OSINFO_IS_DEVICELIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_DEVICELIST);
- return NULL;
- }
-
- OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-
- int ret;
- ret = __osinfo_list_union(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
diff --git a/osinfo/osinfo_devicelist.h b/osinfo/osinfo_devicelist.h
index 5075fa4..e3aaeca 100644
--- a/osinfo/osinfo_devicelist.h
+++ b/osinfo/osinfo_devicelist.h
@@ -44,9 +44,4 @@ struct _OsinfoDeviceListClass
GType osinfo_devicelist_get_type(void);
-OsinfoDeviceList *osinfo_device_list_filter(OsinfoDeviceList *self, OsinfoFilter *filter, GError **err);
-OsinfoDevice *osinfo_device_list_get_nth(OsinfoDeviceList *self, gint idx, GError **err);
-OsinfoDeviceList *osinfo_device_list_intersect(OsinfoDeviceList *self, OsinfoDeviceList *otherDeviceList, GError **err);
-OsinfoDeviceList *osinfo_device_list_union(OsinfoDeviceList *self, OsinfoDeviceList *otherDeviceList, GError **err);
-
#endif /* __OSINFO_DEVICELIST_H__ */
diff --git a/osinfo/osinfo_hypervisorlist.c b/osinfo/osinfo_hypervisorlist.c
index b2aa511..df55b0e 100644
--- a/osinfo/osinfo_hypervisorlist.c
+++ b/osinfo/osinfo_hypervisorlist.c
@@ -35,75 +35,3 @@ osinfo_hypervisorlist_init (OsinfoHypervisorList *self)
self->priv = priv = OSINFO_HYPERVISORLIST_GET_PRIVATE(self);
}
-
-OsinfoHypervisor *osinfo_hypervisor_list_get_nth(OsinfoHypervisorList *self, gint idx, GError **err)
-{
- if (!OSINFO_IS_HYPERVISORLIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_HYPERVISORLIST);
- return NULL;
- }
-
- OsinfoList *selfAsList = OSINFO_LIST (self);
- OsinfoEntity *entity = osinfo_list_get_nth(selfAsList, idx);
- return OSINFO_HYPERVISOR (entity);
-}
-
-OsinfoHypervisorList *osinfo_hypervisor_list_filter(OsinfoHypervisorList *self, OsinfoFilter *filter, GError **err)
-{
- if (!OSINFO_IS_HYPERVISORLIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_HYPERVISORLIST);
- return NULL;
- }
-
- if (filter && !OSINFO_IS_FILTER(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_FILTER);
- return NULL;
- }
-
- // For each element in self, if passes filter, add to new list.
- OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-
- __osinfo_list_filter(OSINFO_LIST (self), OSINFO_LIST (newList), filter);
- return newList;
-}
-
-OsinfoHypervisorList *osinfo_hypervisor_list_intersect(OsinfoHypervisorList *self, OsinfoHypervisorList *otherList, GError **err)
-{
- if (!OSINFO_IS_HYPERVISORLIST(self) || !OSINFO_IS_HYPERVISORLIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_HYPERVISORLIST);
- return NULL;
- }
-
- OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-
- int ret;
-
- ret = __osinfo_list_intersect(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
-
-OsinfoHypervisorList *osinfo_hypervisor_list_union(OsinfoHypervisorList *self, OsinfoHypervisorList *otherList, GError **err)
-{
- if (!OSINFO_IS_HYPERVISORLIST(self) || !OSINFO_IS_HYPERVISORLIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_HYPERVISORLIST);
- return NULL;
- }
-
- OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-
- int ret;
- ret = __osinfo_list_union(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
diff --git a/osinfo/osinfo_hypervisorlist.h b/osinfo/osinfo_hypervisorlist.h
index 482314e..8b5460c 100644
--- a/osinfo/osinfo_hypervisorlist.h
+++ b/osinfo/osinfo_hypervisorlist.h
@@ -44,9 +44,4 @@ struct _OsinfoHypervisorListClass
GType osinfo_hypervisorlist_get_type(void);
-OsinfoHypervisorList *osinfo_hypervisor_list_filter(OsinfoHypervisorList *self, OsinfoFilter *filter, GError **err);
-OsinfoHypervisor *osinfo_hypervisor_list_get_nth(OsinfoHypervisorList *self, gint idx, GError **err);
-OsinfoHypervisorList *osinfo_hypervisor_list_intersect(OsinfoHypervisorList *self, OsinfoHypervisorList *otherHypervisorList, GError **err);
-OsinfoHypervisorList *osinfo_hypervisor_list_union(OsinfoHypervisorList *self, OsinfoHypervisorList *otherHypervisorList, GError **err);
-
#endif /* __OSINFO_HYPERVISORLIST_H__ */
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index 30df4cb..a878824 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -41,6 +41,33 @@ osinfo_list_init (OsinfoList *self)
self->priv->array = g_ptr_array_new();
}
+OsinfoList *osinfo_list_new(void)
+{
+ return g_object_new(OSINFO_TYPE_LIST, NULL);
+}
+
+OsinfoList *osinfo_list_new_filtered(OsinfoList *source, OsinfoFilter *filter)
+{
+ OsinfoList *newList = osinfo_list_new();
+ osinfo_list_add_filtered(newList, source, filter);
+ return newList;
+}
+
+OsinfoList *osinfo_list_new_intersection(OsinfoList *sourceOne, OsinfoList *sourceTwo)
+{
+ OsinfoList *newList = osinfo_list_new();
+ osinfo_list_add_intersection(newList, sourceOne, sourceTwo);
+ return newList;
+}
+
+OsinfoList *osinfo_list_new_union(OsinfoList *sourceOne, OsinfoList *sourceTwo)
+{
+ OsinfoList *newList = osinfo_list_new();
+ osinfo_list_add_union(newList, sourceOne, sourceTwo);
+ return newList;
+}
+
+
gint osinfo_list_get_length(OsinfoList *self)
{
return self->priv->array->len;
@@ -56,37 +83,20 @@ void osinfo_list_add(OsinfoList *self, OsinfoEntity *entity)
g_ptr_array_add(self->priv->array, entity);
}
-void __osinfo_list_filter(OsinfoList *src, OsinfoList *dst, OsinfoFilter *filter)
+
+void osinfo_list_add_filtered(OsinfoList *self, OsinfoList *source, OsinfoFilter *filter)
{
int i, len;
- len = osinfo_list_get_length(src);
+ len = osinfo_list_get_length(source);
for (i = 0; i < len; i++) {
- OsinfoEntity *entity = osinfo_list_get_nth(src, i);
+ OsinfoEntity *entity = osinfo_list_get_nth(source, i);
if (__osinfoEntityPassesFilter(filter, entity))
- osinfo_list_add(dst, entity);
+ osinfo_list_add(self, entity);
}
}
-OsinfoList *osinfo_list_filter(OsinfoList *self, OsinfoFilter *filter, GError **err)
-{
- if (!OSINFO_IS_LIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_LIST);
- return NULL;
- }
-
- if (filter && !OSINFO_IS_FILTER(filter)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_FILTER);
- return NULL;
- }
-
- // For each element in self, if passes filter, add to new list.
- OsinfoList *newList = g_object_new(OSINFO_TYPE_LIST, NULL);
-
- __osinfo_list_filter(self, newList, filter);
- return newList;
-}
-int __osinfo_list_intersect(OsinfoList *src1, OsinfoList *src2, OsinfoList *dst)
+void osinfo_list_add_intersection(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *sourceTwo)
{
int i, len;
@@ -95,97 +105,58 @@ int __osinfo_list_intersect(OsinfoList *src1, OsinfoList *src2, OsinfoList *dst)
GTree *newSet = g_tree_new(__osinfoStringCompareBase);
// Add all from otherList to otherSet
- len = osinfo_list_get_length(src2);
+ len = osinfo_list_get_length(sourceTwo);
for (i = 0; i < len; i++) {
- OsinfoEntity *entity = osinfo_list_get_nth(src2, i);
+ OsinfoEntity *entity = osinfo_list_get_nth(sourceTwo, i);
gchar *id = entity->priv->id;
g_tree_insert(otherSet, id, entity);
}
// If other contains entity, and new list does not, add to new list
- len = osinfo_list_get_length(src1);
+ len = osinfo_list_get_length(sourceOne);
for (i = 0; i < len; i++) {
- OsinfoEntity *entity = osinfo_list_get_nth(src1, i);
+ OsinfoEntity *entity = osinfo_list_get_nth(sourceOne, i);
gchar *id = entity->priv->id;
if (g_tree_lookup(otherSet, entity->priv->id) &&
!g_tree_lookup(newSet, entity->priv->id)) {
g_tree_insert(newSet, id, entity);
- osinfo_list_add(dst, entity);
+ osinfo_list_add(self, entity);
}
}
g_tree_destroy(otherSet);
g_tree_destroy(newSet);
- return 0;
}
-OsinfoList *osinfo_list_intersect(OsinfoList *self, OsinfoList *otherList, GError **err)
-{
- if (!OSINFO_IS_LIST(self) || !OSINFO_IS_LIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_LIST);
- return NULL;
- }
-
- OsinfoList *newList = g_object_new(OSINFO_TYPE_LIST, NULL);
- int ret;
-
- ret = __osinfo_list_intersect(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
-int __osinfo_list_union(OsinfoList *src1, OsinfoList *src2, OsinfoList *dst)
+void osinfo_list_add_union(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *sourceTwo)
{
// Make set version of new list
GTree *newSet = g_tree_new(__osinfoStringCompareBase);
// Add all from other list to new list
int i, len;
- len = osinfo_list_get_length(src2);
+ len = osinfo_list_get_length(sourceTwo);
for (i = 0; i < len; i++) {
- OsinfoEntity *entity = osinfo_list_get_nth(src2, i);
+ OsinfoEntity *entity = osinfo_list_get_nth(sourceTwo, i);
gchar *id = entity->priv->id;
- osinfo_list_add(dst, entity);
+ osinfo_list_add(self, entity);
g_tree_insert(newSet, id, entity);
}
// Add remaining elements from this list to new list
- len = osinfo_list_get_length(src1);
+ len = osinfo_list_get_length(sourceOne);
for (i = 0; i < len; i++) {
- OsinfoEntity *entity = osinfo_list_get_nth(src1, i);
+ OsinfoEntity *entity = osinfo_list_get_nth(sourceOne, i);
gchar *id = entity->priv->id;
// If new list does not contain element, add to new list
if (!g_tree_lookup(newSet, id)) {
- osinfo_list_add(dst, entity);
+ osinfo_list_add(self, entity);
g_tree_insert(newSet, id, entity);
}
}
g_tree_destroy(newSet);
- return 0;
}
-OsinfoList *osinfo_list_union(OsinfoList *self, OsinfoList *otherList, GError **err)
-{
- if (!OSINFO_IS_LIST(self) || !OSINFO_IS_LIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_LIST);
- return NULL;
- }
-
- OsinfoList *newList = g_object_new(OSINFO_TYPE_LIST, NULL);
- int ret;
- ret = __osinfo_list_union(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
diff --git a/osinfo/osinfo_list.h b/osinfo/osinfo_list.h
index 4e9b088..21598ac 100644
--- a/osinfo/osinfo_list.h
+++ b/osinfo/osinfo_list.h
@@ -40,13 +40,19 @@ struct _OsinfoListClass
/* class members */
};
+OsinfoList *osinfo_list_new(void);
+OsinfoList *osinfo_list_new_filtered(OsinfoList *source, OsinfoFilter *filter);
+OsinfoList *osinfo_list_new_intersection(OsinfoList *sourceOne, OsinfoList *sourceTwo);
+OsinfoList *osinfo_list_new_union(OsinfoList *sourceOne, OsinfoList *sourceTwo);
+
GType osinfo_list_get_type(void);
gint osinfo_list_get_length(OsinfoList *self);
-void osinfo_list_add(OsinfoList *self, OsinfoEntity *entity);
-OsinfoList *osinfo_list_filter(OsinfoList *self, OsinfoFilter *filter, GError **err);
OsinfoEntity *osinfo_list_get_nth(OsinfoList *self, gint idx);
-OsinfoList *osinfo_list_intersect(OsinfoList *self, OsinfoList *otherList, GError **err);
-OsinfoList *osinfo_list_union(OsinfoList *self, OsinfoList *otherList, GError **err);
+
+void osinfo_list_add(OsinfoList *self, OsinfoEntity *entity);
+void osinfo_list_add_filtered(OsinfoList *self, OsinfoList *source, OsinfoFilter *filter);
+void osinfo_list_add_intersection(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *sourceTwo);
+void osinfo_list_add_union(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *sourceTwo);
#endif /* __OSINFO_LIST_H__ */
diff --git a/osinfo/osinfo_oslist.c b/osinfo/osinfo_oslist.c
index 5f28a3b..c861fd8 100644
--- a/osinfo/osinfo_oslist.c
+++ b/osinfo/osinfo_oslist.c
@@ -36,74 +36,3 @@ osinfo_oslist_init (OsinfoOsList *self)
}
-OsinfoOs *osinfo_os_list_get_nth(OsinfoOsList *self, gint idx, GError **err)
-{
- if (!OSINFO_IS_OSLIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_OSLIST);
- return NULL;
- }
-
- OsinfoList *selfAsList = OSINFO_LIST (self);
- OsinfoEntity *entity = osinfo_list_get_nth(selfAsList, idx);
- return OSINFO_OS (entity);
-}
-
-OsinfoOsList *osinfo_os_list_filter(OsinfoOsList *self, OsinfoFilter *filter, GError **err)
-{
- if (!OSINFO_IS_OSLIST(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_OSLIST);
- return NULL;
- }
-
- if (filter && !OSINFO_IS_FILTER(self)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_FILTER);
- return NULL;
- }
-
- // For each element in self, if passes filter, add to new list.
- OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-
- __osinfo_list_filter(OSINFO_LIST (self), OSINFO_LIST (newList), filter);
- return newList;
-}
-
-OsinfoOsList *osinfo_os_list_intersect(OsinfoOsList *self, OsinfoOsList *otherList, GError **err)
-{
- if (!OSINFO_IS_OSLIST(self) || !OSINFO_IS_OSLIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_OSLIST);
- return NULL;
- }
-
- OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-
- int ret;
-
- ret = __osinfo_list_intersect(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
-
-OsinfoOsList *osinfo_os_list_union(OsinfoOsList *self, OsinfoOsList *otherList, GError **err)
-{
- if (!OSINFO_IS_OSLIST(self) || !OSINFO_IS_OSLIST(otherList)) {
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -EINVAL, OSINFO_OBJ_NOT_OSLIST);
- return NULL;
- }
-
- OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-
- int ret;
- ret = __osinfo_list_union(self, otherList, newList);
- if (ret != 0) {
- g_object_unref(newList);
- g_set_error_literal(err, g_quark_from_static_string("libosinfo"), ret, __osinfoErrorToString(ret));
- return NULL;
- }
-
- return newList;
-}
diff --git a/osinfo/osinfo_oslist.h b/osinfo/osinfo_oslist.h
index e95862e..5a2c81d 100644
--- a/osinfo/osinfo_oslist.h
+++ b/osinfo/osinfo_oslist.h
@@ -45,9 +45,4 @@ struct _OsinfoOsListClass
GType osinfo_oslist_get_type(void);
-OsinfoOsList *osinfo_os_list_filter(OsinfoOsList *self, OsinfoFilter *filter, GError **err);
-OsinfoOs *osinfo_os_list_get_nth(OsinfoOsList *self, gint idx, GError **err);
-OsinfoOsList *osinfo_os_list_intersect(OsinfoOsList *self, OsinfoOsList *otherOsList, GError **err);
-OsinfoOsList *osinfo_os_list_union(OsinfoOsList *self, OsinfoOsList *otherOsList, GError **err);
-
#endif /* __OSINFO_OSLIST_H__ */
--
1.7.2.1
More information about the virt-tools-list
mailing list