[libvirt] [libvirt-glib 01/13] Add GVirConfigXmlDoc type

Christophe Fergeau cfergeau at redhat.com
Mon Nov 28 15:32:07 UTC 2011


This class derives from GObject and wraps an xmlDocPtr. It will be
useful to refcount xmlDoc so that it can be shared between multiple
GVirConfigObject instances.
---
 libvirt-gconfig/Makefile.am               |    4 +-
 libvirt-gconfig/libvirt-gconfig-xml-doc.c |  137 +++++++++++++++++++++++++++++
 libvirt-gconfig/libvirt-gconfig-xml-doc.h |   66 ++++++++++++++
 3 files changed, 206 insertions(+), 1 deletions(-)
 create mode 100644 libvirt-gconfig/libvirt-gconfig-xml-doc.c
 create mode 100644 libvirt-gconfig/libvirt-gconfig-xml-doc.h

diff --git a/libvirt-gconfig/Makefile.am b/libvirt-gconfig/Makefile.am
index e91c601..8276d07 100644
--- a/libvirt-gconfig/Makefile.am
+++ b/libvirt-gconfig/Makefile.am
@@ -35,6 +35,7 @@ GCONFIG_HEADER_FILES = \
 noinst_HEADERS = \
 			libvirt-gconfig-helpers-private.h \
 			libvirt-gconfig-object-private.h
+			libvirt-gconfig-xml-doc.h
 GCONFIG_SOURCE_FILES = \
 			libvirt-gconfig-object.c \
 			libvirt-gconfig-capabilities.c \
@@ -59,7 +60,8 @@ GCONFIG_SOURCE_FILES = \
 			libvirt-gconfig-node-device.c \
 			libvirt-gconfig-secret.c \
 			libvirt-gconfig-storage-pool.c \
-			libvirt-gconfig-storage-vol.c
+			libvirt-gconfig-storage-vol.c \
+			libvirt-gconfig-xml-doc.c
 
 libvirt_gconfig_1_0_ladir = $(includedir)/libvirt-gconfig-1.0/libvirt-gconfig
 libvirt_gconfig_1_0_la_HEADERS = \
diff --git a/libvirt-gconfig/libvirt-gconfig-xml-doc.c b/libvirt-gconfig/libvirt-gconfig-xml-doc.c
new file mode 100644
index 0000000..7906158
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-xml-doc.c
@@ -0,0 +1,137 @@
+/*
+ * libvirt-gobject-config-xml-node.c: libvirt glib 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: Christophe Fergeau <cfergeau at gmail.com>
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <libxml/tree.h>
+
+#include "libvirt-gconfig/libvirt-gconfig.h"
+#include "libvirt-gconfig/libvirt-gconfig-xml-doc.h"
+
+extern gboolean debugFlag;
+
+#define DEBUG(fmt, ...) do { if (G_UNLIKELY(debugFlag)) g_debug(fmt, ## __VA_ARGS__); } while (0)
+
+#define GVIR_CONFIG_XML_DOC_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_CONFIG_XML_DOC, GVirConfigXmlDocPrivate))
+
+struct _GVirConfigXmlDocPrivate
+{
+    xmlDocPtr doc;
+};
+
+G_DEFINE_TYPE(GVirConfigXmlDoc, gvir_config_xml_doc, G_TYPE_OBJECT);
+
+enum {
+    PROP_0,
+    PROP_DOC
+};
+
+static void gvir_config_xml_doc_get_property(GObject *object,
+                                              guint prop_id,
+                                              GValue *value,
+                                              GParamSpec *pspec)
+{
+    GVirConfigXmlDoc *xml_doc = GVIR_CONFIG_XML_DOC(object);
+
+    switch (prop_id) {
+    case PROP_DOC:
+        g_value_set_pointer(value, xml_doc->priv->doc);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+static void gvir_config_xml_doc_set_property(GObject *object,
+                                              guint prop_id,
+                                              const GValue *value,
+                                              GParamSpec *pspec)
+{
+    GVirConfigXmlDoc *doc = GVIR_CONFIG_XML_DOC(object);
+
+    switch (prop_id) {
+    case PROP_DOC:
+        doc->priv->doc = g_value_get_pointer(value);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+static void gvir_config_xml_doc_finalize(GObject *object)
+{
+    GVirConfigXmlDoc *doc = GVIR_CONFIG_XML_DOC(object);
+    GVirConfigXmlDocPrivate *priv = doc->priv;
+
+    DEBUG("Finalize GVirConfigXmlDoc=%p", doc);
+
+    if (priv->doc)
+        xmlFreeDoc(priv->doc);
+
+    G_OBJECT_CLASS(gvir_config_xml_doc_parent_class)->finalize(object);
+}
+
+static void gvir_config_xml_doc_class_init(GVirConfigXmlDocClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof(GVirConfigXmlDocPrivate));
+
+    object_class->finalize = gvir_config_xml_doc_finalize;
+    object_class->get_property = gvir_config_xml_doc_get_property;
+    object_class->set_property = gvir_config_xml_doc_set_property;
+
+    g_object_class_install_property(object_class,
+                                    PROP_DOC,
+                                    g_param_spec_pointer("doc",
+                                                        "XML Doc",
+                                                        "The XML doc this config object corresponds to",
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY |
+                                                        G_PARAM_STATIC_STRINGS));
+}
+
+
+static void gvir_config_xml_doc_init(GVirConfigXmlDoc *xml_doc)
+{
+    GVirConfigXmlDocPrivate *priv;
+
+    DEBUG("Init GVirConfigXmlDoc=%p", xml_doc);
+
+    priv = xml_doc->priv = GVIR_CONFIG_XML_DOC_GET_PRIVATE(xml_doc);
+
+    memset(priv, 0, sizeof(*priv));
+}
+
+
+GVirConfigXmlDoc *gvir_config_xml_doc_new(xmlDocPtr doc)
+{
+    if (doc == NULL) {
+        doc = xmlNewDoc((xmlChar *)"1.0");
+    }
+    return GVIR_CONFIG_XML_DOC(g_object_new(GVIR_TYPE_CONFIG_XML_DOC,
+                                             "doc", doc,
+                                             NULL));
+}
diff --git a/libvirt-gconfig/libvirt-gconfig-xml-doc.h b/libvirt-gconfig/libvirt-gconfig-xml-doc.h
new file mode 100644
index 0000000..079de1e
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-xml-doc.h
@@ -0,0 +1,66 @@
+/*
+ * libvirt-gobject-xml-doc.h: 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: Christophe Fergeau <cfergeau at gmail.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_XML_DOC_H__
+#define __LIBVIRT_GCONFIG_XML_DOC_H__
+
+G_BEGIN_DECLS
+
+#define GVIR_TYPE_CONFIG_XML_DOC            (gvir_config_xml_doc_get_type ())
+#define GVIR_CONFIG_XML_DOC(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_CONFIG_XML_DOC, GVirConfigXmlDoc))
+#define GVIR_CONFIG_XML_DOC_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GVIR_TYPE_CONFIG_XML_DOC, GVirConfigXmlDocClass))
+#define GVIR_IS_CONFIG_XML_DOC(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GVIR_TYPE_CONFIG_XML_DOC))
+#define GVIR_IS_CONFIG_XML_DOC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GVIR_TYPE_CONFIG_XML_DOC))
+#define GVIR_CONFIG_XML_DOC_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GVIR_TYPE_CONFIG_XML_DOC, GVirConfigXmlDocClass))
+
+typedef struct _GVirConfigXmlDoc GVirConfigXmlDoc;
+typedef struct _GVirConfigXmlDocPrivate GVirConfigXmlDocPrivate;
+typedef struct _GVirConfigXmlDocClass GVirConfigXmlDocClass;
+
+struct _GVirConfigXmlDoc
+{
+    GObject parent;
+
+    GVirConfigXmlDocPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _GVirConfigXmlDocClass
+{
+    GObjectClass parent_class;
+
+    gpointer padding[20];
+};
+
+
+GType gvir_config_xml_doc_get_type(void);
+
+GVirConfigXmlDoc *gvir_config_xml_doc_new(xmlDocPtr doc);
+
+G_END_DECLS
+
+#endif /* __LIBVIRT_GCONFIG_XML_DOC_H__ */
-- 
1.7.7.3




More information about the libvir-list mailing list