Modify bt_ctf_field_enumeration_get_single_mapping to return iterator
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 24 Jan 2017 01:23:28 +0000 (20:23 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 18:09:09 +0000 (14:09 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
formats/ctf/ir/field-types.c
formats/ctf/ir/fields.c
include/babeltrace/ctf-ir/fields.h
plugins/text/print.c
tests/lib/test_ctf_writer.c

index 49144162d49c9c25372a8e0963279453319cd61a..043df078464f5eb41d3bcec5b4f367c42b4546d7 100644 (file)
@@ -1879,22 +1879,30 @@ struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(
                struct bt_ctf_field_type *type,
                struct bt_ctf_field *tag)
 {
+       int ret;
        const char *enum_value;
        struct bt_ctf_field_type *field_type = NULL;
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
 
        if (!type || !tag || type->declaration->id != BT_CTF_TYPE_ID_VARIANT) {
                goto end;
        }
 
-       enum_value = bt_ctf_field_enumeration_get_single_mapping_name(tag);
-       if (!enum_value) {
+       iter = bt_ctf_field_enumeration_get_mappings(tag);
+       if (!iter) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter,
+               &enum_value);
+       if (ret) {
                goto end;
        }
 
-       /* Already increments field_type's reference count */
        field_type = bt_ctf_field_type_variant_get_field_type_by_name(
                type, enum_value);
 end:
+       bt_put(iter);
        return field_type;
 }
 
index 412b7fdef065cc3adf0cf80bd3d35f32a5d811bb..cf08e7807af32fc2ac99822b7a65c4a5acc85f50 100644 (file)
@@ -866,11 +866,10 @@ end:
        return container;
 }
 
-const char *bt_ctf_field_enumeration_get_single_mapping_name(
-       struct bt_ctf_field *field)
+struct bt_ctf_field_type_enumeration_mapping_iterator *
+bt_ctf_field_enumeration_get_mappings(struct bt_ctf_field *field)
 {
        int ret;
-       const char *name = NULL;
        struct bt_ctf_field *container = NULL;
        struct bt_ctf_field_type *container_type = NULL;
        struct bt_ctf_field_type_integer *integer_type = NULL;
@@ -899,11 +898,6 @@ const char *bt_ctf_field_enumeration_get_single_mapping_name(
                }
                iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
                                field->type, value);
-               if (!iter) {
-                       goto error_put_container_type;
-               }
-               (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
-                               iter, &name, NULL, NULL);
        } else {
                int64_t value;
 
@@ -914,11 +908,6 @@ const char *bt_ctf_field_enumeration_get_single_mapping_name(
                }
                iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
                                field->type, value);
-               if (!iter) {
-                       goto error_put_container_type;
-               }
-               (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
-                               iter, &name, NULL, NULL);
        }
 
 error_put_container_type:
@@ -926,8 +915,7 @@ error_put_container_type:
 error_put_container:
        bt_put(container);
 end:
-       bt_put(iter);
-       return name;
+       return iter;
 }
 
 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field,
index e9f59fb303bf20cad66f32cdb1d1cc7f644246b0..4cc8b03d8df55fddc6bd18b6d51b10ec0e469bf7 100644 (file)
@@ -126,7 +126,7 @@ struct bt_ctf_field;
 struct bt_ctf_event_class;
 struct bt_ctf_event;
 struct bt_ctf_field_type;
-struct bt_ctf_field_type_enum_iter;
+struct bt_ctf_field_type_enumeration_mapping_iterator;
 
 /**
 @name Creation and parent field type access functions
@@ -607,9 +607,9 @@ bt_ctf_field_signed_integer_set_value() or
 bt_ctf_field_unsigned_integer_set_value().
 
 Once you set the integral value of an enumeration field by following the
-previous paragraph, you can get the name of the mapping containing this
+previous paragraph, you can get the names of the mappings containing this
 value in the enumeration field with
-bt_ctf_field_enumeration_get_mapping_name().
+bt_ctf_field_enumeration_get_mappings().
 
 @sa ctfirenumfieldtype
 @sa ctfirfields
@@ -639,8 +639,8 @@ extern struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
                struct bt_ctf_field *enum_field);
 
 /**
-@brief Returns the name of the mapping selected by the current integral
-       value of the @enumfield \p enum_field.
+@brief Returns an iterator to the mappings selected by the current
+       integral value of the @enumfield \p enum_field.
 
 On success, \p enum_field remains the sole owner of the returned
 value.
@@ -648,9 +648,9 @@ value.
 @param[in] enum_field  Enumeration field of which to get the name of
                        mapping associated to its current integral
                        value.
-@returns               Name of the mapping associated to the current
-                       integral value of \p enum_field, or \c NULL
-                       on error.
+@returns               An iterator to the mappings associated to the
+                       current integral value of \p enum_field, or
+                       \c NULL on error.
 
 @prenotnull{enum_field}
 @preisenumfield{enum_field}
@@ -658,8 +658,8 @@ value.
        value.
 @postrefcountsame{enum_field}
 */
-extern const char *bt_ctf_field_enumeration_get_mapping_name(
-               struct bt_ctf_field *enum_field);
+extern struct bt_ctf_field_type_enumeration_mapping_iterator *
+bt_ctf_field_enumeration_get_mappings(struct bt_ctf_field *enum_field);
 
 /** @} */
 
@@ -1125,9 +1125,6 @@ extern struct bt_ctf_field *bt_ctf_field_variant_get_tag(
 
 /** @} */
 
-const char *bt_ctf_field_enumeration_get_single_mapping_name(
-       struct bt_ctf_field *field);
-
 #ifdef __cplusplus
 }
 #endif
index 5ba979588d9381231bb6b0d2c20827faa3991547..3e920a02ca54a9bc3e78085c8183d3c09617e7af 100644 (file)
@@ -1010,22 +1010,36 @@ enum bt_component_status print_variant(struct text_component *text,
        fprintf(text->out, "{ ");
        text->depth++;
        if (print_names) {
+               int iter_ret;
                struct bt_ctf_field *tag_field = NULL;
                const char *tag_choice;
+               struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
 
                tag_field = bt_ctf_field_variant_get_tag(variant);
                if (!tag_field) {
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto end;
                }
-               tag_choice = bt_ctf_field_enumeration_get_single_mapping_name(tag_field);
-               if (!tag_choice) {
+
+               iter = bt_ctf_field_enumeration_get_mappings(tag_field);
+               if (!iter) {
+                       bt_put(tag_field);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+
+               iter_ret =
+                       bt_ctf_field_type_enumeration_mapping_iterator_get_name(
+                               iter, &tag_choice);
+               if (iter_ret) {
+                       bt_put(iter);
                        bt_put(tag_field);
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto end;
                }
                fprintf(text->out, "%s = ", rem_(tag_choice));
                bt_put(tag_field);
+               bt_put(iter);
        }
        ret = print_field(text, field, print_names);
        if (ret != BT_COMPONENT_STATUS_OK) {
index 2dd5596302210f9c3223e81c5dadb13cd1adf134..eebb37fb2961317963abddb2d76f644acabd684b 100644 (file)
@@ -60,7 +60,7 @@
 #define DEFAULT_CLOCK_TIME 0
 #define DEFAULT_CLOCK_VALUE 0
 
-#define NR_TESTS 600
+#define NR_TESTS 602
 
 static int64_t current_time = 42;
 
@@ -586,20 +586,24 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        enum_field = bt_ctf_field_create(ep_enum_field_type);
        assert(enum_field);
 
-       ret_char = bt_ctf_field_enumeration_get_single_mapping_name(NULL);
-       ok(!ret_char, "bt_ctf_field_enumeration_get_single_mapping_name handles NULL correctly");
-       ret_char = bt_ctf_field_enumeration_get_single_mapping_name(enum_field);
-       ok(!ret_char, "bt_ctf_field_enumeration_get_single_mapping_name returns NULL if the enumeration's container field is unset");
+       iter = bt_ctf_field_enumeration_get_mappings(NULL);
+       ok(!iter, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
+       iter = bt_ctf_field_enumeration_get_mappings(enum_field);
+       ok(!iter, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
        enum_container_field = bt_ctf_field_enumeration_get_container(
                enum_field);
        ok(bt_ctf_field_signed_integer_set_value(
                enum_container_field, -42) == 0,
                "Set signed enumeration container value");
-       ret_char = bt_ctf_field_enumeration_get_single_mapping_name(enum_field);
+       iter = bt_ctf_field_enumeration_get_mappings(enum_field);
+       ok(iter, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
+       ret = bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter, &ret_char);
+       ok(!ret && ret_char, "bt_ctf_field_type_enumeration_mapping_iterator_get_name return a mapping name");
        ok(!strcmp(ret_char, mapping_name_negative_test),
                "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
        ret = bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
        assert(!ret);
+       BT_PUT(iter);
 
        enum_field_unsigned = bt_ctf_field_create(ep_enum_field_unsigned_type);
        assert(enum_field_unsigned);
@@ -611,7 +615,9 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        ret = bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
                enum_field_unsigned);
        assert(!ret);
-       ret_char = bt_ctf_field_enumeration_get_single_mapping_name(enum_field_unsigned);
+       iter = bt_ctf_field_enumeration_get_mappings(enum_field_unsigned);
+       assert(iter);
+       (void) bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter, &ret_char);
        ok(ret_char && !strcmp(ret_char, mapping_name_test),
                "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an unsigned container");
 
@@ -701,6 +707,7 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        bt_put(ep_integer_field_type);
        bt_put(ep_enum_field_type);
        bt_put(ep_enum_field_unsigned_type);
+       bt_put(iter);
 }
 
 static
@@ -1381,6 +1388,7 @@ void field_copy_tests()
        struct bt_ctf_field *a_3_copy = NULL;
        struct bt_ctf_field *a_4_copy = NULL;
        struct bt_ctf_field *strct_copy = NULL;
+       struct bt_ctf_field_type_enumeration_mapping_iterator *e_iter = NULL;
        uint64_t uint64_t_val;
        const char *str_val;
        double double_val;
@@ -1708,7 +1716,9 @@ void field_copy_tests()
                "bt_ctf_field_copy creates a valid enum's integer field copy");
 
        /* validate e copy */
-       str_val = bt_ctf_field_enumeration_get_single_mapping_name(e_copy);
+       e_iter = bt_ctf_field_enumeration_get_mappings(e_copy);
+       (void) bt_ctf_field_type_enumeration_mapping_iterator_get_name(e_iter,
+               &str_val);
        ok(str_val && !strcmp(str_val, "LABEL2"),
                "bt_ctf_field_copy creates a valid enum field copy");
 
@@ -1831,6 +1841,7 @@ void field_copy_tests()
        bt_put(a_3_copy);
        bt_put(a_4_copy);
        bt_put(strct_copy);
+       bt_put(e_iter);
 }
 
 static
This page took 0.030578 seconds and 4 git commands to generate.