diff --git a/src/include/tc_readable.h b/src/include/tc_readable.h index 79f5952..4bbb081 100644 --- a/src/include/tc_readable.h +++ b/src/include/tc_readable.h @@ -48,6 +48,9 @@ int8_t tc_readable_node_add_child(tc_readable_node_t *parent, tc_readable_node_t // Convinience function for creating a new node and adding it to parent tc_readable_node_t *tc_readable_node_add_new_child(tc_readable_node_t *parent); +// Create a tc_readable_result from data, data type and validity. Avoids boilerplate in returning values from readable nodes. +tc_readable_result_t tc_readable_result_create(enum tc_data_types type, void *data, bool valid); + #ifdef __cplusplus } #endif diff --git a/src/lib/tc_readable.c b/src/lib/tc_readable.c index f9d6cf8..ea469c0 100644 --- a/src/lib/tc_readable.c +++ b/src/lib/tc_readable.c @@ -50,3 +50,28 @@ tc_readable_node_t *tc_readable_node_add_new_child(tc_readable_node_t *parent) { } return node; } + +tc_readable_result_t tc_readable_result_create(enum tc_data_types type, void *data, bool valid) { + tc_readable_result_t res; + res.valid = valid; + res.data.data_type = type; + + if (!data || !valid) { + return res; + } + + switch (type) { + case TC_TYPE_UINT: + res.data.uint_value = *(uint64_t*) data; + break; + case TC_TYPE_INT: + res.data.int_value = *(int64_t*) data; + case TC_TYPE_DOUBLE: + res.data.uint_value = *(double*) data; + break; + default: + res.valid = false; + break; + } + return res; +}