lib: add boolean field class and field types
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 11 Aug 2019 15:20:14 +0000 (11:20 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 15 Aug 2019 15:41:44 +0000 (11:41 -0400)
This patch adds the boolean field class and field to the available
library types. They are analogous to the boolean value object which
already exists.

CTF 2 will very likely have such a type, so Babeltrace 2.0 will be
ready.

This patch does not adapt existing plugins and the Python bindings to
use and wrap the new boolean field class and field; this work is
reserved for subsequent patches.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I90874f214757ad67d5eecaad5bd8297b8610c001
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1891
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
include/babeltrace2/trace-ir/field-class-const.h
include/babeltrace2/trace-ir/field-class.h
include/babeltrace2/trace-ir/field-const.h
include/babeltrace2/trace-ir/field.h
src/lib/lib-logging.c
src/lib/trace-ir/field-class.c
src/lib/trace-ir/field-class.h
src/lib/trace-ir/field.c
src/lib/trace-ir/field.h

index 7511d0d4b7c22a959c1080af2aaf772f24abf135..688c61d1f7a0e3328d0917aeba45843da77a4ace 100644 (file)
@@ -37,6 +37,7 @@ extern "C" {
 #endif
 
 typedef enum bt_field_class_type {
+       BT_FIELD_CLASS_TYPE_BOOL,
        BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER,
        BT_FIELD_CLASS_TYPE_SIGNED_INTEGER,
        BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
index 9d2cf064901e33bd700a8196188110b335a7c6ca..d2f43becf54542546f6b6b47410b3ece7fff641b 100644 (file)
@@ -37,6 +37,9 @@
 extern "C" {
 #endif
 
+extern bt_field_class *bt_field_class_bool_create(
+               bt_trace_class *trace_class);
+
 extern bt_field_class *bt_field_class_integer_unsigned_create(
                bt_trace_class *trace_class);
 
index 7ba63ee0b5e37f05c2f46e4ff898b151fb85a4e4..f3f1ef8d77698f2b2a5d90f2146373cc2a84ae22 100644 (file)
@@ -42,6 +42,8 @@ extern const bt_field_class *bt_field_borrow_class_const(
 extern bt_field_class_type bt_field_get_class_type(
                const bt_field *field);
 
+extern bt_bool bt_field_bool_get_value(const bt_field *field);
+
 extern int64_t bt_field_integer_signed_get_value(const bt_field *field);
 
 extern uint64_t bt_field_integer_unsigned_get_value(
index 75d3ca45397aaa4775b93bc5191e7ceb2e85106a..311c53a8bfbf5af25f9d5e2776b0fac00130e75b 100644 (file)
@@ -35,6 +35,8 @@
 extern "C" {
 #endif
 
+extern void bt_field_bool_set_value(bt_field *field, bt_bool value);
+
 extern void bt_field_integer_signed_set_value(bt_field *field,
                int64_t value);
 
index 42eef20864a864f4998bde9dc6f662657e4a86b4..8b878655c77e1b6a0f22f5031e44351b1f9a43c0 100644 (file)
@@ -352,6 +352,13 @@ static inline void format_field(char **buf_ch, bool extended,
        }
 
        switch (field->class->type) {
+       case BT_FIELD_CLASS_TYPE_BOOL:
+       {
+               const struct bt_field_bool *bool_field = (const void *) field;
+
+               BUF_APPEND(", %svalue=%d", PRFIELD(bool_field->value));
+               break;
+       }
        case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
        case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
        case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
index ffb972f4aa083058f093f3e5326ce39fd00e74f6..6afc32b82acbb2a57ddbec6e257a7f8a9166c18e 100644 (file)
@@ -64,6 +64,40 @@ void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type,
        fc->type = type;
 }
 
+static
+void destroy_bool_field_class(struct bt_object *obj)
+{
+       BT_ASSERT(obj);
+       BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj);
+       g_free(obj);
+}
+
+struct bt_field_class *bt_field_class_bool_create(
+               bt_trace_class *trace_class)
+{
+       struct bt_field_class_bool *bool_fc = NULL;
+
+       BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
+       BT_LOGD("Creating default boolean field class object.");
+       bool_fc = g_new0(struct bt_field_class_bool, 1);
+       if (!bool_fc) {
+               BT_LIB_LOGE_APPEND_CAUSE(
+                       "Failed to allocate one boolean field class.");
+               goto error;
+       }
+
+       init_field_class((void *) bool_fc, BT_FIELD_CLASS_TYPE_BOOL,
+               destroy_bool_field_class);
+       BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc);
+       goto end;
+
+error:
+       BT_OBJECT_PUT_REF_AND_RESET(bool_fc);
+
+end:
+       return (void *) bool_fc;
+}
+
 static
 void init_integer_field_class(struct bt_field_class_integer *fc,
                enum bt_field_class_type type,
index e35b39652c21c7570348071f66e017e7e60fd3c5..53041a1315608738f1864b3885d3eee90b15a8c0 100644 (file)
@@ -186,6 +186,10 @@ struct bt_field_class {
        bool part_of_trace_class;
 };
 
+struct bt_field_class_bool {
+       struct bt_field_class common;
+};
+
 struct bt_field_class_integer {
        struct bt_field_class common;
 
index 26160c20d0d0ca013e29d0a0bd0f1102ca4c8556..95a76e49a95ee4177cf5ca68225b894141dc3fa6 100644 (file)
@@ -74,6 +74,13 @@ bool structure_field_is_set(const struct bt_field *field);
 static
 bool variant_field_is_set(const struct bt_field *field);
 
+static
+struct bt_field_methods bool_field_methods = {
+       .set_is_frozen = set_single_field_is_frozen,
+       .is_set = single_field_is_set,
+       .reset = reset_single_field,
+};
+
 static
 struct bt_field_methods integer_field_methods = {
        .set_is_frozen = set_single_field_is_frozen,
@@ -116,6 +123,9 @@ struct bt_field_methods variant_field_methods = {
        .reset = reset_variant_field,
 };
 
+static
+struct bt_field *create_bool_field(struct bt_field_class *);
+
 static
 struct bt_field *create_integer_field(struct bt_field_class *);
 
@@ -139,6 +149,7 @@ struct bt_field *create_variant_field(struct bt_field_class *);
 
 static
 struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = {
+       [BT_FIELD_CLASS_TYPE_BOOL]                              = create_bool_field,
        [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER]                  = create_integer_field,
        [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER]                    = create_integer_field,
        [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION]              = create_integer_field,
@@ -153,6 +164,9 @@ struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = {
        [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR]      = create_variant_field,
 };
 
+static
+void destroy_bool_field(struct bt_field *field);
+
 static
 void destroy_integer_field(struct bt_field *field);
 
@@ -173,6 +187,7 @@ void destroy_variant_field(struct bt_field *field);
 
 static
 void (* const field_destroy_funcs[])(struct bt_field *) = {
+       [BT_FIELD_CLASS_TYPE_BOOL]                              = destroy_bool_field,
        [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER]                  = destroy_integer_field,
        [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER]                    = destroy_integer_field,
        [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION]              = destroy_integer_field,
@@ -235,6 +250,26 @@ void init_field(struct bt_field *field, struct bt_field_class *fc,
        bt_object_get_no_null_check(fc);
 }
 
+static
+struct bt_field *create_bool_field(struct bt_field_class *fc)
+{
+       struct bt_field_bool *bool_field;
+
+       BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc);
+       bool_field = g_new0(struct bt_field_bool, 1);
+       if (!bool_field) {
+               BT_LIB_LOGE_APPEND_CAUSE(
+                       "Failed to allocate one boolean field.");
+               goto end;
+       }
+
+       init_field((void *) bool_field, fc, &bool_field_methods);
+       BT_LIB_LOGD("Created boolean field object: %!+f", bool_field);
+
+end:
+       return (void *) bool_field;
+}
+
 static
 struct bt_field *create_integer_field(struct bt_field_class *fc)
 {
@@ -494,6 +529,29 @@ end:
        return (void *) array_field;
 }
 
+bt_bool bt_field_bool_get_value(const struct bt_field *field)
+{
+       const struct bt_field_bool *bool_field = (const void *) field;
+
+       BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
+       BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
+       BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL,
+               "Field");
+       return (bt_bool) bool_field->value;
+}
+
+void bt_field_bool_set_value(struct bt_field *field, bt_bool value)
+{
+       struct bt_field_bool *bool_field = (void *) field;
+
+       BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
+       BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL,
+               "Field");
+       BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
+       bool_field->value = (bool) value;
+       bt_field_set_single(field, true);
+}
+
 int64_t bt_field_integer_signed_get_value(const struct bt_field *field)
 {
        const struct bt_field_integer *int_field = (const void *) field;
@@ -952,6 +1010,15 @@ void bt_field_finalize(struct bt_field *field)
        BT_OBJECT_PUT_REF_AND_RESET(field->class);
 }
 
+static
+void destroy_bool_field(struct bt_field *field)
+{
+       BT_ASSERT(field);
+       BT_LIB_LOGD("Destroying boolean field object: %!+f", field);
+       bt_field_finalize(field);
+       g_free(field);
+}
+
 static
 void destroy_integer_field(struct bt_field *field)
 {
index ce595f9726a100c263c38db2aac6dfb161b1a8e9..352514effc2f52dc13b321eb41014bdc08a85fc3 100644 (file)
@@ -105,6 +105,11 @@ struct bt_field {
        bool frozen;
 };
 
+struct bt_field_bool {
+       struct bt_field common;
+       bool value;
+};
+
 struct bt_field_integer {
        struct bt_field common;
 
This page took 0.029947 seconds and 4 git commands to generate.