Cleanup: Use a switch case instead of conditionals
[babeltrace.git] / formats / ctf / ir / event.c
index caea354adcefb5a5c62e68a7f5ef31e951ac5bb1..ad66783854a2b0174986fc0af2646d9f31ee9771 100644 (file)
@@ -33,7 +33,9 @@
 #include <babeltrace/ctf-ir/event-types-internal.h>
 #include <babeltrace/ctf-ir/event-internal.h>
 #include <babeltrace/ctf-ir/stream-class.h>
-#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/ctf-ir/stream-class-internal.h>
+#include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
 
 static
@@ -45,7 +47,7 @@ struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
 {
        struct bt_ctf_event_class *event_class = NULL;
 
-       if (validate_identifier(name)) {
+       if (bt_ctf_validate_identifier(name)) {
                goto end;
        }
 
@@ -133,7 +135,7 @@ int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
 {
        int ret = 0;
 
-       if (!event_class || !type || validate_identifier(name) ||
+       if (!event_class || !type || bt_ctf_validate_identifier(name) ||
                event_class->frozen) {
                ret = -1;
                goto end;
@@ -153,10 +155,10 @@ end:
        return ret;
 }
 
-int64_t bt_ctf_event_class_get_field_count(
+int bt_ctf_event_class_get_field_count(
                struct bt_ctf_event_class *event_class)
 {
-       int64_t ret;
+       int ret;
 
        if (!event_class) {
                ret = -1;
@@ -170,11 +172,11 @@ end:
 
 int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class,
                const char **field_name, struct bt_ctf_field_type **field_type,
-               size_t index)
+               int index)
 {
        int ret;
 
-       if (!event_class) {
+       if (!event_class || index < 0) {
                ret = -1;
                goto end;
        }
@@ -210,6 +212,45 @@ end:
        return field_type;
 }
 
+struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
+               struct bt_ctf_event_class *event_class)
+{
+       struct bt_ctf_field_type *context_type = NULL;
+
+       if (!event_class || !event_class->context) {
+               goto end;
+       }
+
+       bt_ctf_field_type_get(event_class->context);
+       context_type = event_class->context;
+end:
+       return context_type;
+}
+
+int bt_ctf_event_class_set_context_type(
+               struct bt_ctf_event_class *event_class,
+               struct bt_ctf_field_type *context)
+{
+       int ret = 0;
+
+       if (!event_class || !context || event_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) {
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_get(context);
+       bt_ctf_field_type_put(event_class->context);
+       event_class->context = context;
+end:
+       return ret;
+
+}
+
 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
 {
        if (!event_class) {
@@ -245,7 +286,10 @@ struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
        bt_ctf_event_class_get(event_class);
        bt_ctf_event_class_freeze(event_class);
        event->event_class = event_class;
-       event->context_payload = bt_ctf_field_create(event_class->context);
+       if (event_class->context) {
+               event->context_payload = bt_ctf_field_create(
+                       event_class->context);
+       }
        event->fields_payload = bt_ctf_field_create(event_class->fields);
 end:
        return event;
@@ -304,7 +348,7 @@ int bt_ctf_event_set_payload(struct bt_ctf_event *event,
 {
        int ret = 0;
 
-       if (!event || !value || validate_identifier(name)) {
+       if (!event || !value || bt_ctf_validate_identifier(name)) {
                ret = -1;
                goto end;
        }
@@ -331,11 +375,11 @@ end:
 }
 
 struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
-               struct bt_ctf_event *event, size_t index)
+               struct bt_ctf_event *event, int index)
 {
        struct bt_ctf_field *field = NULL;
 
-       if (!event) {
+       if (!event || index < 0) {
                goto end;
        }
 
@@ -345,6 +389,48 @@ end:
        return field;
 }
 
+struct bt_ctf_field *bt_ctf_event_get_event_context(
+               struct bt_ctf_event *event)
+{
+       struct bt_ctf_field *context = NULL;
+
+       if (!event || !event->context_payload) {
+               goto end;
+       }
+
+       context = event->context_payload;
+       bt_ctf_field_get(context);
+end:
+       return context;
+}
+
+int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
+               struct bt_ctf_field *context)
+{
+       int ret = 0;
+       struct bt_ctf_field_type *field_type = NULL;
+
+       if (!event || !context) {
+               ret = -1;
+               goto end;
+       }
+
+       field_type = bt_ctf_field_get_type(context);
+       if (field_type != event->event_class->context) {
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_get(context);
+       bt_ctf_field_put(event->context_payload);
+       event->context_payload = context;
+end:
+       if (field_type) {
+               bt_ctf_field_type_put(field_type);
+       }
+       return ret;
+}
+
 void bt_ctf_event_get(struct bt_ctf_event *event)
 {
        if (!event) {
@@ -377,8 +463,12 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
         * bt_ctf_event_class_set_stream_class for explanation.
         */
        event_class = container_of(ref, struct bt_ctf_event_class, ref_count);
-       bt_ctf_field_type_put(event_class->context);
-       bt_ctf_field_type_put(event_class->fields);
+       if (event_class->context) {
+               bt_ctf_field_type_put(event_class->context);
+       }
+       if (event_class->fields) {
+               bt_ctf_field_type_put(event_class->fields);
+       }
        g_free(event_class);
 }
 
@@ -393,9 +483,15 @@ void bt_ctf_event_destroy(struct bt_ctf_ref *ref)
 
        event = container_of(ref, struct bt_ctf_event,
                ref_count);
-       bt_ctf_event_class_put(event->event_class);
-       bt_ctf_field_put(event->context_payload);
-       bt_ctf_field_put(event->fields_payload);
+       if (event->event_class) {
+               bt_ctf_event_class_put(event->event_class);
+       }
+       if (event->context_payload) {
+               bt_ctf_field_put(event->context_payload);
+       }
+       if (event->fields_payload) {
+               bt_ctf_field_put(event->fields_payload);
+       }
        g_free(event);
 }
 
@@ -485,6 +581,20 @@ end:
        return ret;
 }
 
+void bt_ctf_event_class_set_native_byte_order(
+               struct bt_ctf_event_class *event_class,
+               int byte_order)
+{
+       if (!event_class) {
+               return;
+       }
+
+       bt_ctf_field_type_set_native_byte_order(event_class->context,
+               byte_order);
+       bt_ctf_field_type_set_native_byte_order(event_class->fields,
+               byte_order);
+}
+
 BT_HIDDEN
 int bt_ctf_event_validate(struct bt_ctf_event *event)
 {
This page took 0.027171 seconds and 4 git commands to generate.