tap-driver.sh: flush stdout after each test result
[babeltrace.git] / lib / value.c
index 83168e74fe9cd99c22922342a219684865a31feb..6ec7497dac3b83f6fa28343dd6f5959852e5e438 100644 (file)
  */
 
 #define BT_LOG_TAG "VALUES"
-#include <babeltrace/lib-logging-internal.h>
+#include <babeltrace2/lib-logging-internal.h>
 
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
 #include <inttypes.h>
-#include <babeltrace/compiler-internal.h>
-#include <babeltrace/common-internal.h>
-#include <babeltrace/value-const.h>
-#include <babeltrace/value.h>
-#include <babeltrace/compat/glib-internal.h>
-#include <babeltrace/types.h>
-#include <babeltrace/assert-pre-internal.h>
-#include <babeltrace/object-internal.h>
-#include <babeltrace/value-internal.h>
-#include <babeltrace/assert-internal.h>
-
-#define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
+#include <babeltrace2/compiler-internal.h>
+#include <babeltrace2/common-internal.h>
+#include <babeltrace2/value-const.h>
+#include <babeltrace2/value.h>
+#include <babeltrace2/compat/glib-internal.h>
+#include <babeltrace2/types.h>
+#include <babeltrace2/assert-pre-internal.h>
+#include <babeltrace2/value-internal.h>
+#include <babeltrace2/assert-internal.h>
+
 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
                "Index is out of bound: "                               \
                "index=%" PRIu64 ", count=%u", (_index), (_count));
 
-struct bt_value {
-       struct bt_object base;
-       enum bt_value_type type;
-       bt_bool frozen;
-};
-
 static
 void bt_value_null_instance_release_func(struct bt_object *obj)
 {
@@ -89,36 +81,6 @@ struct bt_value bt_value_null_instance = {
 
 struct bt_value *const bt_value_null = &bt_value_null_instance;
 
-struct bt_value_bool {
-       struct bt_value base;
-       bt_bool value;
-};
-
-struct bt_value_integer {
-       struct bt_value base;
-       int64_t value;
-};
-
-struct bt_value_real {
-       struct bt_value base;
-       double value;
-};
-
-struct bt_value_string {
-       struct bt_value base;
-       GString *gstr;
-};
-
-struct bt_value_array {
-       struct bt_value base;
-       GPtrArray *garray;
-};
-
-struct bt_value_map {
-       struct bt_value base;
-       GHashTable *ght;
-};
-
 static
 void bt_value_destroy(struct bt_object *obj);
 
@@ -154,13 +116,14 @@ void bt_value_map_destroy(struct bt_value *object)
 
 static
 void (* const destroy_funcs[])(struct bt_value *) = {
-       [BT_VALUE_TYPE_NULL] =          NULL,
-       [BT_VALUE_TYPE_BOOL] =          NULL,
-       [BT_VALUE_TYPE_INTEGER] =       NULL,
-       [BT_VALUE_TYPE_REAL] =          NULL,
-       [BT_VALUE_TYPE_STRING] =        bt_value_string_destroy,
-       [BT_VALUE_TYPE_ARRAY] =         bt_value_array_destroy,
-       [BT_VALUE_TYPE_MAP] =           bt_value_map_destroy,
+       [BT_VALUE_TYPE_NULL] =                  NULL,
+       [BT_VALUE_TYPE_BOOL] =                  NULL,
+       [BT_VALUE_TYPE_UNSIGNED_INTEGER] =      NULL,
+       [BT_VALUE_TYPE_SIGNED_INTEGER] =        NULL,
+       [BT_VALUE_TYPE_REAL] =                  NULL,
+       [BT_VALUE_TYPE_STRING] =                bt_value_string_destroy,
+       [BT_VALUE_TYPE_ARRAY] =                 bt_value_array_destroy,
+       [BT_VALUE_TYPE_MAP] =                   bt_value_map_destroy,
 };
 
 static
@@ -176,12 +139,16 @@ struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj)
                BT_VALUE_TO_BOOL(bool_obj)->value);
 }
 
+static inline
+struct bt_value *bt_value_integer_create_init(enum bt_value_type type,
+               uint64_t uval);
+
 static
 struct bt_value *bt_value_integer_copy(
                const struct bt_value *integer_obj)
 {
-       return bt_value_integer_create_init(
-               BT_VALUE_TO_INTEGER(integer_obj)->value);
+       return bt_value_integer_create_init(integer_obj->type,
+               BT_VALUE_TO_INTEGER(integer_obj)->value.u);
 }
 
 static
@@ -305,13 +272,14 @@ end:
 
 static
 struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
-       [BT_VALUE_TYPE_NULL] =          bt_value_null_copy,
-       [BT_VALUE_TYPE_BOOL] =          bt_value_bool_copy,
-       [BT_VALUE_TYPE_INTEGER] =       bt_value_integer_copy,
-       [BT_VALUE_TYPE_REAL] =          bt_value_real_copy,
-       [BT_VALUE_TYPE_STRING] =        bt_value_string_copy,
-       [BT_VALUE_TYPE_ARRAY] =         bt_value_array_copy,
-       [BT_VALUE_TYPE_MAP] =           bt_value_map_copy,
+       [BT_VALUE_TYPE_NULL] =                  bt_value_null_copy,
+       [BT_VALUE_TYPE_BOOL] =                  bt_value_bool_copy,
+       [BT_VALUE_TYPE_UNSIGNED_INTEGER] =      bt_value_integer_copy,
+       [BT_VALUE_TYPE_SIGNED_INTEGER] =        bt_value_integer_copy,
+       [BT_VALUE_TYPE_REAL] =                  bt_value_real_copy,
+       [BT_VALUE_TYPE_STRING] =                bt_value_string_copy,
+       [BT_VALUE_TYPE_ARRAY] =                 bt_value_array_copy,
+       [BT_VALUE_TYPE_MAP] =                   bt_value_map_copy,
 };
 
 static
@@ -346,12 +314,20 @@ static
 bt_bool bt_value_integer_compare(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
-       if (BT_VALUE_TO_INTEGER(object_a)->value !=
-                       BT_VALUE_TO_INTEGER(object_b)->value) {
-               BT_LOGV("Integer value objects are different: "
-                       "int-a-val=%" PRId64 ", int-b-val=%" PRId64,
-                       BT_VALUE_TO_INTEGER(object_a)->value,
-                       BT_VALUE_TO_INTEGER(object_b)->value);
+       if (BT_VALUE_TO_INTEGER(object_a)->value.u !=
+                       BT_VALUE_TO_INTEGER(object_b)->value.u) {
+               if (object_a->type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
+                       BT_LOGV("Unsigned integer value objects are different: "
+                               "int-a-val=%" PRIu64 ", int-b-val=%" PRIu64,
+                               BT_VALUE_TO_INTEGER(object_a)->value.u,
+                               BT_VALUE_TO_INTEGER(object_b)->value.u);
+               } else {
+                       BT_LOGV("Signed integer value objects are different: "
+                               "int-a-val=%" PRId64 ", int-b-val=%" PRId64,
+                               BT_VALUE_TO_INTEGER(object_a)->value.i,
+                               BT_VALUE_TO_INTEGER(object_b)->value.i);
+               }
+
                return BT_FALSE;
        }
 
@@ -479,13 +455,14 @@ end:
 static
 bt_bool (* const compare_funcs[])(const struct bt_value *,
                const struct bt_value *) = {
-       [BT_VALUE_TYPE_NULL] =          bt_value_null_compare,
-       [BT_VALUE_TYPE_BOOL] =          bt_value_bool_compare,
-       [BT_VALUE_TYPE_INTEGER] =       bt_value_integer_compare,
-       [BT_VALUE_TYPE_REAL] =          bt_value_real_compare,
-       [BT_VALUE_TYPE_STRING] =        bt_value_string_compare,
-       [BT_VALUE_TYPE_ARRAY] =         bt_value_array_compare,
-       [BT_VALUE_TYPE_MAP] =           bt_value_map_compare,
+       [BT_VALUE_TYPE_NULL] =                  bt_value_null_compare,
+       [BT_VALUE_TYPE_BOOL] =                  bt_value_bool_compare,
+       [BT_VALUE_TYPE_UNSIGNED_INTEGER] =      bt_value_integer_compare,
+       [BT_VALUE_TYPE_SIGNED_INTEGER] =        bt_value_integer_compare,
+       [BT_VALUE_TYPE_REAL] =                  bt_value_real_compare,
+       [BT_VALUE_TYPE_STRING] =                bt_value_string_compare,
+       [BT_VALUE_TYPE_ARRAY] =                 bt_value_array_compare,
+       [BT_VALUE_TYPE_MAP] =                   bt_value_map_compare,
 };
 
 static
@@ -531,13 +508,14 @@ void bt_value_map_freeze(struct bt_value *object)
 
 static
 void (* const freeze_funcs[])(struct bt_value *) = {
-       [BT_VALUE_TYPE_NULL] =          bt_value_null_freeze,
-       [BT_VALUE_TYPE_BOOL] =          bt_value_generic_freeze,
-       [BT_VALUE_TYPE_INTEGER] =       bt_value_generic_freeze,
-       [BT_VALUE_TYPE_REAL] =          bt_value_generic_freeze,
-       [BT_VALUE_TYPE_STRING] =        bt_value_generic_freeze,
-       [BT_VALUE_TYPE_ARRAY] =         bt_value_array_freeze,
-       [BT_VALUE_TYPE_MAP] =           bt_value_map_freeze,
+       [BT_VALUE_TYPE_NULL] =                  bt_value_null_freeze,
+       [BT_VALUE_TYPE_BOOL] =                  bt_value_generic_freeze,
+       [BT_VALUE_TYPE_UNSIGNED_INTEGER] =      bt_value_generic_freeze,
+       [BT_VALUE_TYPE_SIGNED_INTEGER] =        bt_value_generic_freeze,
+       [BT_VALUE_TYPE_REAL] =                  bt_value_generic_freeze,
+       [BT_VALUE_TYPE_STRING] =                bt_value_generic_freeze,
+       [BT_VALUE_TYPE_ARRAY] =                 bt_value_array_freeze,
+       [BT_VALUE_TYPE_MAP] =                   bt_value_map_freeze,
 };
 
 static
@@ -612,7 +590,7 @@ struct bt_value *bt_value_bool_create_init(bt_bool val)
        BT_LOGD("Created boolean value object: addr=%p", bool_obj);
 
 end:
-       return (void *) BT_VALUE_FROM_CONCRETE(bool_obj);
+       return (void *) bool_obj;
 }
 
 struct bt_value *bt_value_bool_create(void)
@@ -620,29 +598,59 @@ struct bt_value *bt_value_bool_create(void)
        return bt_value_bool_create_init(BT_FALSE);
 }
 
-struct bt_value *bt_value_integer_create_init(int64_t val)
+static inline
+struct bt_value *bt_value_integer_create_init(enum bt_value_type type,
+               uint64_t uval)
 {
        struct bt_value_integer *integer_obj;
 
-       BT_LOGD("Creating integer value object: val=%" PRId64, val);
+       BT_ASSERT(type == BT_VALUE_TYPE_UNSIGNED_INTEGER ||
+               type == BT_VALUE_TYPE_SIGNED_INTEGER);
+
+       if (type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
+               BT_LOGD("Creating unsigned integer value object: val=%" PRIu64,
+                       uval);
+       } else {
+               BT_LOGD("Creating signed integer value object: val=%" PRId64,
+                       (int64_t) uval);
+       }
+
        integer_obj = g_new0(struct bt_value_integer, 1);
        if (!integer_obj) {
                BT_LOGE_STR("Failed to allocate one integer value object.");
                goto end;
        }
 
-       integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER);
-       integer_obj->value = val;
-       BT_LOGD("Created integer value object: addr=%p",
+       integer_obj->base = bt_value_create_base(type);
+       integer_obj->value.u = uval;
+       BT_LOGD("Created %ssigned integer value object: addr=%p",
+               type == BT_VALUE_TYPE_UNSIGNED_INTEGER ? "un" : "",
                integer_obj);
 
 end:
-       return (void *) BT_VALUE_FROM_CONCRETE(integer_obj);
+       return (void *) integer_obj;
+}
+
+struct bt_value *bt_value_unsigned_integer_create_init(uint64_t val)
+{
+       return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER,
+               val);
+}
+
+struct bt_value *bt_value_unsigned_integer_create(void)
+{
+       return bt_value_unsigned_integer_create_init(0);
 }
 
-struct bt_value *bt_value_integer_create(void)
+struct bt_value *bt_value_signed_integer_create_init(int64_t val)
 {
-       return bt_value_integer_create_init(0);
+       return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER,
+               (uint64_t) val);
+}
+
+struct bt_value *bt_value_signed_integer_create(void)
+{
+       return bt_value_signed_integer_create_init(0);
 }
 
 struct bt_value *bt_value_real_create_init(double val)
@@ -662,7 +670,7 @@ struct bt_value *bt_value_real_create_init(double val)
                real_obj);
 
 end:
-       return (void *) BT_VALUE_FROM_CONCRETE(real_obj);
+       return (void *) real_obj;
 }
 
 struct bt_value *bt_value_real_create(void)
@@ -699,7 +707,7 @@ struct bt_value *bt_value_string_create_init(const char *val)
                string_obj);
 
 end:
-       return (void *) BT_VALUE_FROM_CONCRETE(string_obj);
+       return (void *) string_obj;
 }
 
 struct bt_value *bt_value_string_create(void)
@@ -732,7 +740,7 @@ struct bt_value *bt_value_array_create(void)
                array_obj);
 
 end:
-       return (void *) BT_VALUE_FROM_CONCRETE(array_obj);
+       return (void *) array_obj;
 }
 
 struct bt_value *bt_value_map_create(void)
@@ -760,7 +768,7 @@ struct bt_value *bt_value_map_create(void)
                map_obj);
 
 end:
-       return (void *) BT_VALUE_FROM_CONCRETE(map_obj);
+       return (void *) map_obj;
 }
 
 bt_bool bt_value_bool_get(const struct bt_value *bool_obj)
@@ -780,22 +788,47 @@ void bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
                bool_obj, val);
 }
 
-int64_t bt_value_integer_get(const struct bt_value *integer_obj)
+uint64_t bt_value_unsigned_integer_get(const struct bt_value *integer_obj)
 {
        BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
-       BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER);
-       return BT_VALUE_TO_INTEGER(integer_obj)->value;
+       BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj,
+               BT_VALUE_TYPE_UNSIGNED_INTEGER);
+       return BT_VALUE_TO_INTEGER(integer_obj)->value.u;
 }
 
+int64_t bt_value_signed_integer_get(const struct bt_value *integer_obj)
+{
+       BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
+       BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj,
+               BT_VALUE_TYPE_SIGNED_INTEGER);
+       return BT_VALUE_TO_INTEGER(integer_obj)->value.i;
+}
+
+static inline
 void bt_value_integer_set(struct bt_value *integer_obj,
-               int64_t val)
+               enum bt_value_type expected_type, uint64_t uval)
 {
        BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
-       BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER);
+       BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, expected_type);
        BT_ASSERT_PRE_VALUE_HOT(integer_obj, "Value object");
-       BT_VALUE_TO_INTEGER(integer_obj)->value = val;
-       BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64,
-               integer_obj, val);
+       BT_VALUE_TO_INTEGER(integer_obj)->value.u = uval;
+}
+
+void bt_value_unsigned_integer_set(struct bt_value *integer_obj,
+               uint64_t val)
+{
+       bt_value_integer_set(integer_obj, BT_VALUE_TYPE_UNSIGNED_INTEGER, val);
+       BT_LOGV("Set unsigned integer value's raw value: "
+               "value-addr=%p, value=%" PRIu64, integer_obj, val);
+}
+
+void bt_value_signed_integer_set(struct bt_value *integer_obj,
+               int64_t val)
+{
+       bt_value_integer_set(integer_obj, BT_VALUE_TYPE_SIGNED_INTEGER,
+               (uint64_t) val);
+       BT_LOGV("Set signed integer value's raw value: "
+               "value-addr=%p, value=%" PRId64, integer_obj, val);
 }
 
 double bt_value_real_get(const struct bt_value *real_obj)
@@ -894,13 +927,26 @@ enum bt_value_status bt_value_array_append_bool_element(
        return ret;
 }
 
-enum bt_value_status bt_value_array_append_integer_element(
+enum bt_value_status bt_value_array_append_unsigned_integer_element(
+               struct bt_value *array_obj, uint64_t val)
+{
+       enum bt_value_status ret;
+       struct bt_value *integer_obj = NULL;
+
+       integer_obj = bt_value_unsigned_integer_create_init(val);
+       ret = bt_value_array_append_element(array_obj,
+               (void *) integer_obj);
+       bt_object_put_ref(integer_obj);
+       return ret;
+}
+
+enum bt_value_status bt_value_array_append_signed_integer_element(
                struct bt_value *array_obj, int64_t val)
 {
        enum bt_value_status ret;
        struct bt_value *integer_obj = NULL;
 
-       integer_obj = bt_value_integer_create_init(val);
+       integer_obj = bt_value_signed_integer_create_init(val);
        ret = bt_value_array_append_element(array_obj,
                (void *) integer_obj);
        bt_object_put_ref(integer_obj);
@@ -1044,13 +1090,26 @@ enum bt_value_status bt_value_map_insert_bool_entry(
        return ret;
 }
 
-enum bt_value_status bt_value_map_insert_integer_entry(
+enum bt_value_status bt_value_map_insert_unsigned_integer_entry(
+               struct bt_value *map_obj, const char *key, uint64_t val)
+{
+       enum bt_value_status ret;
+       struct bt_value *integer_obj = NULL;
+
+       integer_obj = bt_value_unsigned_integer_create_init(val);
+       ret = bt_value_map_insert_entry(map_obj, key,
+               (void *) integer_obj);
+       bt_object_put_ref(integer_obj);
+       return ret;
+}
+
+enum bt_value_status bt_value_map_insert_signed_integer_entry(
                struct bt_value *map_obj, const char *key, int64_t val)
 {
        enum bt_value_status ret;
        struct bt_value *integer_obj = NULL;
 
-       integer_obj = bt_value_integer_create_init(val);
+       integer_obj = bt_value_signed_integer_create_init(val);
        ret = bt_value_map_insert_entry(map_obj, key,
                (void *) integer_obj);
        bt_object_put_ref(integer_obj);
This page took 0.028815 seconds and 4 git commands to generate.