lib: add tc_readable_result_create()

This commit is contained in:
jussi 2019-11-10 21:10:14 +02:00
parent d29249e0cf
commit f200fae8f4
2 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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;
}