[libvirt] [libvirt-glib 2/5] More internal helpers for GVirConfigObject subclasses

Christophe Fergeau cfergeau at redhat.com
Thu May 10 16:48:37 UTC 2012


On Wed, May 09, 2012 at 03:54:35AM +0300, Zeeshan Ali (Khattak) wrote:
> From: "Zeeshan Ali (Khattak)" <zeeshanak at gnome.org>
> 
> - gvir_config_object_add_child_with_type()
> - gvir_config_object_get_child()
> - gvir_config_object_get_child_with_type()
> ---
>  libvirt-gconfig/libvirt-gconfig-object-private.h |    8 ++++
>  libvirt-gconfig/libvirt-gconfig-object.c         |   47 ++++++++++++++++++++--
>  2 files changed, 51 insertions(+), 4 deletions(-)
> 
> diff --git a/libvirt-gconfig/libvirt-gconfig-object-private.h b/libvirt-gconfig/libvirt-gconfig-object-private.h
> index a6b7395..eb2cc09 100644
> --- a/libvirt-gconfig/libvirt-gconfig-object-private.h
> +++ b/libvirt-gconfig/libvirt-gconfig-object-private.h
> @@ -55,6 +55,9 @@ void gvir_config_object_set_node_content_uint64(GVirConfigObject *object,
>                                                  guint64 value);
>  GVirConfigObject *gvir_config_object_add_child(GVirConfigObject *object,
>                                                 const char *child_name);
> +GVirConfigObject *gvir_config_object_add_child_with_type(GVirConfigObject *object,
> +                                                         const char *child_name,
> +                                                         GType child_type);

This API is not used at all, just drop it

>  void gvir_config_object_add_child_with_attribute(GVirConfigObject *object,
>                                                   const char *child_name,
>                                                   const char *attr_name,
> @@ -96,6 +99,11 @@ void gvir_config_object_foreach_child(GVirConfigObject *object,
>  gboolean gvir_config_object_set_namespace(GVirConfigObject *object,
>                                            const char *ns,
>                                            const char *ns_uri);
> +GVirConfigObject *gvir_config_object_get_child(GVirConfigObject *object,
> +                                               const gchar *child_name);
> +GVirConfigObject *gvir_config_object_get_child_with_type(GVirConfigObject *object,
> +                                                         const gchar *child_name,
> +                                                         GType child_type);
>  
>  G_END_DECLS
>  
> diff --git a/libvirt-gconfig/libvirt-gconfig-object.c b/libvirt-gconfig/libvirt-gconfig-object.c
> index ee3584a..3dac59a 100644
> --- a/libvirt-gconfig/libvirt-gconfig-object.c
> +++ b/libvirt-gconfig/libvirt-gconfig-object.c
> @@ -366,8 +366,9 @@ gvir_config_object_foreach_child(GVirConfigObject *object,
>  }
>  
>  G_GNUC_INTERNAL GVirConfigObject *
> -gvir_config_object_add_child(GVirConfigObject *object,
> -                             const char *child_name)
> +gvir_config_object_add_child_with_type(GVirConfigObject *object,
> +                                       const char *child_name,
> +                                       GType child_type)
>  {
>      xmlNodePtr new_node;
>      xmlNodePtr old_node;
> @@ -380,18 +381,27 @@ gvir_config_object_add_child(GVirConfigObject *object,
>                                                       FALSE);
>      if (old_node != NULL) {
>          xmlFreeNode(new_node);
> -        return GVIR_CONFIG_OBJECT(g_object_new(GVIR_CONFIG_TYPE_OBJECT,
> +        return GVIR_CONFIG_OBJECT(g_object_new(child_type,
>                                                 "doc", object->priv->doc,
>                                                 "node", old_node,
>                                                 NULL));
>      }
>  
> -    return GVIR_CONFIG_OBJECT(g_object_new(GVIR_CONFIG_TYPE_OBJECT,
> +    return GVIR_CONFIG_OBJECT(g_object_new(child_type,
>                                             "doc", object->priv->doc,
>                                             "node", new_node,
>                                             NULL));
>  }
>  
> +G_GNUC_INTERNAL GVirConfigObject *
> +gvir_config_object_add_child(GVirConfigObject *object,
> +                             const char *child_name)
> +{
> +    return gvir_config_object_add_child_with_type(object,
> +                                                  child_name,
> +                                                  GVIR_CONFIG_TYPE_OBJECT);
> +}
> +
>  G_GNUC_INTERNAL void
>  gvir_config_object_add_child_with_attribute(GVirConfigObject *object,
>                                              const char *child_name,
> @@ -865,3 +875,32 @@ gvir_config_object_set_namespace(GVirConfigObject *object, const char *ns,
>  
>      return TRUE;
>  }
> +
> +G_GNUC_INTERNAL GVirConfigObject *
> +gvir_config_object_get_child_with_type(GVirConfigObject *object,
> +                                       const gchar *child_name,
> +                                       GType child_type)
> +{
> +    xmlNodePtr node;
> +
> +    g_return_val_if_fail(GVIR_CONFIG_IS_OBJECT(object), NULL);
> +    g_return_val_if_fail(child_name != NULL, NULL);
> +
> +    node = gvir_config_xml_get_element(object->priv->node, child_name, NULL);
> +    g_return_val_if_fail(node != NULL, NULL);


Do we want to be extra paranoid and check that node->name is child_name?

> +
> +    return gvir_config_object_new_from_tree(child_type,
> +                                            object->priv->doc,
> +                                            object->priv->schema,
> +                                            node);


So I think this API is a bit too limited, and a bit dangerous, but I'm not
sure what the best forward is. What I'm worried about is that it's
basically casting a random xml node to an arbitrary type, and we don't
really have a way to let the class it's being cast to make some sanity
checks first. Maybe we could have a new_from_tree function pointer on
GVirObjectClass and use that to create new instances of GVirObject. Child
classes would be able to override it. Maybe we could use a GInitable. Maybe
we should do something totally different, I don't really know, just random
thinking ;)

ACK with the add_child_with_type changes removed.

Christophe


> +}
> +
> +G_GNUC_INTERNAL GVirConfigObject *
> +gvir_config_object_get_child(GVirConfigObject *object,
> +                             const gchar *child_name)
> +{
> +    return gvir_config_object_get_child_with_type(object,
> +                                                  child_name,
> +                                                  GVIR_CONFIG_TYPE_OBJECT);
> +}
> +
> -- 
> 1.7.7.6
> 
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120510/60acdbb6/attachment-0001.sig>


More information about the libvir-list mailing list