From: Jérémie Galarneau Date: Tue, 24 Jan 2017 01:23:28 +0000 (-0500) Subject: Modify bt_ctf_field_enumeration_get_single_mapping to return iterator X-Git-Tag: v2.0.0-pre1~568 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=e0f15669bbfe5c29244ba6e0eb9f1e81f26e5244 Modify bt_ctf_field_enumeration_get_single_mapping to return iterator Signed-off-by: Jérémie Galarneau --- diff --git a/formats/ctf/ir/field-types.c b/formats/ctf/ir/field-types.c index 49144162..043df078 100644 --- a/formats/ctf/ir/field-types.c +++ b/formats/ctf/ir/field-types.c @@ -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; } diff --git a/formats/ctf/ir/fields.c b/formats/ctf/ir/fields.c index 412b7fde..cf08e780 100644 --- a/formats/ctf/ir/fields.c +++ b/formats/ctf/ir/fields.c @@ -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, diff --git a/include/babeltrace/ctf-ir/fields.h b/include/babeltrace/ctf-ir/fields.h index e9f59fb3..4cc8b03d 100644 --- a/include/babeltrace/ctf-ir/fields.h +++ b/include/babeltrace/ctf-ir/fields.h @@ -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 diff --git a/plugins/text/print.c b/plugins/text/print.c index 5ba97958..3e920a02 100644 --- a/plugins/text/print.c +++ b/plugins/text/print.c @@ -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) { diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 2dd55963..eebb37fb 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -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