[libvirt] [libvirt-gconfig PATCHv2 26/32] Add GVirConfigInterfaceNetwork

Christophe Fergeau cfergeau at redhat.com
Mon Nov 21 18:04:23 UTC 2011


Make GVirConfigInterface an abstract base class, and move the
actual code in subclasses. This will make handling of the various
types of network interfaces more understandable and more logical
with respect to the corresponding XML.

--
v2: use g_return_if_fail to test function args for sanity
---
 libvirt-gconfig/Makefile.am                        |    2 +
 .../libvirt-gconfig-interface-network.c            |   94 ++++++++++++++++++++
 .../libvirt-gconfig-interface-network.h            |   67 ++++++++++++++
 libvirt-gconfig/libvirt-gconfig-interface.c        |   28 +-----
 libvirt-gconfig/libvirt-gconfig-interface.h        |    5 -
 libvirt-gconfig/libvirt-gconfig.h                  |    1 +
 libvirt-gconfig/libvirt-gconfig.sym                |    6 +-
 7 files changed, 171 insertions(+), 32 deletions(-)
 create mode 100644 libvirt-gconfig/libvirt-gconfig-interface-network.c
 create mode 100644 libvirt-gconfig/libvirt-gconfig-interface-network.h

diff --git a/libvirt-gconfig/Makefile.am b/libvirt-gconfig/Makefile.am
index 4bc7a21..4318124 100644
--- a/libvirt-gconfig/Makefile.am
+++ b/libvirt-gconfig/Makefile.am
@@ -18,6 +18,7 @@ GCONFIG_HEADER_FILES = \
 			libvirt-gconfig-domain-snapshot.h \
 			libvirt-gconfig-helpers.h \
 			libvirt-gconfig-interface.h \
+			libvirt-gconfig-interface-network.h \
 			libvirt-gconfig-network.h \
 			libvirt-gconfig-network-filter.h \
 			libvirt-gconfig-node-device.h \
@@ -40,6 +41,7 @@ GCONFIG_SOURCE_FILES = \
 			libvirt-gconfig-enum-types.c \
 			libvirt-gconfig-helpers.c \
 			libvirt-gconfig-interface.c \
+			libvirt-gconfig-interface-network.c \
 			libvirt-gconfig-network.c \
 			libvirt-gconfig-network-filter.c \
 			libvirt-gconfig-node-device.c \
diff --git a/libvirt-gconfig/libvirt-gconfig-interface-network.c b/libvirt-gconfig/libvirt-gconfig-interface-network.c
new file mode 100644
index 0000000..6e88cda
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-interface-network.c
@@ -0,0 +1,94 @@
+/*
+ * libvirt-gobject-config-interface_network-network.c: libvirt glib integration
+ *
+ * Copyright (C) 2008 Daniel P. Berrange
+ * Copyright (C) 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Daniel P. Berrange <berrange at redhat.com>
+ * Author: Christophe Fergeau <cfergeau at redhat.com>
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include "libvirt-gconfig/libvirt-gconfig.h"
+#include "libvirt-gconfig/libvirt-gconfig-helpers-private.h"
+#include "libvirt-gconfig/libvirt-gconfig-object-private.h"
+
+extern gboolean debugFlag;
+
+#define DEBUG(fmt, ...) do { if (G_UNLIKELY(debugFlag)) g_debug(fmt, ## __VA_ARGS__); } while (0)
+
+#define GVIR_CONFIG_INTERFACE_NETWORK_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_CONFIG_INTERFACE_NETWORK, GVirConfigInterfaceNetworkPrivate))
+
+struct _GVirConfigInterfaceNetworkPrivate
+{
+    gboolean unused;
+};
+
+G_DEFINE_TYPE(GVirConfigInterfaceNetwork, gvir_config_interface_network, GVIR_TYPE_CONFIG_INTERFACE);
+
+
+static void gvir_config_interface_network_class_init(GVirConfigInterfaceNetworkClass *klass)
+{
+    g_type_class_add_private(klass, sizeof(GVirConfigInterfaceNetworkPrivate));
+}
+
+
+static void gvir_config_interface_network_init(GVirConfigInterfaceNetwork *conn)
+{
+    GVirConfigInterfaceNetworkPrivate *priv;
+
+    DEBUG("Init GVirConfigInterfaceNetwork=%p", conn);
+
+    priv = conn->priv = GVIR_CONFIG_INTERFACE_NETWORK_GET_PRIVATE(conn);
+
+    memset(priv, 0, sizeof(*priv));
+}
+
+
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new(void)
+{
+    xmlDocPtr doc;
+    xmlNodePtr node;
+
+    doc = xmlNewDoc((xmlChar *)"1.0");
+    node= xmlNewDocNode(doc, NULL, (xmlChar *)"interface", NULL);
+    xmlNewProp(doc->children, (xmlChar*)"type", (xmlChar*)"network");
+    xmlDocSetRootElement(doc, node);
+    return GVIR_CONFIG_INTERFACE_NETWORK(g_object_new(GVIR_TYPE_CONFIG_INTERFACE_NETWORK,
+                                         "node", node,
+                                         "schema", DATADIR "/libvirt/schemas/interface.rng",
+                                         NULL));
+}
+
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new_from_xml(const gchar *xml,
+                                                                       GError **error)
+{
+    xmlNodePtr node;
+
+    node = gvir_config_xml_parse(xml, "interface", error);
+    if ((error != NULL) && (*error != NULL))
+        return NULL;
+    xmlNewProp(node, (xmlChar*)"type", (xmlChar*)"network");
+    return GVIR_CONFIG_INTERFACE_NETWORK(g_object_new(GVIR_TYPE_CONFIG_INTERFACE_NETWORK,
+                                                      "node", node,
+                                                      "schema", DATADIR "/libvirt/schemas/interface.rng",
+                                                      NULL));
+}
diff --git a/libvirt-gconfig/libvirt-gconfig-interface-network.h b/libvirt-gconfig/libvirt-gconfig-interface-network.h
new file mode 100644
index 0000000..0c0e542
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-interface-network.h
@@ -0,0 +1,67 @@
+/*
+ * libvirt-gobject-config-interface-network.c: libvirt gobject integration
+ *
+ * Copyright (C) 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Daniel P. Berrange <berrange at redhat.com>
+ * Author: Christophe Fergeau <cfergeau at redhat.com>
+ */
+
+#if !defined(__LIBVIRT_GCONFIG_H__) && !defined(LIBVIRT_GCONFIG_BUILD)
+#error "Only <libvirt-gconfig/libvirt-gconfig.h> can be included directly."
+#endif
+
+#ifndef __LIBVIRT_GCONFIG_INTERFACE_NETWORK_H__
+#define __LIBVIRT_GCONFIG_INTERFACE_NETWORK_H__
+
+G_BEGIN_DECLS
+
+#define GVIR_TYPE_CONFIG_INTERFACE_NETWORK            (gvir_config_interface_network_get_type ())
+#define GVIR_CONFIG_INTERFACE_NETWORK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_CONFIG_INTERFACE, GVirConfigInterfaceNetwork))
+#define GVIR_CONFIG_INTERFACE_NETWORK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GVIR_TYPE_CONFIG_INTERFACE, GVirConfigInterfaceNetworkClass))
+#define GVIR_IS_CONFIG_INTERFACE_NETWORK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GVIR_TYPE_CONFIG_INTERFACE))
+#define GVIR_IS_CONFIG_INTERFACE_NETWORK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GVIR_TYPE_CONFIG_INTERFACE))
+#define GVIR_CONFIG_INTERFACE_NETWORK_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GVIR_TYPE_CONFIG_INTERFACE, GVirConfigInterfaceNetworkClass))
+
+typedef struct _GVirConfigInterfaceNetwork GVirConfigInterfaceNetwork;
+typedef struct _GVirConfigInterfaceNetworkPrivate GVirConfigInterfaceNetworkPrivate;
+typedef struct _GVirConfigInterfaceNetworkClass GVirConfigInterfaceNetworkClass;
+
+struct _GVirConfigInterfaceNetwork
+{
+    GVirConfigInterface parent;
+
+    GVirConfigInterfaceNetworkPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _GVirConfigInterfaceNetworkClass
+{
+    GVirConfigInterfaceClass parent_class;
+
+    gpointer padding[20];
+};
+
+GType gvir_config_interface_network_get_type(void);
+
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new(void);
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new_from_xml(const gchar *xml,
+                                                                       GError **error);
+G_END_DECLS
+
+#endif /* __LIBVIRT_GCONFIG_INTERFACE_NETWORK_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig-interface.c b/libvirt-gconfig/libvirt-gconfig-interface.c
index aadbc96..336d715 100644
--- a/libvirt-gconfig/libvirt-gconfig-interface.c
+++ b/libvirt-gconfig/libvirt-gconfig-interface.c
@@ -26,6 +26,8 @@
 #include <string.h>
 
 #include "libvirt-gconfig/libvirt-gconfig.h"
+#include "libvirt-gconfig/libvirt-gconfig-helpers-private.h"
+#include "libvirt-gconfig/libvirt-gconfig-object-private.h"
 
 extern gboolean debugFlag;
 
@@ -39,7 +41,7 @@ struct _GVirConfigInterfacePrivate
     gboolean unused;
 };
 
-G_DEFINE_TYPE(GVirConfigInterface, gvir_config_interface, GVIR_TYPE_CONFIG_DEVICE);
+G_DEFINE_ABSTRACT_TYPE(GVirConfigInterface, gvir_config_interface, GVIR_TYPE_CONFIG_DEVICE);
 
 
 static void gvir_config_interface_class_init(GVirConfigInterfaceClass *klass)
@@ -58,27 +60,3 @@ static void gvir_config_interface_init(GVirConfigInterface *conn)
 
     memset(priv, 0, sizeof(*priv));
 }
-
-
-GVirConfigInterface *gvir_config_interface_new(void)
-{
-    GVirConfigObject *object;
-
-    object = gvir_config_object_new(GVIR_TYPE_CONFIG_INTERFACE,
-                                    "interface",
-                                    DATADIR "/libvirt/schemas/interface.rng");
-    return GVIR_CONFIG_INTERFACE(object);
-}
-
-GVirConfigInterface *gvir_config_interface_new_from_xml(const gchar *xml,
-                                                        GError **error)
-{
-    GVirConfigObject *object;
-
-    object = gvir_config_object_new_from_xml(GVIR_TYPE_CONFIG_INTERFACE,
-                                             "interface",
-                                             DATADIR "/libvirt/schemas/interface.rng",
-                                             xml, error);
-    return GVIR_CONFIG_INTERFACE(object);
-}
-
diff --git a/libvirt-gconfig/libvirt-gconfig-interface.h b/libvirt-gconfig/libvirt-gconfig-interface.h
index 2877341..88b7d46 100644
--- a/libvirt-gconfig/libvirt-gconfig-interface.h
+++ b/libvirt-gconfig/libvirt-gconfig-interface.h
@@ -56,13 +56,8 @@ struct _GVirConfigInterfaceClass
     gpointer padding[20];
 };
 
-
 GType gvir_config_interface_get_type(void);
 
-GVirConfigInterface *gvir_config_interface_new(void);
-GVirConfigInterface *gvir_config_interface_new_from_xml(const gchar *xml,
-                                                        GError **error);
-
 G_END_DECLS
 
 #endif /* __LIBVIRT_GCONFIG_INTERFACE_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig.h b/libvirt-gconfig/libvirt-gconfig.h
index 01a1af3..c0deae9 100644
--- a/libvirt-gconfig/libvirt-gconfig.h
+++ b/libvirt-gconfig/libvirt-gconfig.h
@@ -35,6 +35,7 @@
 #include <libvirt-gconfig/libvirt-gconfig-enum-types.h>
 #include <libvirt-gconfig/libvirt-gconfig-helpers.h>
 #include <libvirt-gconfig/libvirt-gconfig-interface.h>
+#include <libvirt-gconfig/libvirt-gconfig-interface-network.h>
 #include <libvirt-gconfig/libvirt-gconfig-network.h>
 #include <libvirt-gconfig/libvirt-gconfig-network-filter.h>
 #include <libvirt-gconfig/libvirt-gconfig-node-device.h>
diff --git a/libvirt-gconfig/libvirt-gconfig.sym b/libvirt-gconfig/libvirt-gconfig.sym
index e33ecf5..25eab10 100644
--- a/libvirt-gconfig/libvirt-gconfig.sym
+++ b/libvirt-gconfig/libvirt-gconfig.sym
@@ -49,8 +49,10 @@ LIBVIRT_GOBJECT_0.0.1 {
 	gvir_config_domain_snapshot_new_from_xml;
 
 	gvir_config_interface_get_type;
-	gvir_config_interface_new;
-	gvir_config_interface_new_from_xml;
+
+	gvir_config_interface_network_get_type;
+	gvir_config_interface_network_new;
+	gvir_config_interface_network_new_from_xml;
 
 	gvir_config_network_get_type;
 	gvir_config_network_new;
-- 
1.7.7.3




More information about the libvir-list mailing list