Make API CTF-agnostic
[babeltrace.git] / include / babeltrace / ctf-ir / fields-internal.h
index 22d551a59576fd8b939b24e6e7d1c61e4955200c..31ec621329b1f8b7afa7d662d43c5c131648a062 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-ir/field-types.h>
-#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/assert-pre-internal.h>
+#include <babeltrace/common-internal.h>
+#include <babeltrace/ctf-ir/field-types-internal.h>
+#include <babeltrace/ctf-ir/utils-internal.h>
 #include <babeltrace/object-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/types.h>
 #include <stdint.h>
+#include <string.h>
+#include <inttypes.h>
 #include <stdbool.h>
 #include <glib.h>
 
-struct bt_ctf_stream_pos;
+#define BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(_field, _type_id, _name)       \
+       BT_ASSERT_PRE(((struct bt_field *) (_field))->type->id == (_type_id), \
+               _name " has the wrong type ID: expected-type-id=%s, "   \
+               "%![field-]+f",                                         \
+               bt_common_field_type_id_string(_type_id), (_field))
+
+#define BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(_field, _name)             \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \
+               ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION, \
+               _name " is not an unsigned integer field: %![field-]+f", \
+               (_field))
+
+#define BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(_field, _name)               \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_SIGNED_INTEGER || \
+               ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \
+               _name " is not a signed integer field: %![field-]+f", \
+               (_field))
+
+#define BT_ASSERT_PRE_FIELD_IS_ARRAY(_field, _name)                    \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_STATIC_ARRAY || \
+               ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_DYNAMIC_ARRAY, \
+               _name " is not an array field: %![field-]+f", (_field))
+
+#define BT_ASSERT_PRE_FIELD_IS_SET(_field, _name)                      \
+       BT_ASSERT_PRE(bt_field_is_set(_field),                          \
+               _name " is not set: %!+f", (_field))
+
+#define BT_ASSERT_PRE_FIELD_HOT(_field, _name)                         \
+       BT_ASSERT_PRE_HOT((struct bt_field *) (_field), (_name),        \
+               ": %!+f", (_field))
+
+struct bt_field;
+
+typedef struct bt_field *(* bt_field_create_func)(struct bt_field_type *);
+typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool);
+typedef bool (*bt_field_method_is_set)(struct bt_field *);
+typedef void (*bt_field_method_reset)(struct bt_field *);
+
+struct bt_field_methods {
+       bt_field_method_set_is_frozen set_is_frozen;
+       bt_field_method_is_set is_set;
+       bt_field_method_reset reset;
+};
 
-struct bt_ctf_field {
+struct bt_field {
        struct bt_object base;
-       struct bt_ctf_field_type *type;
-       bool payload_set;
+
+       /* Owned by this */
+       struct bt_field_type *type;
+
+       /* Virtual table for slow path (dev mode) operations */
+       struct bt_field_methods *methods;
+
+       bool is_set;
        bool frozen;
 };
 
-struct bt_ctf_field_integer {
-       struct bt_ctf_field parent;
+struct bt_field_integer {
+       struct bt_field common;
+
        union {
-               int64_t signd;
-               uint64_t unsignd;
-       } payload;
+               uint64_t u;
+               int64_t i;
+       } value;
 };
 
-struct bt_ctf_field_enumeration {
-       struct bt_ctf_field parent;
-       struct bt_ctf_field *payload;
+struct bt_field_real {
+       struct bt_field common;
+       double value;
 };
 
-struct bt_ctf_field_floating_point {
-       struct bt_ctf_field parent;
-       double payload;
-};
+struct bt_field_structure {
+       struct bt_field common;
 
-struct bt_ctf_field_structure {
-       struct bt_ctf_field parent;
-       GHashTable *field_name_to_index;
-       GPtrArray *fields; /* Array of pointers to struct bt_ctf_field */
+       /* Array of `struct bt_field *`, owned by this */
+       GPtrArray *fields;
 };
 
-struct bt_ctf_field_variant {
-       struct bt_ctf_field parent;
-       struct bt_ctf_field *tag;
-       struct bt_ctf_field *payload;
-};
+struct bt_field_variant {
+       struct bt_field common;
 
-struct bt_ctf_field_array {
-       struct bt_ctf_field parent;
-       GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */
-};
+       /* Weak: belongs to `fields` below */
+       struct bt_field *selected_field;
+
+       /* Index of currently selected field */
+       uint64_t selected_index;
 
-struct bt_ctf_field_sequence {
-       struct bt_ctf_field parent;
-       struct bt_ctf_field *length;
-       GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */
+       /* Array of `struct bt_field *`, owned by this */
+       GPtrArray *fields;
 };
 
-struct bt_ctf_field_string {
-       struct bt_ctf_field parent;
-       GString *payload;
+struct bt_field_array {
+       struct bt_field common;
+
+       /* Array of `struct bt_field *`, owned by this */
+       GPtrArray *fields;
+
+       /* Current effective length */
+       uint64_t length;
 };
 
-/* Validate that the field's payload is set (returns 0 if set). */
-BT_HIDDEN
-int bt_ctf_field_validate(struct bt_ctf_field *field);
+struct bt_field_string {
+       struct bt_field common;
+       GArray *buf;
+       uint64_t length;
+};
 
-/* Mark field payload as unset. */
-BT_HIDDEN
-int bt_ctf_field_reset(struct bt_ctf_field *field);
+#ifdef BT_DEV_MODE
+# define bt_field_set_is_frozen                _bt_field_set_is_frozen
+# define bt_field_is_set               _bt_field_is_set
+# define bt_field_reset                        _bt_field_reset
+# define bt_field_set_single           _bt_field_set_single
+#else
+# define bt_field_set_is_frozen(_field, _is_frozen)
+# define bt_field_is_set(_field)       (BT_FALSE)
+# define bt_field_reset(_field)
+# define bt_field_set_single(_field, _val)
+#endif
 
 BT_HIDDEN
-int bt_ctf_field_serialize(struct bt_ctf_field *field,
-               struct bt_ctf_stream_pos *pos,
-               enum bt_ctf_byte_order native_byte_order);
+void _bt_field_set_is_frozen(struct bt_field *field, bool is_frozen);
+
+static inline
+void _bt_field_reset(struct bt_field *field)
+{
+       BT_ASSERT(field);
+       BT_ASSERT(field->methods->reset);
+       field->methods->reset(field);
+}
+
+static inline
+void _bt_field_set_single(struct bt_field *field, bool value)
+{
+       BT_ASSERT(field);
+       field->is_set = value;
+}
+
+static inline
+bt_bool _bt_field_is_set(struct bt_field *field)
+{
+       bt_bool is_set = BT_FALSE;
+
+       if (!field) {
+               goto end;
+       }
+
+       BT_ASSERT(bt_field_type_has_known_id(field->type));
+       BT_ASSERT(field->methods->is_set);
+       is_set = field->methods->is_set(field);
+
+end:
+       return is_set;
+}
 
 BT_HIDDEN
-void bt_ctf_field_freeze(struct bt_ctf_field *field);
+struct bt_field *bt_field_create(struct bt_field_type *type);
 
 BT_HIDDEN
-bt_bool bt_ctf_field_is_set(struct bt_ctf_field *field);
+void bt_field_destroy(struct bt_field *field);
 
 #endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */
This page took 0.027704 seconds and 4 git commands to generate.