1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include <mos/lib/structures/list.h>
6#include <mos/moslib_global.h>
7#include <mos/types.h>
8
9typedef struct tree_node tree_node_t;
10
11typedef struct tree_node
12{
13 as_linked_list;
14 tree_node_t *parent;
15 list_head children;
16} tree_node_t;
17
18/**
19 * @brief Embed a tree node into a struct
20 */
21#define as_tree tree_node_t tree_node
22
23typedef const struct
24{
25 void (*get_node_name)(const tree_node_t *node, char **name, size_t *name_len);
26} tree_op_t;
27
28#define tree_entry(node, type) container_of((node), type, tree_node)
29#define tree_node(element) (&((element)->tree_node))
30#define tree_parent(node, type) (tree_entry(tree_node(node)->parent, type))
31
32#define tree_children_list(node) (&((node)->tree_node.children))
33#define tree_child_entry(node, type) container_of((node), type, tree_node.list_node)
34#define tree_child_node(node) (&((node)->tree_node.list_node))
35
36#define tree_foreach_child(t, v, h) \
37 for (__typeof(tree_child_entry(tree_child_node(h), t)) v = tree_child_entry(tree_children_list(h)->next, t); tree_child_node(v) != tree_children_list(h); \
38 v = tree_child_entry(tree_child_node(v)->next, t))
39
40MOSAPI void tree_node_init(tree_node_t *node);
41MOSAPI void tree_add_child(tree_node_t *parent, tree_node_t *child);
42