[virt-tools-list] [PATCH 18/47] Add constructors for all objects. Remove constructor for abstract list object
Daniel P. Berrange
berrange at redhat.com
Wed Aug 25 19:37:13 UTC 2010
While using g_object_new() is functionally fine, it is not
type-safe/checked. It should generally only be used by
bindings for non-C language. Add explicit constructors for
every object and update all callers of g_object_new to use
the new constructors.
* osinfo/osinfo.h, osinfo/osinfo_dataread.c,
osinfo/osinfo_db.c, osinfo/osinfo_db.h,
osinfo/osinfo_device.c, osinfo/osinfo_device.h,
osinfo/osinfo_devicelist.c, osinfo/osinfo_devicelist.h,
osinfo/osinfo_hypervisor.c, osinfo/osinfo_hypervisor.h,
osinfo/osinfo_hypervisorlist.c, osinfo/osinfo_hypervisorlist.h,
osinfo/osinfo_list.c, osinfo/osinfo_list.h,
osinfo/osinfo_os.c, osinfo/osinfo_os.h,
osinfo/osinfo_oslist.c, osinfo/osinfo_oslist.h: Add
constructors for all objects & use them
---
osinfo/osinfo.h | 10 +++++-----
osinfo/osinfo_dataread.c | 7 +++----
osinfo/osinfo_db.c | 8 ++++----
osinfo/osinfo_db.h | 4 ----
osinfo/osinfo_device.c | 7 +++++++
osinfo/osinfo_device.h | 2 ++
osinfo/osinfo_devicelist.c | 32 ++++++++++++++++++++++++++++++++
osinfo/osinfo_devicelist.h | 9 +++++++--
osinfo/osinfo_hypervisor.c | 10 +++++++++-
osinfo/osinfo_hypervisor.h | 2 ++
osinfo/osinfo_hypervisorlist.c | 34 ++++++++++++++++++++++++++++++++++
osinfo/osinfo_hypervisorlist.h | 9 +++++++--
osinfo/osinfo_list.c | 27 ---------------------------
osinfo/osinfo_list.h | 5 -----
osinfo/osinfo_os.c | 12 ++++++++++--
osinfo/osinfo_os.h | 2 ++
osinfo/osinfo_oslist.c | 33 +++++++++++++++++++++++++++++++++
osinfo/osinfo_oslist.h | 9 +++++++--
18 files changed, 164 insertions(+), 58 deletions(-)
diff --git a/osinfo/osinfo.h b/osinfo/osinfo.h
index 7591487..dc64722 100644
--- a/osinfo/osinfo.h
+++ b/osinfo/osinfo.h
@@ -3,15 +3,15 @@
#include <glib-object.h>
#include <osinfo/osinfo_common.h>
-#include <osinfo/osinfo_db.h>
#include <osinfo/osinfo_entity.h>
-#include <osinfo/osinfo_device.h>
-#include <osinfo/osinfo_os.h>
-#include <osinfo/osinfo_hypervisor.h>
-#include <osinfo/osinfo_filter.h>
#include <osinfo/osinfo_list.h>
#include <osinfo/osinfo_devicelist.h>
#include <osinfo/osinfo_oslist.h>
#include <osinfo/osinfo_hypervisorlist.h>
+#include <osinfo/osinfo_device.h>
+#include <osinfo/osinfo_os.h>
+#include <osinfo/osinfo_hypervisor.h>
+#include <osinfo/osinfo_filter.h>
+#include <osinfo/osinfo_db.h>
#endif
diff --git a/osinfo/osinfo_dataread.c b/osinfo/osinfo_dataread.c
index 8e4183c..4b5394f 100644
--- a/osinfo/osinfo_dataread.c
+++ b/osinfo/osinfo_dataread.c
@@ -433,7 +433,7 @@ static int __osinfoProcessOs(OsinfoDb *db,
if (!id)
return -EINVAL;
- os = g_object_new(OSINFO_TYPE_OS, "id", id, NULL);
+ os = osinfo_os_new(id);
free(id);
if (empty)
@@ -551,8 +551,7 @@ static int __osinfoProcessHypervisor(OsinfoDb *db,
if (!id)
return -EINVAL;
-
- hv = g_object_new(OSINFO_TYPE_HYPERVISOR, "id", id, NULL);
+ hv = osinfo_hypervisor_new(id);
free(id);
if (empty)
@@ -659,7 +658,7 @@ static int __osinfoProcessDevice(OsinfoDb *db,
if (!id)
return -EINVAL;
- dev = g_object_new(OSINFO_TYPE_DEVICE, "id", id, NULL);
+ dev = osinfo_device_new(id);
free(id);
if (empty)
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index f509b02..51e4030 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -209,7 +209,7 @@ OsinfoOsList *osinfo_db_get_os_list(OsinfoDb *self, OsinfoFilter *filter)
g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);
// Create list
- OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
+ OsinfoOsList *newList = osinfo_oslist_new();
osinfo_db_populate_list(self->priv->oses, OSINFO_LIST (newList), filter);
return newList;
}
@@ -220,7 +220,7 @@ OsinfoHypervisorList *osinfo_db_get_hypervisor_list(OsinfoDb *self, OsinfoFilter
g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);
// Create list
- OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
+ OsinfoHypervisorList *newList = osinfo_hypervisorlist_new();
osinfo_db_populate_list(self->priv->hypervisors, OSINFO_LIST (newList), filter);
return newList;
}
@@ -231,7 +231,7 @@ OsinfoDeviceList *osinfo_db_get_device_list(OsinfoDb *self, OsinfoFilter *filter
g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);
// Create list
- OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
+ OsinfoDeviceList *newList = osinfo_devicelist_new();
osinfo_db_populate_list(self->priv->devices, OSINFO_LIST (newList), filter);
return newList;
}
@@ -339,7 +339,7 @@ OsinfoOsList *osinfo_db_unique_values_for_os_relationship(OsinfoDb *self, osinfo
g_return_val_if_fail(OSINFO_IS_DB(self), NULL);
// Create list
- OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
+ OsinfoOsList *newList = osinfo_oslist_new();
struct __osinfoOsCheckRelationshipArgs args = {OSINFO_LIST (newList), relshp};
diff --git a/osinfo/osinfo_db.h b/osinfo/osinfo_db.h
index d50d0ee..61bf13e 100644
--- a/osinfo/osinfo_db.h
+++ b/osinfo/osinfo_db.h
@@ -8,10 +8,6 @@
#ifndef __OSINFO_DB_H__
#define __OSINFO_DB_H__
-#include "osinfo_devicelist.h"
-#include "osinfo_hypervisorlist.h"
-#include "osinfo_oslist.h"
-
/*
* Type macros.
*/
diff --git a/osinfo/osinfo_device.c b/osinfo/osinfo_device.c
index bddc5e9..bcb2fed 100644
--- a/osinfo/osinfo_device.c
+++ b/osinfo/osinfo_device.c
@@ -32,6 +32,13 @@ osinfo_device_init (OsinfoDevice *self)
self->priv = priv = OSINFO_DEVICE_GET_PRIVATE(self);
}
+OsinfoDevice *osinfo_device_new(const gchar *id)
+{
+ return g_object_new(OSINFO_TYPE_DEVICE,
+ "id", id,
+ NULL);
+}
+
gchar *osinfo_device_get_driver(OsinfoDevice *self,
gchar *devType,
OsinfoOs *os,
diff --git a/osinfo/osinfo_device.h b/osinfo/osinfo_device.h
index d218229..cdd8fc3 100644
--- a/osinfo/osinfo_device.h
+++ b/osinfo/osinfo_device.h
@@ -46,6 +46,8 @@ struct _OsinfoDeviceClass
GType osinfo_device_get_type(void);
+OsinfoDevice *osinfo_device_new(const gchar *id);
+
gchar *osinfo_device_get_driver(OsinfoDevice *self, gchar *devType, OsinfoOs *os, OsinfoHypervisor *hv);
#endif /* __OSINFO_DEVICE_H__ */
diff --git a/osinfo/osinfo_devicelist.c b/osinfo/osinfo_devicelist.c
index f8a4661..87280d8 100644
--- a/osinfo/osinfo_devicelist.c
+++ b/osinfo/osinfo_devicelist.c
@@ -36,3 +36,35 @@ osinfo_devicelist_init (OsinfoDeviceList *self)
}
+OsinfoDeviceList *osinfo_devicelist_new(void)
+{
+ return g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
+}
+
+OsinfoDeviceList *osinfo_devicelist_new_filtered(OsinfoDeviceList *source, OsinfoFilter *filter)
+{
+ OsinfoDeviceList *newList = osinfo_devicelist_new();
+ osinfo_list_add_filtered(OSINFO_LIST(newList),
+ OSINFO_LIST(source),
+ filter);
+ return newList;
+}
+
+OsinfoDeviceList *osinfo_devicelist_new_intersection(OsinfoDeviceList *sourceOne, OsinfoDeviceList *sourceTwo)
+{
+ OsinfoDeviceList *newList = osinfo_devicelist_new();
+ osinfo_list_add_intersection(OSINFO_LIST(newList),
+ OSINFO_LIST(sourceOne),
+ OSINFO_LIST(sourceTwo));
+ return newList;
+}
+
+OsinfoDeviceList *osinfo_devicelist_new_union(OsinfoDeviceList *sourceOne, OsinfoDeviceList *sourceTwo)
+{
+ OsinfoDeviceList *newList = osinfo_devicelist_new();
+ osinfo_list_add_union(OSINFO_LIST(newList),
+ OSINFO_LIST(sourceOne),
+ OSINFO_LIST(sourceTwo));
+ return newList;
+}
+
diff --git a/osinfo/osinfo_devicelist.h b/osinfo/osinfo_devicelist.h
index e3aaeca..e5cb25d 100644
--- a/osinfo/osinfo_devicelist.h
+++ b/osinfo/osinfo_devicelist.h
@@ -26,7 +26,7 @@ typedef struct _OsinfoDeviceListPrivate OsinfoDeviceListPrivate;
/* object */
struct _OsinfoDeviceList
{
- GObject parent_instance;
+ OsinfoList parent_instance;
/* public */
@@ -37,11 +37,16 @@ struct _OsinfoDeviceList
/* class */
struct _OsinfoDeviceListClass
{
- GObjectClass parent_class;
+ OsinfoListClass parent_class;
/* class members */
};
GType osinfo_devicelist_get_type(void);
+OsinfoDeviceList *osinfo_devicelist_new(void);
+OsinfoDeviceList *osinfo_devicelist_new_filtered(OsinfoDeviceList *source, OsinfoFilter *filter);
+OsinfoDeviceList *osinfo_devicelist_new_intersection(OsinfoDeviceList *sourceOne, OsinfoDeviceList *sourceTwo);
+OsinfoDeviceList *osinfo_devicelist_new_union(OsinfoDeviceList *sourceOne, OsinfoDeviceList *sourceTwo);
+
#endif /* __OSINFO_DEVICELIST_H__ */
diff --git a/osinfo/osinfo_hypervisor.c b/osinfo/osinfo_hypervisor.c
index 2f6d0c9..0f8849a 100644
--- a/osinfo/osinfo_hypervisor.c
+++ b/osinfo/osinfo_hypervisor.c
@@ -38,6 +38,14 @@ osinfo_hypervisor_init (OsinfoHypervisor *self)
self->priv->sectionsAsList = g_tree_new_full(__osinfoStringCompare, NULL, g_free, __osinfoFreePtrArray);
}
+OsinfoHypervisor *osinfo_hypervisor_new(const gchar *id)
+{
+ return g_object_new(OSINFO_TYPE_HYPERVISOR,
+ "id", id,
+ NULL);
+}
+
+
int __osinfoAddDeviceToSectionHv(OsinfoHypervisor *self, gchar *section, gchar *id, gchar *driver)
{
if( !OSINFO_IS_HYPERVISOR(self) || !section || !id || !driver)
@@ -72,7 +80,7 @@ OsinfoDeviceList *osinfo_hypervisor_get_devices_by_type(OsinfoHypervisor *self,
g_return_val_if_fail(devType != NULL, NULL);
// Create our device list
- OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
+ OsinfoDeviceList *newList = osinfo_devicelist_new();
// If section does not exist, return empty list
GPtrArray *sectionList = NULL;
diff --git a/osinfo/osinfo_hypervisor.h b/osinfo/osinfo_hypervisor.h
index 658b6b9..d8f893f 100644
--- a/osinfo/osinfo_hypervisor.h
+++ b/osinfo/osinfo_hypervisor.h
@@ -48,6 +48,8 @@ struct _OsinfoHypervisorClass
GType osinfo_hypervisor_get_type(void);
+OsinfoHypervisor *osinfo_hypervisor_new(const gchar *id);
+
GPtrArray *osinfo_hypervisor_get_device_types(OsinfoHypervisor *self);
OsinfoDeviceList *osinfo_hypervisor_get_devices_by_type(OsinfoHypervisor *self, gchar *devType, OsinfoFilter *filter);
diff --git a/osinfo/osinfo_hypervisorlist.c b/osinfo/osinfo_hypervisorlist.c
index df55b0e..2d6a5b2 100644
--- a/osinfo/osinfo_hypervisorlist.c
+++ b/osinfo/osinfo_hypervisorlist.c
@@ -35,3 +35,37 @@ osinfo_hypervisorlist_init (OsinfoHypervisorList *self)
self->priv = priv = OSINFO_HYPERVISORLIST_GET_PRIVATE(self);
}
+
+
+OsinfoHypervisorList *osinfo_hypervisorlist_new(void)
+{
+ return g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
+}
+
+OsinfoHypervisorList *osinfo_hypervisorlist_new_filtered(OsinfoHypervisorList *source, OsinfoFilter *filter)
+{
+ OsinfoHypervisorList *newList = osinfo_hypervisorlist_new();
+ osinfo_list_add_filtered(OSINFO_LIST(newList),
+ OSINFO_LIST(source),
+ filter);
+ return newList;
+}
+
+OsinfoHypervisorList *osinfo_hypervisorlist_new_intersection(OsinfoHypervisorList *sourceOne, OsinfoHypervisorList *sourceTwo)
+{
+ OsinfoHypervisorList *newList = osinfo_hypervisorlist_new();
+ osinfo_list_add_intersection(OSINFO_LIST(newList),
+ OSINFO_LIST(sourceOne),
+ OSINFO_LIST(sourceTwo));
+ return newList;
+}
+
+OsinfoHypervisorList *osinfo_hypervisorlist_new_union(OsinfoHypervisorList *sourceOne, OsinfoHypervisorList *sourceTwo)
+{
+ OsinfoHypervisorList *newList = osinfo_hypervisorlist_new();
+ osinfo_list_add_union(OSINFO_LIST(newList),
+ OSINFO_LIST(sourceOne),
+ OSINFO_LIST(sourceTwo));
+ return newList;
+}
+
diff --git a/osinfo/osinfo_hypervisorlist.h b/osinfo/osinfo_hypervisorlist.h
index 8b5460c..2487ac2 100644
--- a/osinfo/osinfo_hypervisorlist.h
+++ b/osinfo/osinfo_hypervisorlist.h
@@ -26,7 +26,7 @@ typedef struct _OsinfoHypervisorListPrivate OsinfoHypervisorListPrivate;
/* object */
struct _OsinfoHypervisorList
{
- GObject parent_instance;
+ OsinfoList parent_instance;
/* public */
@@ -37,11 +37,16 @@ struct _OsinfoHypervisorList
/* class */
struct _OsinfoHypervisorListClass
{
- GObjectClass parent_class;
+ OsinfoListClass parent_class;
/* class members */
};
GType osinfo_hypervisorlist_get_type(void);
+OsinfoHypervisorList *osinfo_hypervisorlist_new(void);
+OsinfoHypervisorList *osinfo_hypervisorlist_new_filtered(OsinfoHypervisorList *source, OsinfoFilter *filter);
+OsinfoHypervisorList *osinfo_hypervisorlist_new_intersection(OsinfoHypervisorList *sourceOne, OsinfoHypervisorList *sourceTwo);
+OsinfoHypervisorList *osinfo_hypervisorlist_new_union(OsinfoHypervisorList *sourceOne, OsinfoHypervisorList *sourceTwo);
+
#endif /* __OSINFO_HYPERVISORLIST_H__ */
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index a878824..872f520 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -41,33 +41,6 @@ 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;
diff --git a/osinfo/osinfo_list.h b/osinfo/osinfo_list.h
index 21598ac..4daeea9 100644
--- a/osinfo/osinfo_list.h
+++ b/osinfo/osinfo_list.h
@@ -40,11 +40,6 @@ 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);
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index 51862cb..53be431 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -59,6 +59,14 @@ osinfo_os_init (OsinfoOs *self)
__osinfoFreeHvSection);
}
+OsinfoOs *osinfo_os_new(const gchar *id)
+{
+ return g_object_new(OSINFO_TYPE_OS,
+ "id", id,
+ NULL);
+}
+
+
static int __osinfoAddOsRelationshipByOs(OsinfoOs *self,
gchar *otherOsId,
osinfoRelationship rel,
@@ -262,7 +270,7 @@ OsinfoOsList *osinfo_os_get_related(OsinfoOs *self, osinfoRelationship relshp)
g_return_val_if_fail(OSINFO_IS_OS(self), NULL);
// Create our list
- OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
+ OsinfoOsList *newList = osinfo_oslist_new();
GPtrArray *relatedOses = NULL;
relatedOses = g_tree_lookup(self->priv->relationshipsByType, (gpointer) relshp);
@@ -288,7 +296,7 @@ OsinfoDeviceList *osinfo_os_get_devices(OsinfoOs *self, OsinfoHypervisor *hv, gc
GPtrArray *sectionList = NULL;
// Create our device list
- OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
+ OsinfoDeviceList *newList = osinfo_devicelist_new();
if (hv) {
struct __osinfoHvSection *hvSection = NULL;
diff --git a/osinfo/osinfo_os.h b/osinfo/osinfo_os.h
index 82d50ae..62b5da4 100644
--- a/osinfo/osinfo_os.h
+++ b/osinfo/osinfo_os.h
@@ -50,6 +50,8 @@ struct _OsinfoOsClass
GType osinfo_os_get_type(void);
+OsinfoOs *osinfo_os_new(const gchar *id);
+
OsinfoDevice *osinfo_os_get_preferred_device(OsinfoOs *self, OsinfoHypervisor *hv, gchar *devType, OsinfoFilter *filter);
OsinfoOsList *osinfo_os_get_related(OsinfoOs *self, osinfoRelationship relshp);
OsinfoDeviceList *osinfo_os_get_devices(OsinfoOs *self, OsinfoHypervisor *hv, gchar *devType, OsinfoFilter *filter);
diff --git a/osinfo/osinfo_oslist.c b/osinfo/osinfo_oslist.c
index c861fd8..49f11a1 100644
--- a/osinfo/osinfo_oslist.c
+++ b/osinfo/osinfo_oslist.c
@@ -36,3 +36,36 @@ osinfo_oslist_init (OsinfoOsList *self)
}
+
+OsinfoOsList *osinfo_oslist_new(void)
+{
+ return g_object_new(OSINFO_TYPE_OSLIST, NULL);
+}
+
+OsinfoOsList *osinfo_oslist_new_filtered(OsinfoOsList *source, OsinfoFilter *filter)
+{
+ OsinfoOsList *newList = osinfo_oslist_new();
+ osinfo_list_add_filtered(OSINFO_LIST(newList),
+ OSINFO_LIST(source),
+ filter);
+ return newList;
+}
+
+OsinfoOsList *osinfo_oslist_new_intersection(OsinfoOsList *sourceOne, OsinfoOsList *sourceTwo)
+{
+ OsinfoOsList *newList = osinfo_oslist_new();
+ osinfo_list_add_intersection(OSINFO_LIST(newList),
+ OSINFO_LIST(sourceOne),
+ OSINFO_LIST(sourceTwo));
+ return newList;
+}
+
+OsinfoOsList *osinfo_oslist_new_union(OsinfoOsList *sourceOne, OsinfoOsList *sourceTwo)
+{
+ OsinfoOsList *newList = osinfo_oslist_new();
+ osinfo_list_add_union(OSINFO_LIST(newList),
+ OSINFO_LIST(sourceOne),
+ OSINFO_LIST(sourceTwo));
+ return newList;
+}
+
diff --git a/osinfo/osinfo_oslist.h b/osinfo/osinfo_oslist.h
index 5a2c81d..7adc69f 100644
--- a/osinfo/osinfo_oslist.h
+++ b/osinfo/osinfo_oslist.h
@@ -27,7 +27,7 @@ typedef struct _OsinfoOsListPrivate OsinfoOsListPrivate;
/* object */
struct _OsinfoOsList
{
- GObject parent_instance;
+ OsinfoList parent_instance;
/* public */
@@ -38,11 +38,16 @@ struct _OsinfoOsList
/* class */
struct _OsinfoOsListClass
{
- GObjectClass parent_class;
+ OsinfoListClass parent_class;
/* class members */
};
GType osinfo_oslist_get_type(void);
+OsinfoOsList *osinfo_oslist_new(void);
+OsinfoOsList *osinfo_oslist_new_filtered(OsinfoOsList *source, OsinfoFilter *filter);
+OsinfoOsList *osinfo_oslist_new_intersection(OsinfoOsList *sourceOne, OsinfoOsList *sourceTwo);
+OsinfoOsList *osinfo_oslist_new_union(OsinfoOsList *sourceOne, OsinfoOsList *sourceTwo);
+
#endif /* __OSINFO_OSLIST_H__ */
--
1.7.2.1
More information about the virt-tools-list
mailing list