diff mbox

[v4,6/8] qom: object_class_property_iter_init() function

Message ID 1477705687-31175-7-git-send-email-ehabkost@redhat.com
State New
Headers show

Commit Message

Eduardo Habkost Oct. 29, 2016, 1:48 a.m. UTC
The new function will allow us to iterate over class properties
using the same logic we use for object properties. Unit test
included.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes series v1 -> v3:
* (none)

Changes series v3 -> v4:
* Trivial whitespace change (removed extra newline)
---
 include/qom/object.h       | 14 ++++++++++++++
 qom/object.c               | 10 ++++++++--
 tests/check-qom-proplist.c | 28 ++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 2 deletions(-)

Comments

Igor Mammedov Oct. 31, 2016, 1:45 p.m. UTC | #1
On Fri, 28 Oct 2016 23:48:05 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> The new function will allow us to iterate over class properties
> using the same logic we use for object properties. Unit test
> included.
> 
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
> Changes series v1 -> v3:
> * (none)
> 
> Changes series v3 -> v4:
> * Trivial whitespace change (removed extra newline)
> ---
>  include/qom/object.h       | 14 ++++++++++++++
>  qom/object.c               | 10 ++++++++--
>  tests/check-qom-proplist.c | 28 ++++++++++++++++++++++++++++
>  3 files changed, 50 insertions(+), 2 deletions(-)
> 
> diff --git a/include/qom/object.h b/include/qom/object.h
> index 5ecc2d1..6e3646e 100644
> --- a/include/qom/object.h
> +++ b/include/qom/object.h
> @@ -995,6 +995,20 @@ void object_property_iter_init(ObjectPropertyIterator *iter,
>                                 Object *obj);
>  
>  /**
> + * object_class_property_iter_init:
> + * @klass: the class
> + *
> + * Initializes an iterator for traversing all properties
> + * registered against an object class and all parent classes.
> + *
> + * It is forbidden to modify the property list while iterating,
> + * whether removing or adding properties.
> + */
> +void object_class_property_iter_init(ObjectPropertyIterator *iter,
> +                                     ObjectClass *klass);
> +
> +
> +/**
>   * object_property_iter_next:
>   * @iter: the iterator instance
>   *
> diff --git a/qom/object.c b/qom/object.c
> index 7a05e35..0cbe228 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -1010,6 +1010,13 @@ void object_property_iter_init(ObjectPropertyIterator *iter,
>      iter->nextclass = object_get_class(obj);
>  }
>  
> +void object_class_property_iter_init(ObjectPropertyIterator *iter,
> +                                     ObjectClass *klass)
> +{
> +    g_hash_table_iter_init(&iter->iter, klass->properties);
> +    iter->nextclass = object_class_get_parent(klass);
> +}
> +
>  ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
>  {
>      gpointer key, val;
> @@ -1017,8 +1024,7 @@ ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
>          if (!iter->nextclass) {
>              return NULL;
>          }
> -        g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
> -        iter->nextclass = object_class_get_parent(iter->nextclass);
> +        object_class_property_iter_init(iter, iter->nextclass);
>      }
>      return val;
>  }
> diff --git a/tests/check-qom-proplist.c b/tests/check-qom-proplist.c
> index 766a4a1..9536415 100644
> --- a/tests/check-qom-proplist.c
> +++ b/tests/check-qom-proplist.c
> @@ -496,6 +496,33 @@ static void test_dummy_iterator(void)
>  }
>  
>  
> +static void test_dummy_class_iterator(void)
> +{
> +    ObjectClass *klass = object_class_by_name(TYPE_DUMMY);
> +    ObjectProperty *prop;
> +    ObjectPropertyIterator iter;
> +    bool seensv = false, seenav = false, seentype;
> +
> +    object_class_property_iter_init(&iter, klass);
> +    while ((prop = object_property_iter_next(&iter))) {
> +        if (g_str_equal(prop->name, "sv")) {
> +            seensv = true;
> +        } else if (g_str_equal(prop->name, "av")) {
> +            seenav = true;
> +        } else if (g_str_equal(prop->name, "type")) {
> +            /* This prop comes from the base Object class */
> +            seentype = true;
> +        } else {
> +            g_printerr("Found prop '%s'\n", prop->name);
> +            g_assert_not_reached();
> +        }
> +    }
> +    g_assert(seenav);
> +    g_assert(seensv);
> +    g_assert(seentype);
> +}
> +
> +
>  static void test_dummy_delchild(void)
>  {
>      Object *parent = object_get_objects_root();
> @@ -524,6 +551,7 @@ int main(int argc, char **argv)
>      g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
>      g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
>      g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
> +    g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator);
>      g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
>  
>      return g_test_run();
Markus Armbruster Nov. 4, 2016, 3:31 p.m. UTC | #2
Eduardo Habkost <ehabkost@redhat.com> writes:

> The new function will allow us to iterate over class properties
> using the same logic we use for object properties. Unit test
> included.
>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
> Changes series v1 -> v3:
> * (none)
>
> Changes series v3 -> v4:
> * Trivial whitespace change (removed extra newline)
> ---
>  include/qom/object.h       | 14 ++++++++++++++
>  qom/object.c               | 10 ++++++++--
>  tests/check-qom-proplist.c | 28 ++++++++++++++++++++++++++++
>  3 files changed, 50 insertions(+), 2 deletions(-)
>
> diff --git a/include/qom/object.h b/include/qom/object.h
> index 5ecc2d1..6e3646e 100644
> --- a/include/qom/object.h
> +++ b/include/qom/object.h
> @@ -995,6 +995,20 @@ void object_property_iter_init(ObjectPropertyIterator *iter,
>                                 Object *obj);
>  
>  /**
> + * object_class_property_iter_init:
> + * @klass: the class
> + *
> + * Initializes an iterator for traversing all properties
> + * registered against an object class and all parent classes.
> + *
> + * It is forbidden to modify the property list while iterating,
> + * whether removing or adding properties.

You chose not to duplicate the example from
object_property_iter_init()'s function comment.  That's okay, but you
might want to point to it.

> + */
> +void object_class_property_iter_init(ObjectPropertyIterator *iter,
> +                                     ObjectClass *klass);
> +
> +
> +/**
>   * object_property_iter_next:
>   * @iter: the iterator instance
>   *
[...]
diff mbox

Patch

diff --git a/include/qom/object.h b/include/qom/object.h
index 5ecc2d1..6e3646e 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -995,6 +995,20 @@  void object_property_iter_init(ObjectPropertyIterator *iter,
                                Object *obj);
 
 /**
+ * object_class_property_iter_init:
+ * @klass: the class
+ *
+ * Initializes an iterator for traversing all properties
+ * registered against an object class and all parent classes.
+ *
+ * It is forbidden to modify the property list while iterating,
+ * whether removing or adding properties.
+ */
+void object_class_property_iter_init(ObjectPropertyIterator *iter,
+                                     ObjectClass *klass);
+
+
+/**
  * object_property_iter_next:
  * @iter: the iterator instance
  *
diff --git a/qom/object.c b/qom/object.c
index 7a05e35..0cbe228 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1010,6 +1010,13 @@  void object_property_iter_init(ObjectPropertyIterator *iter,
     iter->nextclass = object_get_class(obj);
 }
 
+void object_class_property_iter_init(ObjectPropertyIterator *iter,
+                                     ObjectClass *klass)
+{
+    g_hash_table_iter_init(&iter->iter, klass->properties);
+    iter->nextclass = object_class_get_parent(klass);
+}
+
 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
 {
     gpointer key, val;
@@ -1017,8 +1024,7 @@  ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
         if (!iter->nextclass) {
             return NULL;
         }
-        g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
-        iter->nextclass = object_class_get_parent(iter->nextclass);
+        object_class_property_iter_init(iter, iter->nextclass);
     }
     return val;
 }
diff --git a/tests/check-qom-proplist.c b/tests/check-qom-proplist.c
index 766a4a1..9536415 100644
--- a/tests/check-qom-proplist.c
+++ b/tests/check-qom-proplist.c
@@ -496,6 +496,33 @@  static void test_dummy_iterator(void)
 }
 
 
+static void test_dummy_class_iterator(void)
+{
+    ObjectClass *klass = object_class_by_name(TYPE_DUMMY);
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    bool seensv = false, seenav = false, seentype;
+
+    object_class_property_iter_init(&iter, klass);
+    while ((prop = object_property_iter_next(&iter))) {
+        if (g_str_equal(prop->name, "sv")) {
+            seensv = true;
+        } else if (g_str_equal(prop->name, "av")) {
+            seenav = true;
+        } else if (g_str_equal(prop->name, "type")) {
+            /* This prop comes from the base Object class */
+            seentype = true;
+        } else {
+            g_printerr("Found prop '%s'\n", prop->name);
+            g_assert_not_reached();
+        }
+    }
+    g_assert(seenav);
+    g_assert(seensv);
+    g_assert(seentype);
+}
+
+
 static void test_dummy_delchild(void)
 {
     Object *parent = object_get_objects_root();
@@ -524,6 +551,7 @@  int main(int argc, char **argv)
     g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
     g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
     g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
+    g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator);
     g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
 
     return g_test_run();