lib: strictly type function return status enumerations
[babeltrace.git] / src / plugins / utils / dummy / dummy.c
CommitLineData
e0dfa761
PP
1/*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
3fadfbc0 23#include <babeltrace2/babeltrace.h>
91d81473 24#include "common/macros.h"
578e048b 25#include "common/assert.h"
c2b71c92
JG
26#include "dummy.h"
27
5badd463
PP
28static
29const char * const in_port_name = "in";
30
c2b71c92
JG
31void destroy_private_dummy_data(struct dummy *dummy)
32{
d6e69534 33 bt_self_component_port_input_message_iterator_put_ref(dummy->msg_iter);
c2b71c92 34 g_free(dummy);
d94d92ac 35
c2b71c92
JG
36}
37
d94d92ac 38BT_HIDDEN
b19ff26f 39void dummy_finalize(bt_self_component_sink *comp)
c2b71c92
JG
40{
41 struct dummy *dummy;
42
d94d92ac
PP
43 BT_ASSERT(comp);
44 dummy = bt_self_component_get_data(
707b7d35 45 bt_self_component_sink_as_self_component(comp));
f6ccaed9 46 BT_ASSERT(dummy);
c2b71c92
JG
47 destroy_private_dummy_data(dummy);
48}
49
d94d92ac 50BT_HIDDEN
d24d5663 51bt_component_class_init_method_status dummy_init(
b19ff26f
PP
52 bt_self_component_sink *component,
53 const bt_value *params,
c88dd1cb 54 __attribute__((unused)) void *init_method_data)
c2b71c92 55{
d24d5663
PP
56 bt_component_class_init_method_status status =
57 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
58 bt_self_component_add_port_status add_port_status;
c2b71c92
JG
59 struct dummy *dummy = g_new0(struct dummy, 1);
60
61 if (!dummy) {
d24d5663 62 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
c2b71c92
JG
63 goto end;
64 }
65
d24d5663 66 add_port_status = bt_self_component_sink_add_input_port(component,
147337a3 67 "in", NULL, NULL);
d24d5663
PP
68 switch (add_port_status) {
69 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
70 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
c2b71c92 71 goto error;
d24d5663
PP
72 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
73 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
74 goto error;
75 default:
76 break;
c2b71c92 77 }
d19348b6 78
d94d92ac 79 bt_self_component_set_data(
707b7d35 80 bt_self_component_sink_as_self_component(component), dummy);
d19348b6
PP
81 goto end;
82
c2b71c92
JG
83error:
84 destroy_private_dummy_data(dummy);
d19348b6
PP
85
86end:
d24d5663 87 return status;
c2b71c92
JG
88}
89
d94d92ac 90BT_HIDDEN
d24d5663 91bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured(
5badd463 92 bt_self_component_sink *comp)
c2b71c92 93{
d24d5663
PP
94 bt_component_class_sink_graph_is_configured_method_status status =
95 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
c2b71c92 96 struct dummy *dummy;
d6e69534 97 bt_self_component_port_input_message_iterator *iterator;
c2b71c92 98
d94d92ac 99 dummy = bt_self_component_get_data(
707b7d35 100 bt_self_component_sink_as_self_component(comp));
f6ccaed9 101 BT_ASSERT(dummy);
d6e69534 102 iterator = bt_self_component_port_input_message_iterator_create(
5badd463
PP
103 bt_self_component_sink_borrow_input_port_by_name(comp,
104 in_port_name));
d94d92ac 105 if (!iterator) {
d24d5663 106 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR;
c2b71c92
JG
107 goto end;
108 }
109
d6e69534
PP
110 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
111 dummy->msg_iter, iterator);
0d8b4d8e 112
c2b71c92 113end:
bf55043c 114 return status;
c2b71c92 115}
e0dfa761 116
d94d92ac 117BT_HIDDEN
d24d5663 118bt_component_class_sink_consume_method_status dummy_consume(
b19ff26f 119 bt_self_component_sink *component)
e0dfa761 120{
d24d5663
PP
121 bt_component_class_sink_consume_method_status status =
122 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
d6e69534 123 bt_message_array_const msgs;
d4393e08 124 uint64_t count;
c2b71c92 125 struct dummy *dummy;
d24d5663 126 bt_message_iterator_next_status next_status;
d4393e08 127 uint64_t i;
e0dfa761 128
d94d92ac 129 dummy = bt_self_component_get_data(
707b7d35 130 bt_self_component_sink_as_self_component(component));
f6ccaed9 131 BT_ASSERT(dummy);
e0dfa761 132
91d81473 133 if (G_UNLIKELY(!dummy->msg_iter)) {
d24d5663 134 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d19348b6 135 goto end;
e0dfa761
PP
136 }
137
d6e69534 138 /* Consume one message */
d24d5663 139 next_status = bt_self_component_port_input_message_iterator_next(
d6e69534 140 dummy->msg_iter, &msgs, &count);
d24d5663
PP
141 switch (next_status) {
142 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
143 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
d4393e08
PP
144
145 for (i = 0; i < count; i++) {
d6e69534 146 bt_message_put_ref(msgs[i]);
d4393e08
PP
147 }
148
149 break;
d24d5663
PP
150 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
151 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
d19348b6 152 goto end;
d24d5663
PP
153 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
154 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d19348b6 155 goto end;
d24d5663
PP
156 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
157 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
d94d92ac 158 goto end;
d24d5663
PP
159 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
160 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
d4393e08 161 goto end;
d19348b6
PP
162 default:
163 break;
e0dfa761 164 }
d19348b6 165
e0dfa761 166end:
d24d5663 167 return status;
e0dfa761 168}
This page took 0.050071 seconds and 4 git commands to generate.