Revert "Add event header accessors and support for custom event headers"
[babeltrace.git] / formats / ctf / ir / stream-class.c
index 6cba98f9e483567a70b6f0379ae0d19b7fbaa793..058f4b29ecd1765f6a7d0521d58c02eb559a73c5 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * stream.c
+ * stream-class.c
  *
- * Babeltrace CTF Writer
+ * Babeltrace CTF IR - Stream Class
  *
  * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
@@ -49,6 +49,7 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class,
 
 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
 {
+       int ret;
        struct bt_ctf_stream_class *stream_class = NULL;
 
        if (!name || !strlen(name)) {
@@ -67,6 +68,11 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
                goto error_destroy;
        }
 
+       ret = init_packet_context(stream_class, BT_CTF_BYTE_ORDER_NATIVE);
+       if (ret) {
+               goto error_destroy;
+       }
+
        bt_ctf_ref_init(&stream_class->ref_count);
        return stream_class;
 
@@ -77,6 +83,35 @@ error:
        return stream_class;
 }
 
+const char *bt_ctf_stream_class_get_name(
+               struct bt_ctf_stream_class *stream_class)
+{
+       const char *name = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       name = stream_class->name->str;
+end:
+       return name;
+}
+
+struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_clock *clock = NULL;
+
+       if (!stream_class || !stream_class->clock) {
+               goto end;
+       }
+
+       clock = stream_class->clock;
+       bt_ctf_clock_get(clock);
+end:
+       return clock;
+}
+
 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_clock *clock)
 {
@@ -97,11 +132,42 @@ end:
        return ret;
 }
 
+int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
+{
+       int64_t ret;
+
+       if (!stream_class || !stream_class->id_set) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = (int64_t) stream_class->id;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
+               uint32_t id)
+{
+       int ret = 0;
+
+       if (!stream_class || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       stream_class->id = id;
+       stream_class->id_set = 1;
+end:
+       return ret;
+}
+
 int bt_ctf_stream_class_add_event_class(
                struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_event_class *event_class)
 {
        int ret = 0;
+       int64_t event_id;
 
        if (!stream_class || !event_class) {
                ret = -1;
@@ -116,10 +182,18 @@ int bt_ctf_stream_class_add_event_class(
                goto end;
        }
 
-       if (bt_ctf_event_class_set_id(event_class,
-               stream_class->next_event_id++)) {
-               /* The event is already associated to a stream class */
-               ret = -1;
+       /* Only set an event id if none was explicitly set before */
+       event_id = bt_ctf_event_class_get_id(event_class);
+       if (event_id < 0) {
+               if (bt_ctf_event_class_set_id(event_class,
+                       stream_class->next_event_id++)) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
+       if (ret) {
                goto end;
        }
 
@@ -129,6 +203,150 @@ end:
        return ret;
 }
 
+int bt_ctf_stream_class_get_event_class_count(
+               struct bt_ctf_stream_class *stream_class)
+{
+       int ret;
+
+       if (!stream_class) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = (int) stream_class->event_classes->len;
+end:
+       return ret;
+}
+
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
+               struct bt_ctf_stream_class *stream_class, int index)
+{
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (!stream_class || index < 0 ||
+               index >= stream_class->event_classes->len) {
+               goto end;
+       }
+
+       event_class = g_ptr_array_index(stream_class->event_classes, index);
+       bt_ctf_event_class_get(event_class);
+end:
+       return event_class;
+}
+
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
+               struct bt_ctf_stream_class *stream_class, const char *name)
+{
+       size_t i;
+       GQuark name_quark;
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (!stream_class || !name) {
+               goto end;
+       }
+
+       name_quark = g_quark_try_string(name);
+       if (!name_quark) {
+               goto end;
+       }
+
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_ctf_event_class *current_event_class =
+                       g_ptr_array_index(stream_class->event_classes, i);
+
+               if (name_quark == current_event_class->name) {
+                       event_class = current_event_class;
+                       bt_ctf_event_class_get(event_class);
+                       goto end;
+               }
+       }
+end:
+       return event_class;
+}
+
+struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       assert(stream_class->packet_context_type);
+       bt_ctf_field_type_get(stream_class->packet_context_type);
+       ret = stream_class->packet_context_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_packet_context_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *packet_context_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !packet_context_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       assert(stream_class->packet_context_type);
+       if (bt_ctf_field_type_get_type_id(packet_context_type) !=
+               CTF_TYPE_STRUCT) {
+               /* A packet context must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->packet_context_type);
+       bt_ctf_field_type_get(packet_context_type);
+       stream_class->packet_context_type = packet_context_type;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class || !stream_class->event_context_type) {
+               goto end;
+       }
+
+       assert(stream_class->event_context_type);
+       bt_ctf_field_type_get(stream_class->event_context_type);
+       ret = stream_class->event_context_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_event_context_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *event_context_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_context_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_field_type_get_type_id(event_context_type) !=
+               CTF_TYPE_STRUCT) {
+               /* A packet context must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->event_context_type);
+       bt_ctf_field_type_get(event_context_type);
+       stream_class->event_context_type = event_context_type;
+end:
+       return ret;
+}
+
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
 {
        if (!stream_class) {
@@ -155,40 +373,19 @@ void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
        }
 
        stream_class->frozen = 1;
+       bt_ctf_field_type_freeze(stream_class->packet_context_type);
+       bt_ctf_field_type_freeze(stream_class->event_context_type);
        bt_ctf_clock_freeze(stream_class->clock);
        g_ptr_array_foreach(stream_class->event_classes,
                (GFunc)bt_ctf_event_class_freeze, NULL);
 }
 
-BT_HIDDEN
-int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
-       uint32_t id)
-{
-       int ret = 0;
-
-       if (!stream_class ||
-               (stream_class->id_set && (id != stream_class->id))) {
-               ret = -1;
-               goto end;
-       }
-
-       stream_class->id = id;
-       stream_class->id_set = 1;
-end:
-       return ret;
-}
-
 BT_HIDDEN
 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
        enum bt_ctf_byte_order byte_order)
 {
        int ret = 0;
 
-       ret = init_packet_context(stream_class, byte_order);
-       if (ret) {
-               goto end;
-       }
-
        ret = init_event_header(stream_class, byte_order);
        if (ret) {
                goto end;
@@ -201,7 +398,7 @@ BT_HIDDEN
 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
                struct metadata_context *context)
 {
-       int ret = 0;
+       int64_t ret = 0;
        size_t i;
 
        g_string_assign(context->field_name, "");
@@ -237,21 +434,10 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
        }
 
        g_string_append(context->string, ";\n};\n\n");
-
-       /* Assign this stream's ID to every event and serialize them */
-       g_ptr_array_foreach(stream_class->event_classes,
-               (GFunc) bt_ctf_event_class_set_stream_id,
-               GUINT_TO_POINTER(stream_class->id));
        for (i = 0; i < stream_class->event_classes->len; i++) {
                struct bt_ctf_event_class *event_class =
                        stream_class->event_classes->pdata[i];
 
-               ret = bt_ctf_event_class_set_stream_id(event_class,
-                       stream_class->id);
-               if (ret) {
-                       goto end;
-               }
-
                ret = bt_ctf_event_class_serialize(event_class, context);
                if (ret) {
                        goto end;
@@ -275,6 +461,17 @@ void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
        bt_ctf_clock_put(stream_class->clock);
 
        if (stream_class->event_classes) {
+               size_t i;
+
+               /* Unregister this stream class from the event classes */
+               for (i = 0; i < stream_class->event_classes->len; i++) {
+                       struct bt_ctf_event_class *event_class =
+                               g_ptr_array_index(stream_class->event_classes,
+                               i);
+
+                       bt_ctf_event_class_set_stream_class(event_class, NULL);
+               }
+
                g_ptr_array_free(stream_class->event_classes, TRUE);
        }
 
@@ -285,9 +482,9 @@ void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
        bt_ctf_field_type_put(stream_class->event_header_type);
        bt_ctf_field_put(stream_class->event_header);
        bt_ctf_field_type_put(stream_class->packet_context_type);
-       bt_ctf_field_put(stream_class->packet_context);
-       bt_ctf_field_type_put(stream_class->event_context_type);
-       bt_ctf_field_put(stream_class->event_context);
+       if (stream_class->event_context_type) {
+               bt_ctf_field_type_put(stream_class->event_context_type);
+       }
        g_free(stream_class);
 }
 
@@ -401,10 +598,6 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class,
        }
 
        stream_class->packet_context_type = packet_context_type;
-       stream_class->packet_context = bt_ctf_field_create(packet_context_type);
-       if (!stream_class->packet_context) {
-               ret = -1;
-       }
 end:
        if (ret) {
                bt_ctf_field_type_put(packet_context_type);
This page took 0.027606 seconds and 4 git commands to generate.