lib: merge common CTF IR part with the remaining implementation
[babeltrace.git] / include / babeltrace / ctf-ir / field-types-internal.h
index 129446bd9f20ffddc6ad1b29ddf218c62737c866..2acb5bbbe41193b6706579130d16cc5211bdd3f9 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-writer/event-fields.h>
-#include <babeltrace/ctf-writer/writer.h>
-#include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/assert-pre-internal.h>
 #include <babeltrace/ctf-ir/clock-class.h>
+#include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/object-internal.h>
+#include <babeltrace/types.h>
+#include <stdint.h>
 #include <glib.h>
 
-typedef void (*type_freeze_func)(struct bt_ctf_field_type *);
-typedef int (*type_serialize_func)(struct bt_ctf_field_type *,
-               struct metadata_context *);
+#define BT_ASSERT_PRE_FT_HAS_ID(_ft, _type_id, _name)          \
+       BT_ASSERT_PRE(((struct bt_field_type *) (_ft))->id == (_type_id), \
+               _name " has the wrong type ID: expected-type-id=%s, "   \
+               "%![ft-]+F", bt_common_field_type_id_string(_type_id), (_ft))
+
+#define BT_ASSERT_PRE_FT_HOT(_ft, _name)                               \
+       BT_ASSERT_PRE_HOT((_ft), (_name), ": %!+F", (_ft))
+
+#define BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(_ft, _index)    \
+       (&g_array_index(((struct bt_field_type_structure *) (_ft))->fields, \
+               struct bt_field_type_structure_field, (_index)))
+
+#define BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(_ft, _index)     \
+       (&g_array_index(((struct bt_field_type_variant *) (_ft))->choices, \
+               struct bt_field_type_variant_choice, (_index)))
+
+struct bt_field;
+struct bt_field_type;
+
+typedef void (*bt_field_type_method_freeze)(
+               struct bt_field_type *);
+typedef int (*bt_field_type_method_validate)(
+               struct bt_field_type *);
+typedef void (*bt_field_type_method_set_byte_order)(
+               struct bt_field_type *, enum bt_byte_order);
+typedef struct bt_field_type *(*bt_field_type_method_copy)(
+               struct bt_field_type *);
+typedef int (*bt_field_type_method_compare)(
+               struct bt_field_type *,
+               struct bt_field_type *);
+
+struct bt_field_type_methods {
+       bt_field_type_method_freeze freeze;
+       bt_field_type_method_validate validate;
+       bt_field_type_method_set_byte_order set_byte_order;
+       bt_field_type_method_copy copy;
+       bt_field_type_method_compare compare;
+};
 
-struct bt_ctf_field_type {
+struct bt_field_type {
        struct bt_object base;
-       enum bt_ctf_field_type_id id;
+       enum bt_field_type_id id;
        unsigned int alignment;
-       type_freeze_func freeze;
-       type_serialize_func serialize;
+
+       /* Virtual table */
+       struct bt_field_type_methods *methods;
+
        /*
         * A type can't be modified once it is added to an event or after a
         * a field has been instanciated from it.
@@ -60,14 +97,17 @@ struct bt_ctf_field_type {
        int valid;
 };
 
-struct bt_ctf_field_type_integer {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_clock_class *mapped_clock;
-       enum bt_ctf_byte_order user_byte_order;
-       bool is_signed;
+struct bt_field_type_integer {
+       struct bt_field_type common;
+
+       /* Owned by this */
+       struct bt_clock_class *mapped_clock_class;
+
+       enum bt_byte_order user_byte_order;
+       bt_bool is_signed;
        unsigned int size;
-       enum bt_ctf_integer_base base;
-       enum bt_ctf_string_encoding encoding;
+       enum bt_integer_base base;
+       enum bt_string_encoding encoding;
 };
 
 struct enumeration_mapping {
@@ -75,7 +115,6 @@ struct enumeration_mapping {
                uint64_t _unsigned;
                int64_t _signed;
        } range_start;
-
        union {
                uint64_t _unsigned;
                int64_t _signed;
@@ -83,24 +122,32 @@ struct enumeration_mapping {
        GQuark string;
 };
 
-struct bt_ctf_field_type_enumeration {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_field_type *container;
-       GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */
-       /* Only set during validation. */
-       bool has_overlapping_ranges;
+struct bt_field_type_enumeration {
+       struct bt_field_type common;
+
+       /* Owned by this */
+       struct bt_field_type_integer *container_ft;
+
+       /* Array of `struct enumeration_mapping *`, owned by this */
+       GPtrArray *entries;
+
+       /* Only set during validation */
+       bt_bool has_overlapping_ranges;
 };
 
-enum bt_ctf_field_type_enumeration_mapping_iterator_type {
+enum bt_field_type_enumeration_mapping_iterator_type {
        ITERATOR_BY_NAME,
        ITERATOR_BY_SIGNED_VALUE,
        ITERATOR_BY_UNSIGNED_VALUE,
 };
 
-struct bt_ctf_field_type_enumeration_mapping_iterator {
+struct bt_field_type_enumeration_mapping_iterator {
        struct bt_object base;
-       struct bt_ctf_field_type_enumeration *enumeration_type;
-       enum bt_ctf_field_type_enumeration_mapping_iterator_type type;
+
+       /* Owned by this */
+       struct bt_field_type_enumeration *enumeration_ft;
+
+       enum bt_field_type_enumeration_mapping_iterator_type type;
        int index;
        union {
                GQuark name_quark;
@@ -109,119 +156,151 @@ struct bt_ctf_field_type_enumeration_mapping_iterator {
        } u;
 };
 
-struct bt_ctf_field_type_floating_point {
-       struct bt_ctf_field_type parent;
-       enum bt_ctf_byte_order user_byte_order;
+struct bt_field_type_floating_point {
+       struct bt_field_type common;
+       enum bt_byte_order user_byte_order;
        unsigned int exp_dig;
        unsigned int mant_dig;
 };
 
-struct structure_field {
+struct bt_field_type_structure_field {
        GQuark name;
-       struct bt_ctf_field_type *type;
+
+       /* Owned by this */
+       struct bt_field_type *type;
 };
 
-struct bt_ctf_field_type_structure {
-       struct bt_ctf_field_type parent;
+struct bt_field_type_structure {
+       struct bt_field_type common;
        GHashTable *field_name_to_index;
-       GPtrArray *fields; /* Array of pointers to struct structure_field */
+
+       /*
+        * Array of `struct bt_field_type_structure_field`,
+        * owned by this
+        */
+       GArray *fields;
 };
 
-struct bt_ctf_field_type_variant {
-       struct bt_ctf_field_type parent;
-       GString *tag_name;
-       struct bt_ctf_field_type_enumeration *tag;
-       struct bt_ctf_field_path *tag_field_path;
-       GHashTable *field_name_to_index;
-       GPtrArray *fields; /* Array of pointers to struct structure_field */
+struct bt_field_type_variant_choice_range {
+       union {
+               int64_t i;
+               uint64_t u;
+       } lower;
+       union {
+               int64_t i;
+               uint64_t u;
+       } upper;
 };
 
-struct bt_ctf_field_type_array {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_field_type *element_type;
-       unsigned int length; /* Number of elements */
+struct bt_field_type_variant_choice {
+       GQuark name;
+
+       /* Owned by this */
+       struct bt_field_type *type;
+
+       /* Array of `struct bt_field_type_variant_choice_range` */
+       GArray *ranges;
 };
 
-struct bt_ctf_field_type_sequence {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_field_type *element_type;
-       GString *length_field_name;
-       struct bt_ctf_field_path *length_field_path;
+struct bt_field_type_variant {
+       struct bt_field_type common;
+       GString *tag_name;
+       bool choices_up_to_date;
+
+       /* Owned by this */
+       struct bt_field_type_enumeration *tag_ft;
+
+       /* Owned by this */
+       struct bt_field_path *tag_field_path;
+
+       GHashTable *choice_name_to_index;
+
+       /*
+        * Array of `struct bt_field_type_variant_choice`,
+        * owned by this */
+       GArray *choices;
 };
 
-struct bt_ctf_field_type_string {
-       struct bt_ctf_field_type parent;
-       enum bt_ctf_string_encoding encoding;
+struct bt_field_type_array {
+       struct bt_field_type common;
+
+       /* Owned by this */
+       struct bt_field_type *element_ft;
+
+       unsigned int length;
 };
 
-BT_HIDDEN
-void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type);
+struct bt_field_type_sequence {
+       struct bt_field_type common;
 
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
-               struct bt_ctf_field_type_variant *variant, int64_t tag_value);
+       /* Owned by this */
+       struct bt_field_type *element_ft;
 
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
-               struct bt_ctf_field_type_variant *variant, uint64_t tag_value);
+       GString *length_field_name;
 
-BT_HIDDEN
-int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
-               struct metadata_context *context);
+       /* Owned by this */
+       struct bt_field_path *length_field_path;
+};
 
-BT_HIDDEN
-int bt_ctf_field_type_validate(struct bt_ctf_field_type *type);
+struct bt_field_type_string {
+       struct bt_field_type common;
+       enum bt_string_encoding encoding;
+};
 
-BT_HIDDEN
-int bt_ctf_field_type_structure_get_field_name_index(
-               struct bt_ctf_field_type *structure, const char *name);
+typedef struct bt_field *(* bt_field_create_func)(
+               struct bt_field_type *);
+
+BT_ASSERT_FUNC
+static inline bool bt_field_type_has_known_id(
+               struct bt_field_type *ft)
+{
+       return (int) ft->id > BT_FIELD_TYPE_ID_UNKNOWN ||
+               (int) ft->id < BT_FIELD_TYPE_ID_NR;
+}
 
-/* Replace an existing field's type in a structure */
 BT_HIDDEN
-int bt_ctf_field_type_structure_set_field_index(
-               struct bt_ctf_field_type *structure,
-               struct bt_ctf_field_type *field, int index);
+int bt_field_type_variant_update_choices(
+               struct bt_field_type *ft);
 
 BT_HIDDEN
-int bt_ctf_field_type_variant_get_field_name_index(
-               struct bt_ctf_field_type *variant, const char *name);
+void bt_field_type_freeze(struct bt_field_type *ft);
 
 BT_HIDDEN
-int bt_ctf_field_type_sequence_set_length_field_path(
-               struct bt_ctf_field_type *type,
-               struct bt_ctf_field_path *path);
+int bt_field_type_validate(struct bt_field_type *ft);
 
 BT_HIDDEN
-int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
-               struct bt_ctf_field_path *path);
+int bt_field_type_sequence_set_length_field_path(
+               struct bt_field_type *ft, struct bt_field_path *path);
 
 BT_HIDDEN
-int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
-               struct bt_ctf_field_type *tag_type);
+int bt_field_type_variant_set_tag_field_path(
+               struct bt_field_type *ft,
+               struct bt_field_path *path);
 
-/* Replace an existing field's type in a variant */
 BT_HIDDEN
-int bt_ctf_field_type_variant_set_field_index(
-               struct bt_ctf_field_type *variant,
-               struct bt_ctf_field_type *field, int index);
+int bt_field_type_variant_set_tag_field_type(
+               struct bt_field_type *ft,
+               struct bt_field_type *tag_ft);
 
 BT_HIDDEN
-int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array,
-               struct bt_ctf_field_type *element_type);
+int64_t bt_field_type_get_field_count(struct bt_field_type *ft);
 
 BT_HIDDEN
-int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array,
-               struct bt_ctf_field_type *element_type);
+struct bt_field_type *bt_field_type_borrow_field_at_index(
+               struct bt_field_type *ft, int index);
 
 BT_HIDDEN
-int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type);
+int bt_field_type_get_field_index(struct bt_field_type *ft,
+               const char *name);
 
 BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
-               struct bt_ctf_field_type *type, int index);
+int bt_field_type_validate_single_clock_class(
+               struct bt_field_type *ft,
+               struct bt_clock_class **expected_clock_class);
 
 BT_HIDDEN
-int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type,
-               const char *name);
+int64_t bt_field_type_variant_find_choice_index(
+               struct bt_field_type *ft, uint64_t uval,
+               bool is_signed);
 
 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */
This page took 0.032165 seconds and 4 git commands to generate.