[virt-tools-list] [libosinfo 1/4] Add osinfo_list_new_*

Christophe Fergeau cfergeau at redhat.com
Fri Dec 7 09:34:59 UTC 2012


Currently, every class inheriting from OsinfoList reimplements
_new_copy, _new_filtered, _new_intersection and _new_union. This
commit adds generic implementations of these methodes in OsinfoList
which will allow us to deprecate all the other implementations.
---
 osinfo/libosinfo.syms |  10 +++--
 osinfo/osinfo_list.c  | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++
 osinfo/osinfo_list.h  |   5 +++
 3 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index fe1ac16..6848eb3 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -363,11 +363,15 @@ LIBOSINFO_0.2.2 {
 	osinfo_install_config_get_post_install_drivers_location;
 
 	osinfo_install_script_get_avatar_format;
-	osinfo_install_script_get_path_format;
-
-	osinfo_install_script_get_product_key_format;
 	osinfo_install_script_get_can_pre_install_drivers;
 	osinfo_install_script_get_can_post_install_drivers;
+	osinfo_install_script_get_path_format;
+	osinfo_install_script_get_product_key_format;
+
+	osinfo_list_new_copy;
+	osinfo_list_new_filtered;
+	osinfo_list_new_intersection;
+	osinfo_list_new_union;
 
 	osinfo_os_get_device_drivers;
 	osinfo_os_add_device_driver;
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index d161f10..afc04d3 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -19,6 +19,7 @@
  *
  * Authors:
  *   Arjun Roy <arroy at redhat.com>
+ *   Christophe Fergeau <cfergeau at redhat.com>
  *   Daniel P. Berrange <berrange at redhat.com>
  */
 
@@ -382,6 +383,111 @@ void osinfo_list_add_all(OsinfoList *list, OsinfoList *source)
     }
 }
 
+/*
+ * Creates a list of the same type as sourceOne and sourceTwo after
+ * checking they are the same type. The created list elements are
+ * of the same type as the elements of sourceOne and sourceTwo
+ */
+static OsinfoList *osinfo_list_new_same(OsinfoList *sourceOne,
+                                        OsinfoList *sourceTwo)
+{
+    GType typeOne = G_OBJECT_TYPE(sourceOne);
+
+    if (sourceTwo != NULL) {
+        GType typeTwo = G_OBJECT_TYPE(sourceTwo);
+
+        g_return_val_if_fail(typeOne == typeTwo, NULL);
+        g_return_val_if_fail(OSINFO_IS_LIST(sourceTwo), NULL);
+    }
+
+    g_return_val_if_fail(OSINFO_IS_LIST(sourceOne), NULL);
+
+    return g_object_new(typeOne,
+                        "element-type",
+                        sourceOne->priv->elementType,
+                        NULL);
+}
+
+/**
+ * osinfo_list_new_copy:
+ * @source: the list to copy
+ *
+ * Construct a new list that is filled with elements from @source
+ *
+ * Returns: (transfer full): a copy of the list
+ */
+OsinfoList *osinfo_list_new_copy(OsinfoList *source)
+{
+    OsinfoList *newList = osinfo_list_new_same(source, NULL);
+    g_return_val_if_fail(newList != NULL, NULL);
+    osinfo_list_add_all(OSINFO_LIST(newList),
+                        OSINFO_LIST(source));
+    return newList;
+}
+
+/**
+ * osinfo_list_new_filtered:
+ * @source: the list to copy
+ * @filter: the filter to apply
+ *
+ * Construct a new list that is filled with elements from @source that
+ * match @filter
+ *
+ * Returns: (transfer full): a filtered copy of the list
+ */
+OsinfoList *osinfo_list_new_filtered(OsinfoList *source, OsinfoFilter *filter)
+{
+    OsinfoList *newList = osinfo_list_new_same(source, NULL);
+    g_return_val_if_fail(newList != NULL, NULL);
+    osinfo_list_add_filtered(OSINFO_LIST(newList),
+                             OSINFO_LIST(source),
+                             filter);
+    return newList;
+}
+
+/**
+ * osinfo_list_new_intersection:
+ * @sourceOne: the first list to copy
+ * @sourceTwo: the second list to copy
+ *
+ * Construct a new list that is filled with only the
+ * s that are present in both @sourceOne and @sourceTwo.
+ *
+ * Returns: (transfer full): an intersection of the two lists
+ */
+OsinfoList *osinfo_list_new_intersection(OsinfoList *sourceOne,
+                                         OsinfoList *sourceTwo)
+{
+    OsinfoList *newList = osinfo_list_new_same(sourceOne, sourceTwo);
+    g_return_val_if_fail(newList != NULL, NULL);
+    osinfo_list_add_intersection(OSINFO_LIST(newList),
+                                 OSINFO_LIST(sourceOne),
+                                 OSINFO_LIST(sourceTwo));
+    return newList;
+}
+
+/**
+ * osinfo_new_union:
+ * @sourceOne: the first list to copy
+ * @sourceTwo: the second list to copy
+ *
+ * Construct a new list that is filled with all the that are present in
+ * either @sourceOne and @sourceTwo. @sourceOne and @sourceTwo must be of
+ * the same type.
+ *
+ * Returns: (transfer full): a union of the two lists
+ */
+OsinfoList *osinfo_list_new_union(OsinfoList *sourceOne,
+                                  OsinfoList *sourceTwo)
+{
+    OsinfoList *newList = osinfo_list_new_same(sourceOne, sourceTwo);
+    g_return_val_if_fail(newList != NULL, NULL);
+    osinfo_list_add_union(OSINFO_LIST(newList),
+                          OSINFO_LIST(sourceOne),
+                          OSINFO_LIST(sourceTwo));
+    return newList;
+}
+
 
 /*
  * Local variables:
diff --git a/osinfo/osinfo_list.h b/osinfo/osinfo_list.h
index 8fc1202..fd60b1d 100644
--- a/osinfo/osinfo_list.h
+++ b/osinfo/osinfo_list.h
@@ -75,6 +75,11 @@ void osinfo_list_add_intersection(OsinfoList *list, OsinfoList *sourceOne, Osinf
 void osinfo_list_add_union(OsinfoList *list, OsinfoList *sourceOne, OsinfoList *sourceTwo);
 void osinfo_list_add_all(OsinfoList *list, OsinfoList *source);
 
+OsinfoList *osinfo_list_new_copy(OsinfoList *source);
+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);
+
 #endif /* __OSINFO_LIST_H__ */
 /*
  * Local variables:
-- 
1.8.0.1




More information about the virt-tools-list mailing list