1 /** 2 * @file lv_tree.h 3 * Tree. The tree nodes are dynamically allocated by the 'lv_mem' module. 4 */ 5 6 #ifndef LV_TREE_H 7 #define LV_TREE_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 /********************* 14 * INCLUDES 15 *********************/ 16 #include "lv_types.h" 17 18 /********************* 19 * DEFINES 20 *********************/ 21 22 #define LV_TREE_NODE(n) ((lv_tree_node_t*)(n)) 23 24 /********************** 25 * TYPEDEFS 26 **********************/ 27 28 struct _lv_tree_node_t; 29 30 /** 31 * Describe the common methods of every object. 32 * Similar to a C++ class. 33 */ 34 typedef struct _lv_tree_class_t { 35 const struct _lv_tree_class_t * base_class; 36 uint32_t instance_size; 37 void (*constructor_cb)(const struct _lv_tree_class_t * class_p, struct _lv_tree_node_t * node); 38 void (*destructor_cb)(const struct _lv_tree_class_t * class_p, struct _lv_tree_node_t * node); 39 } lv_tree_class_t; 40 41 /** Description of a tree node*/ 42 typedef struct _lv_tree_node_t { 43 struct _lv_tree_node_t * parent; 44 struct _lv_tree_node_t ** children; 45 uint32_t child_cnt; 46 uint32_t child_cap; 47 const struct _lv_tree_class_t * class_p; 48 } lv_tree_node_t; 49 50 enum { 51 LV_TREE_WALK_PRE_ORDER = 0, 52 LV_TREE_WALK_POST_ORDER, 53 }; 54 typedef uint8_t lv_tree_walk_mode_t; 55 56 typedef bool (*lv_tree_traverse_cb_t)(const lv_tree_node_t * node, void * user_data); 57 typedef bool (*lv_tree_before_cb_t)(const lv_tree_node_t * node, void * user_data); 58 typedef void (*lv_tree_after_cb_t)(const lv_tree_node_t * node, void * user_data); 59 60 /********************** 61 * GLOBAL PROTOTYPES 62 **********************/ 63 64 extern const lv_tree_class_t lv_tree_node_class; 65 66 /** 67 * @brief Create a tree node 68 * @param class_p pointer to a class of the node 69 * @param parent pointer to the parent node (or NULL if it's the root node) 70 * @return pointer to the new node 71 */ 72 lv_tree_node_t * lv_tree_node_create(const lv_tree_class_t * class_p, lv_tree_node_t * parent); 73 74 /** 75 * @brief Delete a tree node and all its children recursively 76 * @param node pointer to the node to delete 77 */ 78 void lv_tree_node_delete(lv_tree_node_t * node); 79 80 /** 81 * @brief Walk the tree recursively and call a callback function on each node 82 * @param node pointer to the root node of the tree 83 * @param mode LV_TREE_WALK_PRE_ORDER or LV_TREE_WALK_POST_ORDER 84 * @param cb callback function to call on each node 85 * @param bcb callback function to call before visiting a node 86 * @param acb callback function to call after visiting a node 87 * @param user_data user data to pass to the callback functions 88 * @return true: traversal is finished; false: traversal broken 89 */ 90 bool lv_tree_walk(const lv_tree_node_t * node, 91 lv_tree_walk_mode_t mode, 92 lv_tree_traverse_cb_t cb, 93 lv_tree_before_cb_t bcb, 94 lv_tree_after_cb_t acb, 95 void * user_data); 96 97 /********************** 98 * MACROS 99 **********************/ 100 101 #ifdef __cplusplus 102 } /*extern "C"*/ 103 #endif 104 105 #endif 106