Make API CTF-agnostic
[babeltrace.git] / lib / ctf-ir / event-class.c
index 5ff9d0d481c06feea7c9df1ef6ee6dd46509461d..b8824082c7c62eaadec7fc0ec2c08dd4837f5b9f 100644 (file)
 #define BT_LOG_TAG "EVENT-CLASS"
 #include <babeltrace/lib-logging-internal.h>
 
+#include <babeltrace/assert-pre-internal.h>
+#include <babeltrace/ctf-ir/clock-value-internal.h>
 #include <babeltrace/ctf-ir/fields-internal.h>
+#include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/field-types-internal.h>
 #include <babeltrace/ctf-ir/event-class.h>
 #include <babeltrace/ctf-ir/event-class-internal.h>
+#include <babeltrace/ctf-ir/event-internal.h>
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
-#include <babeltrace/ctf-ir/validation-internal.h>
-#include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/ctf-ir/utils-internal.h>
+#include <babeltrace/ctf-ir/resolve-field-path-internal.h>
 #include <babeltrace/ref.h>
 #include <babeltrace/ctf-ir/attributes-internal.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/types.h>
 #include <babeltrace/values-internal.h>
 #include <babeltrace/assert-internal.h>
-#include <babeltrace/assert-pre-internal.h>
 #include <inttypes.h>
 #include <stdlib.h>
 
-static
-void bt_event_class_destroy(struct bt_object *obj);
+#define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \
+       BT_ASSERT_PRE_HOT((_ec), "Event class", ": %!+E", (_ec))
 
-struct bt_event_class *bt_event_class_create(const char *name)
+static
+void destroy_event_class(struct bt_object *obj)
 {
-       struct bt_value *obj = NULL;
-       struct bt_event_class *event_class = NULL;
+       struct bt_event_class *event_class = (void *) obj;
 
-       BT_LOGD("Creating event class object: name=\"%s\"",
-               name);
+       BT_LIB_LOGD("Destroying event class: %!+E", event_class);
 
-       if (!name) {
-               BT_LOGW_STR("Invalid parameter: name is NULL.");
-               goto error;
+       if (event_class->name.str) {
+               g_string_free(event_class->name.str, TRUE);
        }
 
-       event_class = g_new0(struct bt_event_class, 1);
-       if (!event_class) {
-               BT_LOGE_STR("Failed to allocate one event class.");
-               goto error;
+       if (event_class->emf_uri.str) {
+               g_string_free(event_class->emf_uri.str, TRUE);
        }
 
-       bt_object_init(event_class, bt_event_class_destroy);
-       event_class->fields = bt_field_type_structure_create();
-       if (!event_class->fields) {
-               BT_LOGE_STR("Cannot create event class's initial payload field type object.");
-               goto error;
-       }
-
-       event_class->id = -1;
-       event_class->name = g_string_new(name);
-       if (!event_class->name) {
-               BT_LOGE_STR("Failed to allocate a GString.");
-               goto error;
-       }
-
-       event_class->emf_uri = g_string_new(NULL);
-       if (!event_class->emf_uri) {
-               BT_LOGE_STR("Failed to allocate a GString.");
-               goto error;
-       }
-
-       event_class->log_level = BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED;
-       BT_PUT(obj);
-       BT_LOGD("Created event class object: addr=%p, name=\"%s\"",
-               event_class, bt_event_class_get_name(event_class));
-       return event_class;
-
-error:
-        BT_PUT(event_class);
-       BT_PUT(obj);
-       return event_class;
+       BT_LOGD_STR("Putting context field type.");
+       bt_put(event_class->specific_context_ft);
+       BT_LOGD_STR("Putting payload field type.");
+       bt_put(event_class->payload_ft);
+       bt_object_pool_finalize(&event_class->event_pool);
+       g_free(obj);
 }
 
-const char *bt_event_class_get_name(struct bt_event_class *event_class)
+static
+void free_event(struct bt_event *event,
+               struct bt_event_class *event_class)
 {
-       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
-       BT_ASSERT(event_class->name);
-       return event_class->name->str;
+       bt_event_destroy(event);
 }
 
-int64_t bt_event_class_get_id(struct bt_event_class *event_class)
-{
-       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
-       return event_class->id;
-}
-
-int bt_event_class_set_id(struct bt_event_class *event_class,
-               uint64_t id_param)
+BT_ASSERT_PRE_FUNC
+static
+bool event_class_id_is_unique(struct bt_stream_class *stream_class, uint64_t id)
 {
-       int ret = 0;
-       int64_t id = (int64_t) id_param;
-
-       if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = -1;
-               goto end;
-       }
+       uint64_t i;
+       bool is_unique = true;
 
-       if (event_class->frozen) {
-               BT_LOGW("Invalid parameter: event class is frozen: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               ret = -1;
-               goto end;
-       }
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_event_class *ec =
+                       stream_class->event_classes->pdata[i];
 
-       if (id < 0) {
-               BT_LOGW("Invalid parameter: invalid event class's ID: "
-                       "addr=%p, name=\"%s\", id=%" PRIu64,
-                       event_class, bt_event_class_get_name(event_class),
-                       id_param);
-               ret = -1;
-               goto end;
+               if (ec->id == id) {
+                       is_unique = false;
+                       goto end;
+               }
        }
 
-       event_class->id = id;
-       BT_LOGV("Set event class's ID: "
-               "addr=%p, name=\"%s\", id=%" PRId64,
-               event_class, bt_event_class_get_name(event_class), id);
-
 end:
-       return ret;
-}
-
-enum bt_event_class_log_level bt_event_class_get_log_level(
-               struct bt_event_class *event_class)
-{
-       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
-       return event_class->log_level;
+       return is_unique;
 }
 
-int bt_event_class_set_log_level(struct bt_event_class *event_class,
-               enum bt_event_class_log_level log_level)
+static
+struct bt_event_class *create_event_class_with_id(
+               struct bt_stream_class *stream_class, uint64_t id)
 {
-       int ret = 0;
+       int ret;
+       struct bt_event_class *event_class;
 
+       BT_ASSERT(stream_class);
+       BT_ASSERT_PRE(event_class_id_is_unique(stream_class, id),
+               "Duplicate event class ID: %![sc-]+S, id=%" PRIu64,
+               stream_class, id);
+       BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64,
+               stream_class, id);
+       event_class = g_new0(struct bt_event_class, 1);
        if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = -1;
-               goto end;
+               BT_LOGE_STR("Failed to allocate one event class.");
+               goto error;
        }
 
-       if (event_class->frozen) {
-               BT_LOGW("Invalid parameter: event class is frozen: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
+       bt_object_init_shared_with_parent(&event_class->base,
+               destroy_event_class);
+       event_class->id = id;
+       bt_property_uint_init(&event_class->log_level,
+                       BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
+       event_class->name.str = g_string_new(NULL);
+       if (!event_class->name.str) {
+               BT_LOGE_STR("Failed to allocate a GString.");
                ret = -1;
                goto end;
        }
 
-       switch (log_level) {
-       case BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED:
-       case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
-       case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
-       case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
-       case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
-       case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
-       case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
-       case BT_EVENT_CLASS_LOG_LEVEL_INFO:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
-       case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
-               break;
-       default:
-               BT_LOGW("Invalid parameter: unknown event class log level: "
-                       "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%d",
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class), log_level);
+       event_class->emf_uri.str = g_string_new(NULL);
+       if (!event_class->emf_uri.str) {
+               BT_LOGE_STR("Failed to allocate a GString.");
                ret = -1;
                goto end;
        }
 
-       event_class->log_level = log_level;
-       BT_LOGV("Set event class's log level: "
-               "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%s",
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class),
-               bt_event_class_log_level_string(log_level));
+       ret = bt_object_pool_initialize(&event_class->event_pool,
+               (bt_object_pool_new_object_func) bt_event_new,
+               (bt_object_pool_destroy_object_func) free_event,
+               event_class);
+       if (ret) {
+               BT_LOGE("Failed to initialize event pool: ret=%d",
+                       ret);
+               goto error;
+       }
+
+       bt_object_set_parent(&event_class->base, &stream_class->base);
+       g_ptr_array_add(stream_class->event_classes, event_class);
+       bt_stream_class_freeze(stream_class);
+       BT_LIB_LOGD("Created event class object: %!+E", event_class);
+       goto end;
+
+error:
+       BT_PUT(event_class);
 
 end:
-       return ret;
+       return event_class;
 }
 
-const char *bt_event_class_get_emf_uri(
-               struct bt_event_class *event_class)
+struct bt_event_class *bt_event_class_create(
+               struct bt_stream_class *stream_class)
 {
-       const char *emf_uri = NULL;
-
-       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
-
-       if (event_class->emf_uri->len > 0) {
-               emf_uri = event_class->emf_uri->str;
-       }
-
-       return emf_uri;
+       BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
+       BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id,
+               "Stream class does not automatically assigns event class IDs: "
+               "%![sc-]+S", stream_class);
+       return create_event_class_with_id(stream_class,
+               (uint64_t) stream_class->event_classes->len);
 }
 
-int bt_event_class_set_emf_uri(struct bt_event_class *event_class,
-               const char *emf_uri)
+struct bt_event_class *bt_event_class_create_with_id(
+               struct bt_stream_class *stream_class, uint64_t id)
 {
-       int ret = 0;
-
-       if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = -1;
-               goto end;
-       }
-
-       if (emf_uri && strlen(emf_uri) == 0) {
-               BT_LOGW_STR("Invalid parameter: EMF URI is empty.");
-               ret = -1;
-               goto end;
-       }
-
-       if (event_class->frozen) {
-               BT_LOGW("Invalid parameter: event class is frozen: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               ret = -1;
-               goto end;
-       }
-
-       if (emf_uri) {
-               g_string_assign(event_class->emf_uri, emf_uri);
-               BT_LOGV("Set event class's EMF URI: "
-                       "addr=%p, name=\"%s\", id=%" PRId64 ", emf-uri=\"%s\"",
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class), emf_uri);
-       } else {
-               g_string_assign(event_class->emf_uri, "");
-               BT_LOGV("Reset event class's EMF URI: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-       }
-
-end:
-       return ret;
+       BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id,
+               "Stream class automatically assigns event class IDs: "
+               "%![sc-]+S", stream_class);
+       return create_event_class_with_id(stream_class, id);
 }
 
-struct bt_stream_class *bt_event_class_get_stream_class(
-               struct bt_event_class *event_class)
+const char *bt_event_class_get_name(struct bt_event_class *event_class)
 {
        BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
-       return bt_get(bt_event_class_borrow_stream_class(event_class));
+       return event_class->name.value;
 }
 
-struct bt_field_type *bt_event_class_get_payload_type(
-               struct bt_event_class *event_class)
+int bt_event_class_set_name(struct bt_event_class *event_class,
+               const char *name)
 {
        BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
-       return bt_get(event_class->fields);
+       BT_ASSERT_PRE_NON_NULL(name, "Name");
+       BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
+       g_string_assign(event_class->name.str, name);
+       event_class->name.value = event_class->name.str->str;
+       BT_LIB_LOGV("Set event class's name: %!+E", event_class);
+       return 0;
 }
 
-int bt_event_class_set_payload_type(struct bt_event_class *event_class,
-               struct bt_field_type *payload)
+uint64_t bt_event_class_get_id(struct bt_event_class *event_class)
 {
-       int ret = 0;
-
-       if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = -1;
-               goto end;
-       }
-
-       if (payload && bt_field_type_get_type_id(payload) !=
-                       BT_FIELD_TYPE_ID_STRUCT) {
-               BT_LOGW("Invalid parameter: event class's payload field type must be a structure: "
-                       "addr=%p, name=\"%s\", id=%" PRId64 ", "
-                       "payload-ft-addr=%p, payload-ft-id=%s",
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class), payload,
-                       bt_field_type_id_string(
-                               bt_field_type_get_type_id(payload)));
-               ret = -1;
-               goto end;
-       }
-
-       bt_put(event_class->fields);
-       event_class->fields = bt_get(payload);
-       BT_LOGV("Set event class's payload field type: "
-               "event-class-addr=%p, event-class-name=\"%s\", "
-               "event-class-id=%" PRId64 ", payload-ft-addr=%p",
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class), payload);
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       return event_class->id;
 }
 
-int bt_event_class_add_field(struct bt_event_class *event_class,
-               struct bt_field_type *type,
-               const char *name)
+enum bt_property_availability bt_event_class_get_log_level(
+               struct bt_event_class *event_class,
+               enum bt_event_class_log_level *log_level)
 {
-       int ret = 0;
-
-       if (!event_class || !type) {
-               BT_LOGW("Invalid parameter: event class or field type is NULL: "
-                       "event-class-addr=%p, field-type-addr=%p",
-                       event_class, type);
-               ret = -1;
-               goto end;
-       }
-
-       if (!bt_identifier_is_valid(name)) {
-               BT_LOGW("Invalid parameter: event class's payload field type's field name is not a valid CTF identifier: "
-                       "addr=%p, name=\"%s\", id=%" PRId64 ", field-name=\"%s\"",
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class),
-                       name);
-               ret = -1;
-               goto end;
-       }
-
-       if (event_class->frozen) {
-               BT_LOGW("Invalid parameter: event class is frozen: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               ret = -1;
-               goto end;
-       }
-
-       if (!event_class->fields) {
-               BT_LOGW("Event class has no payload field type: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               ret = -1;
-               goto end;
-       }
-
-       BT_ASSERT(bt_field_type_get_type_id(event_class->fields) ==
-               BT_FIELD_TYPE_ID_STRUCT);
-       ret = bt_field_type_structure_add_field(event_class->fields,
-               type, name);
-       BT_LOGV("Added field to event class's payload field type: "
-               "event-class-addr=%p, event-class-name=\"%s\", "
-               "event-class-id=%" PRId64 ", field-name=\"%s\", ft-addr=%p",
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class), name, type);
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       BT_ASSERT_PRE_NON_NULL(log_level, "Log level (output)");
+       *log_level = (enum bt_event_class_log_level)
+               event_class->log_level.value;
+       return event_class->log_level.base.avail;
 }
 
-int64_t bt_event_class_get_payload_type_field_count(
-               struct bt_event_class *event_class)
+int bt_event_class_set_log_level(struct bt_event_class *event_class,
+               enum bt_event_class_log_level log_level)
 {
-       int64_t ret;
-
-       if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = (int64_t) -1;
-               goto end;
-       }
-
-       if (!event_class->fields) {
-               BT_LOGV("Event class has no payload field type: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               ret = (int64_t) -1;
-               goto end;
-       }
-
-       BT_ASSERT(bt_field_type_get_type_id(event_class->fields) ==
-               BT_FIELD_TYPE_ID_STRUCT);
-       ret = bt_field_type_structure_get_field_count(event_class->fields);
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
+       bt_property_uint_set(&event_class->log_level,
+               (uint64_t) log_level);
+       BT_LIB_LOGV("Set event class's log level: %!+E", event_class);
+       return 0;
 }
 
-int bt_event_class_get_payload_type_field_by_index(
-               struct bt_event_class *event_class,
-               const char **field_name, struct bt_field_type **field_type,
-               uint64_t index)
+const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class)
 {
-       int ret;
-
-       if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = -1;
-               goto end;
-       }
-
-       if (!event_class->fields) {
-               BT_LOGV("Event class has no payload field type: "
-                       "addr=%p, name=\"%s\", id=%" PRId64 ", index=%" PRIu64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class), index);
-               ret = -1;
-               goto end;
-       }
-
-       BT_ASSERT(bt_field_type_get_type_id(event_class->fields) ==
-               BT_FIELD_TYPE_ID_STRUCT);
-       ret = bt_field_type_structure_get_field_by_index(event_class->fields,
-               field_name, field_type, index);
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       return event_class->emf_uri.value;
 }
 
-struct bt_field_type *
-bt_event_class_get_payload_type_field_type_by_name(
-               struct bt_event_class *event_class, const char *name)
+int bt_event_class_set_emf_uri(struct bt_event_class *event_class,
+               const char *emf_uri)
 {
-       GQuark name_quark;
-       struct bt_field_type *field_type = NULL;
-
-       if (!event_class || !name) {
-               BT_LOGW("Invalid parameter: event class or name is NULL: "
-                       "event-class-addr=%p, name-addr=%p",
-                       event_class, name);
-               goto end;
-       }
-
-       if (!event_class->fields) {
-               BT_LOGV("Event class has no payload field type: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               goto end;
-       }
-
-       BT_ASSERT(bt_field_type_get_type_id(event_class->fields) ==
-               BT_FIELD_TYPE_ID_STRUCT);
-       name_quark = g_quark_try_string(name);
-       if (!name_quark) {
-               BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
-               goto end;
-       }
-
-       /*
-        * No need to increment field_type's reference count since getting it
-        * from the structure already does.
-        */
-       field_type = bt_field_type_structure_get_field_type_by_name(
-               event_class->fields, name);
-end:
-       return field_type;
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI");
+       BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
+       g_string_assign(event_class->emf_uri.str, emf_uri);
+       event_class->emf_uri.value = event_class->emf_uri.str->str;
+       BT_LIB_LOGV("Set event class's EMF URI: %!+E", event_class);
+       return 0;
 }
 
-struct bt_field_type *bt_event_class_get_context_type(
+struct bt_stream_class *bt_event_class_borrow_stream_class(
                struct bt_event_class *event_class)
 {
-       struct bt_field_type *context_type = NULL;
-
        BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       return bt_event_class_borrow_stream_class_inline(event_class);
+}
 
-       if (!event_class->context) {
-               BT_LOGV("Event class has no context field type: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               goto end;
-       }
-
-       context_type = bt_get(event_class->context);
-
-end:
-       return context_type;
+struct bt_field_type *bt_event_class_borrow_specific_context_field_type(
+               struct bt_event_class *event_class)
+{
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       return event_class->specific_context_ft;
 }
 
-int bt_event_class_set_context_type(
+int bt_event_class_set_specific_context_field_type(
                struct bt_event_class *event_class,
-               struct bt_field_type *context)
+               struct bt_field_type *field_type)
 {
-       int ret = 0;
-
-       if (!event_class) {
-               BT_LOGW_STR("Invalid parameter: event class is NULL.");
-               ret = -1;
-               goto end;
-       }
+       int ret;
+       struct bt_stream_class *stream_class;
+       struct bt_trace *trace;
+       struct bt_resolve_field_path_context resolve_ctx = {
+               .packet_header = NULL,
+               .packet_context = NULL,
+               .event_header = NULL,
+               .event_common_context = NULL,
+               .event_specific_context = field_type,
+               .event_payload = NULL,
+       };
 
-       if (event_class->frozen) {
-               BT_LOGW("Invalid parameter: event class is frozen: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class));
-               ret = -1;
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
+       BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
+       BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
+               BT_FIELD_TYPE_ID_STRUCTURE,
+               "Specific context field type is not a structure field type: "
+               "%!+F", field_type);
+       stream_class = bt_event_class_borrow_stream_class_inline(
+               event_class);
+       trace = bt_stream_class_borrow_trace_inline(stream_class);
+       resolve_ctx.packet_header = trace->packet_header_ft;
+       resolve_ctx.packet_context = stream_class->packet_context_ft;
+       resolve_ctx.event_header = stream_class->event_header_ft;
+       resolve_ctx.event_common_context =
+               stream_class->event_common_context_ft;
+
+       ret = bt_resolve_field_paths(field_type, &resolve_ctx);
+       if (ret) {
                goto end;
        }
 
-       if (context && bt_field_type_get_type_id(context) !=
-                       BT_FIELD_TYPE_ID_STRUCT) {
-               BT_LOGW("Invalid parameter: event class's context field type must be a structure: "
-                       "addr=%p, name=\"%s\", id=%" PRId64 ", "
-                       "context-ft-id=%s",
-                       event_class, bt_event_class_get_name(event_class),
-                       bt_event_class_get_id(event_class),
-                       bt_field_type_id_string(
-                               bt_field_type_get_type_id(context)));
-               ret = -1;
-               goto end;
-       }
+       bt_field_type_make_part_of_trace(field_type);
+       bt_put(event_class->specific_context_ft);
+       event_class->specific_context_ft = bt_get(field_type);
+       bt_field_type_freeze(field_type);
+       BT_LIB_LOGV("Set event class's specific context field type: %!+E",
+               event_class);
 
-       bt_put(event_class->context);
-       event_class->context = bt_get(context);
-       BT_LOGV("Set event class's context field type: "
-               "event-class-addr=%p, event-class-name=\"%s\", "
-               "event-class-id=%" PRId64 ", context-ft-addr=%p",
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class), context);
 end:
        return ret;
-
 }
 
-/* Pre-2.0 CTF writer backward compatibility */
-void bt_ctf_event_class_get(struct bt_event_class *event_class)
-{
-       bt_get(event_class);
-}
-
-/* Pre-2.0 CTF writer backward compatibility */
-void bt_ctf_event_class_put(struct bt_event_class *event_class)
-{
-       bt_put(event_class);
-}
-
-static
-void bt_event_class_destroy(struct bt_object *obj)
-{
-       struct bt_event_class *event_class;
-
-       event_class = container_of(obj, struct bt_event_class, base);
-       BT_LOGD("Destroying event class: addr=%p, name=\"%s\", id=%" PRId64,
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class));
-       g_string_free(event_class->name, TRUE);
-       g_string_free(event_class->emf_uri, TRUE);
-       BT_LOGD_STR("Putting context field type.");
-       bt_put(event_class->context);
-       BT_LOGD_STR("Putting payload field type.");
-       bt_put(event_class->fields);
-       g_free(event_class);
-}
-
-BT_HIDDEN
-void bt_event_class_freeze(struct bt_event_class *event_class)
+struct bt_field_type *bt_event_class_borrow_payload_field_type(
+               struct bt_event_class *event_class)
 {
-       BT_ASSERT(event_class);
-
-       if (event_class->frozen) {
-               return;
-       }
-
-       BT_LOGD("Freezing event class: addr=%p, name=\"%s\", id=%" PRId64,
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class));
-       event_class->frozen = 1;
-       BT_LOGD_STR("Freezing event class's context field type.");
-       bt_field_type_freeze(event_class->context);
-       BT_LOGD_STR("Freezing event class's payload field type.");
-       bt_field_type_freeze(event_class->fields);
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       return event_class->payload_ft;
 }
 
-BT_HIDDEN
-int bt_event_class_serialize(struct bt_event_class *event_class,
-               struct metadata_context *context)
+int bt_event_class_set_payload_field_type(struct bt_event_class *event_class,
+               struct bt_field_type *field_type)
 {
-       int ret = 0;
-       struct bt_value *attr_value = NULL;
-
-       BT_ASSERT(event_class);
-       BT_ASSERT(context);
-       BT_LOGD("Serializing event class's metadata: "
-               "event-class-addr=%p, event-class-name=\"%s\", "
-               "event-class-id=%" PRId64 ", metadata-context-addr=%p",
-               event_class, bt_event_class_get_name(event_class),
-               bt_event_class_get_id(event_class), context);
-       context->current_indentation_level = 1;
-       g_string_assign(context->field_name, "");
-       g_string_append(context->string, "event {\n");
-
-       /* Serialize attributes */
-       g_string_append_printf(context->string, "\tname = \"%s\";\n",
-               event_class->name->str);
-       BT_ASSERT(event_class->id >= 0);
-       g_string_append_printf(context->string, "\tid = %" PRId64 ";\n",
-               event_class->id);
-       g_string_append_printf(context->string, "\tstream_id = %" PRId64 ";\n",
-               bt_stream_class_get_id(
-                       bt_event_class_borrow_stream_class(event_class)));
-
-       if (event_class->log_level != BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED) {
-               g_string_append_printf(context->string, "\tloglevel = %d;\n",
-                       (int) event_class->log_level);
-       }
-
-       if (event_class->emf_uri->len > 0) {
-               g_string_append_printf(context->string, "\tmodel.emf.uri = \"%s\";\n",
-                       event_class->emf_uri->str);
-       }
+       int ret;
+       struct bt_stream_class *stream_class;
+       struct bt_trace *trace;
+       struct bt_resolve_field_path_context resolve_ctx = {
+               .packet_header = NULL,
+               .packet_context = NULL,
+               .event_header = NULL,
+               .event_common_context = NULL,
+               .event_specific_context = NULL,
+               .event_payload = field_type,
+       };
 
-       /* Serialize context field type */
-       if (event_class->context) {
-               g_string_append(context->string, "\tcontext := ");
-               BT_LOGD_STR("Serializing event class's context field type metadata.");
-               ret = bt_field_type_serialize(event_class->context,
-                       context);
-               if (ret) {
-                       BT_LOGW("Cannot serialize event class's context field type's metadata: "
-                               "ret=%d", ret);
-                       goto end;
-               }
-               g_string_append(context->string, ";\n");
+       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
+       BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
+       BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
+               BT_FIELD_TYPE_ID_STRUCTURE,
+               "Payload field type is not a structure field type: %!+F",
+               field_type);
+       stream_class = bt_event_class_borrow_stream_class_inline(
+               event_class);
+       trace = bt_stream_class_borrow_trace_inline(stream_class);
+       resolve_ctx.packet_header = trace->packet_header_ft;
+       resolve_ctx.packet_context = stream_class->packet_context_ft;
+       resolve_ctx.event_header = stream_class->event_header_ft;
+       resolve_ctx.event_common_context =
+               stream_class->event_common_context_ft;
+       resolve_ctx.event_specific_context = event_class->specific_context_ft;
+
+       ret = bt_resolve_field_paths(field_type, &resolve_ctx);
+       if (ret) {
+               goto end;
        }
 
-       /* Serialize payload field type */
-       if (event_class->fields) {
-               g_string_append(context->string, "\tfields := ");
-               BT_LOGD_STR("Serializing event class's payload field type metadata.");
-               ret = bt_field_type_serialize(event_class->fields, context);
-               if (ret) {
-                       BT_LOGW("Cannot serialize event class's payload field type's metadata: "
-                               "ret=%d", ret);
-                       goto end;
-               }
-               g_string_append(context->string, ";\n");
-       }
+       bt_field_type_make_part_of_trace(field_type);
+       bt_put(event_class->payload_ft);
+       event_class->payload_ft = bt_get(field_type);
+       bt_field_type_freeze(field_type);
+       BT_LIB_LOGV("Set event class's payload field type: %!+E", event_class);
 
-       g_string_append(context->string, "};\n\n");
 end:
-       context->current_indentation_level = 0;
-       BT_PUT(attr_value);
        return ret;
 }
 
 BT_HIDDEN
-int bt_event_class_validate_single_clock_class(
-               struct bt_event_class *event_class,
-               struct bt_clock_class **expected_clock_class)
+void _bt_event_class_freeze(struct bt_event_class *event_class)
 {
-       int ret = 0;
-
+       /* The field types are already frozen */
        BT_ASSERT(event_class);
-       BT_ASSERT(expected_clock_class);
-       ret = bt_validate_single_clock_class(event_class->context,
-               expected_clock_class);
-       if (ret) {
-               BT_LOGW("Event class's context field type "
-                       "is not recursively mapped to the "
-                       "expected clock class: "
-                       "event-class-addr=%p, "
-                       "event-class-name=\"%s\", "
-                       "event-class-id=%" PRId64 ", "
-                       "ft-addr=%p",
-                       event_class,
-                       bt_event_class_get_name(event_class),
-                       event_class->id,
-                       event_class->context);
-               goto end;
-       }
-
-       ret = bt_validate_single_clock_class(event_class->fields,
-               expected_clock_class);
-       if (ret) {
-               BT_LOGW("Event class's payload field type "
-                       "is not recursively mapped to the "
-                       "expected clock class: "
-                       "event-class-addr=%p, "
-                       "event-class-name=\"%s\", "
-                       "event-class-id=%" PRId64 ", "
-                       "ft-addr=%p",
-                       event_class,
-                       bt_event_class_get_name(event_class),
-                       event_class->id,
-                       event_class->fields);
-               goto end;
-       }
-
-end:
-       return ret;
+       BT_LIB_LOGD("Freezing event class: %!+E", event_class);
+       event_class->frozen = true;
 }
This page took 0.036477 seconds and 4 git commands to generate.