[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [libvirt-glib] Add {replace,add}_with_attribute



These functions may seem very specialized, and the same can easily
be achieved with 2 separate API calls, but since this is something
that is commonly done, these helpers save quite a few lines of code.
---
 libvirt-gconfig/libvirt-gconfig-domain-disk.c      |   49 ++++++--------------
 libvirt-gconfig/libvirt-gconfig-domain-filesys.c   |   19 ++------
 .../libvirt-gconfig-domain-interface-bridge.c      |    9 +---
 .../libvirt-gconfig-domain-interface-network.c     |    9 +---
 libvirt-gconfig/libvirt-gconfig-domain-interface.c |   27 ++--------
 libvirt-gconfig/libvirt-gconfig-object-private.h   |    8 +++
 libvirt-gconfig/libvirt-gconfig-object.c           |   25 ++++++++++
 7 files changed, 62 insertions(+), 84 deletions(-)

diff --git a/libvirt-gconfig/libvirt-gconfig-domain-disk.c b/libvirt-gconfig/libvirt-gconfig-domain-disk.c
index e517d65..aa5ab81 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-disk.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-disk.c
@@ -130,13 +130,9 @@ void gvir_config_domain_disk_set_snapshot_type(GVirConfigDomainDisk *disk,
 void gvir_config_domain_disk_set_source(GVirConfigDomainDisk *disk,
                                         const char *source)
 {
-    GVirConfigObject *source_node;
     const char *attribute_name;
 
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_DISK(disk));
-    source_node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(disk),
-                                                   "source");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(source_node));
 
     switch (disk->priv->type) {
         case GVIR_CONFIG_DOMAIN_DISK_FILE:
@@ -154,62 +150,45 @@ void gvir_config_domain_disk_set_source(GVirConfigDomainDisk *disk,
         default:
             g_return_if_reached();
     }
-    gvir_config_object_set_attribute(source_node,
-                                     attribute_name, source,
-                                     NULL);
-    g_object_unref(G_OBJECT(source_node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(disk),
+                                                   "source",
+                                                   attribute_name, source);
 }
 
 void gvir_config_domain_disk_set_driver_name(GVirConfigDomainDisk *disk,
                                              const char *driver_name)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_DISK(disk));
-    node = gvir_config_object_add_child(GVIR_CONFIG_OBJECT(disk), "driver");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "name", driver_name, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_add_child_with_attribute(GVIR_CONFIG_OBJECT(disk),
+                                                "driver", "name", driver_name);
 }
 
 void gvir_config_domain_disk_set_driver_type(GVirConfigDomainDisk *disk,
                                              const char *driver_type)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_DISK(disk));
-    node = gvir_config_object_add_child(GVIR_CONFIG_OBJECT(disk), "driver");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "type", driver_type, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_add_child_with_attribute(GVIR_CONFIG_OBJECT(disk),
+                                                "driver", "type", driver_type);
 }
 
 void gvir_config_domain_disk_set_target_bus(GVirConfigDomainDisk *disk,
                                             GVirConfigDomainDiskBus bus)
 {
-    GVirConfigObject *node;
+    const char *bus_str;
 
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_DISK(disk));
-    node = gvir_config_object_add_child(GVIR_CONFIG_OBJECT(disk), "target");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute_with_type(node,
-                                               "bus",
-                                               GVIR_TYPE_CONFIG_DOMAIN_DISK_BUS,
-                                               bus,
-                                               NULL);
-    g_object_unref(G_OBJECT(node));
+    bus_str = gvir_config_genum_get_nick(GVIR_TYPE_CONFIG_DOMAIN_DISK_BUS, bus);
+    g_return_if_fail(bus_str != NULL);
+    gvir_config_object_add_child_with_attribute(GVIR_CONFIG_OBJECT(disk),
+                                                "target", "bus", bus_str);
 }
 
 void gvir_config_domain_disk_set_target_dev(GVirConfigDomainDisk *disk,
                                             const char *dev)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_DISK(disk));
-    node = gvir_config_object_add_child(GVIR_CONFIG_OBJECT(disk), "target");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "dev", dev, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_add_child_with_attribute(GVIR_CONFIG_OBJECT(disk),
+                                                "target", "dev", dev);
 }
 
 GVirConfigDomainDiskType
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-filesys.c b/libvirt-gconfig/libvirt-gconfig-domain-filesys.c
index ab33e50..ef942f8 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-filesys.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-filesys.c
@@ -114,13 +114,9 @@ void gvir_config_domain_filesys_set_driver_type(GVirConfigDomainFilesys *filesys
 void gvir_config_domain_filesys_set_source(GVirConfigDomainFilesys *filesys,
                                            const char *source)
 {
-    GVirConfigObject *source_node;
     const char *attribute_name;
 
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_FILESYS(filesys));
-    source_node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(filesys),
-                                                   "source");
-    g_return_if_fail(source_node != NULL);
 
     switch (filesys->priv->type) {
         case GVIR_CONFIG_DOMAIN_FILESYS_MOUNT:
@@ -139,23 +135,18 @@ void gvir_config_domain_filesys_set_source(GVirConfigDomainFilesys *filesys,
             g_return_if_reached();
     }
 
-    gvir_config_object_set_attribute(source_node,
-                                     attribute_name, source,
-                                     NULL);
-    g_object_unref(G_OBJECT(source_node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(filesys),
+                                                    "source",
+                                                    attribute_name, source);
 }
 
 
 void gvir_config_domain_filesys_set_target(GVirConfigDomainFilesys *filesys,
                                            const char *path)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_FILESYS(filesys));
-    node = gvir_config_object_add_child(GVIR_CONFIG_OBJECT(filesys), "target");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "dir", path, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_add_child_with_attribute(GVIR_CONFIG_OBJECT(filesys),
+                                                "target", "dir", path);
 }
 
 void gvir_config_domain_filesys_set_readonly(GVirConfigDomainFilesys *filesys,
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c b/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c
index 9bf6853..61d3563 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c
@@ -79,13 +79,8 @@ GVirConfigDomainInterfaceBridge *gvir_config_domain_interface_bridge_new_from_xm
 void gvir_config_domain_interface_bridge_set_source(GVirConfigDomainInterfaceBridge *interface,
                                                     const char *brname)
 {
-    GVirConfigObject *source_node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_INTERFACE_BRIDGE(interface));
 
-    source_node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(interface),
-                                                   "source");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(source_node));
-    gvir_config_object_set_attribute(source_node, "bridge", brname, NULL);
-    g_object_unref(G_OBJECT(source_node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(interface),
+                                                    "source", "bridge", brname);
 }
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c b/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c
index 433c02a..6f24708 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c
@@ -78,13 +78,8 @@ GVirConfigDomainInterfaceNetwork *gvir_config_domain_interface_network_new_from_
 void gvir_config_domain_interface_network_set_source(GVirConfigDomainInterfaceNetwork *interface,
                                                      const char *source)
 {
-    GVirConfigObject *source_node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_INTERFACE_NETWORK(interface));
 
-    source_node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(interface),
-                                                   "source");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(source_node));
-    gvir_config_object_set_attribute(source_node, "network", source, NULL);
-    g_object_unref(G_OBJECT(source_node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(interface),
+                                                    "source", "network", source);
 }
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-interface.c b/libvirt-gconfig/libvirt-gconfig-domain-interface.c
index ba3c9e3..0f427ff 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-interface.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-interface.c
@@ -54,15 +54,10 @@ static void gvir_config_domain_interface_init(GVirConfigDomainInterface *interfa
 void gvir_config_domain_interface_set_ifname(GVirConfigDomainInterface *interface,
                                              const char *ifname)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_INTERFACE(interface));
 
-    node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(interface),
-                                            "target");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "device", ifname, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(interface),
+                                                    "target", "device", ifname);
 }
 
 void gvir_config_domain_interface_set_link_state(GVirConfigDomainInterface *interface,
@@ -86,29 +81,19 @@ void gvir_config_domain_interface_set_link_state(GVirConfigDomainInterface *inte
 void gvir_config_domain_interface_set_mac(GVirConfigDomainInterface *interface,
                                           const char *mac_address)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_INTERFACE(interface));
 
-    node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(interface),
-                                            "mac");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "address", mac_address, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(interface),
+                                                    "mac", "address", mac_address);
 }
 
 void gvir_config_domain_interface_set_model(GVirConfigDomainInterface *interface,
                                             const char *model)
 {
-    GVirConfigObject *node;
-
     g_return_if_fail(GVIR_IS_CONFIG_DOMAIN_INTERFACE(interface));
 
-    node = gvir_config_object_replace_child(GVIR_CONFIG_OBJECT(interface),
-                                            "model");
-    g_return_if_fail(GVIR_IS_CONFIG_OBJECT(node));
-    gvir_config_object_set_attribute(node, "type", model, NULL);
-    g_object_unref(G_OBJECT(node));
+    gvir_config_object_replace_child_with_attribute(GVIR_CONFIG_OBJECT(interface),
+                                                    "model", "type", model);
 }
 
 G_GNUC_INTERNAL GVirConfigDomainDevice *
diff --git a/libvirt-gconfig/libvirt-gconfig-object-private.h b/libvirt-gconfig/libvirt-gconfig-object-private.h
index f7c1f68..38c129f 100644
--- a/libvirt-gconfig/libvirt-gconfig-object-private.h
+++ b/libvirt-gconfig/libvirt-gconfig-object-private.h
@@ -43,8 +43,16 @@ void gvir_config_object_set_node_content_uint64(GVirConfigObject *object,
                                                 guint64 value);
 GVirConfigObject *gvir_config_object_add_child(GVirConfigObject *object,
                                                const char *child_name);
+void gvir_config_object_add_child_with_attribute(GVirConfigObject *object,
+                                                 const char *child_name,
+                                                 const char *attr_name,
+                                                 const char *attr_value);
 GVirConfigObject *gvir_config_object_replace_child(GVirConfigObject *object,
                                                    const char *child_name);
+void gvir_config_object_replace_child_with_attribute(GVirConfigObject *object,
+                                                     const char *child_name,
+                                                     const char *attr_name,
+                                                     const char *attr_value);
 void gvir_config_object_delete_child(GVirConfigObject *object,
                                      const char *child_name);
 void gvir_config_object_set_child(GVirConfigObject *object,
diff --git a/libvirt-gconfig/libvirt-gconfig-object.c b/libvirt-gconfig/libvirt-gconfig-object.c
index 4f95189..7a6d062 100644
--- a/libvirt-gconfig/libvirt-gconfig-object.c
+++ b/libvirt-gconfig/libvirt-gconfig-object.c
@@ -363,6 +363,19 @@ gvir_config_object_add_child(GVirConfigObject *object,
                                            NULL));
 }
 
+G_GNUC_INTERNAL void
+gvir_config_object_add_child_with_attribute(GVirConfigObject *object,
+                                            const char *child_name,
+                                            const char *attr_name,
+                                            const char *attr_value)
+{
+    GVirConfigObject *child;
+
+    child = gvir_config_object_add_child(object, child_name);
+    gvir_config_object_set_attribute(child, attr_name, attr_value, NULL);
+    g_object_unref(G_OBJECT(child));
+}
+
 G_GNUC_INTERNAL GVirConfigObject *
 gvir_config_object_replace_child(GVirConfigObject *object,
                                  const char *child_name)
@@ -381,6 +394,18 @@ gvir_config_object_replace_child(GVirConfigObject *object,
                                            NULL));
 }
 
+G_GNUC_INTERNAL void
+gvir_config_object_replace_child_with_attribute(GVirConfigObject *object,
+                                                const char *child_name,
+                                                const char *attr_name,
+                                                const char *attr_value)
+{
+    GVirConfigObject *child;
+
+    child = gvir_config_object_replace_child(object, child_name);
+    gvir_config_object_set_attribute(child, attr_name, attr_value, NULL);
+    g_object_unref(G_OBJECT(child));
+}
 
 G_GNUC_INTERNAL void
 gvir_config_object_delete_child(GVirConfigObject *object,
-- 
1.7.7.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]