From fcd3e90b8b9e55efa2abdd1769037013afac7215 Mon Sep 17 00:00:00 2001 From: Dendy Date: Sun, 7 Feb 2021 07:26:57 +0100 Subject: [PATCH] Improve inheriting system --- Makefile | 6 ++++-- src/main.c | 8 +++++++- src/node/node.c | 12 ++++++++---- src/node/node.h | 19 +++++++++++++++---- src/types.h | 9 +++++++++ 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 src/types.h diff --git a/Makefile b/Makefile index 843bb3f..7c3026d 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,10 @@ CC="tcc" -main: src/main.c src/node/node.c src/node2d/node2d.c - tcc -o $@ -I src $^ +SRC=src/main.c src/node/node.c src/node2d/node2d.c + +main: $(SRC) + tcc -o $@ -I src $(SRC) clean: rm -rf main diff --git a/src/main.c b/src/main.c index 9024cd0..4c1b947 100644 --- a/src/main.c +++ b/src/main.c @@ -22,10 +22,16 @@ int main(int argc, char* *argv){ NOD_Node *debianbsd = NOD_CreateNode("Debian GNU/kFreeBSD"); NOD_Node2D *point = NOD_CreateNode2D("Point2D"); + NOD_Node2D *point2 = NOD_CreateNode2D("Point2D2"); + point->pos.x = 2; printf("The X value of '%s' is %d!\n", point->node.name, 3); - NOD_NodeAddChild(debian, point); + NOD_NodeAddChild(point, point2); + + printf("The parent of '%s' is '%s'.\n", NOD(point2)->name, NOD(NOD(point2)->parent)->name); + + NOD_PrintNodeTree(point); NOD_NodeAddChild(distros, linoox); NOD_NodeAddChild(distros, bsd); diff --git a/src/node/node.c b/src/node/node.c index 7b4aa5c..bfeeab5 100644 --- a/src/node/node.c +++ b/src/node/node.c @@ -41,7 +41,8 @@ void NOD_DestroyNodeBranch(NOD_Node *node){ } } -void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){ +/* You call this by calling NOD_NodeAddChild, see header */ +void _nod_add_child(NOD_Node *parent, NOD_Node *child){ if(parent != NULL && child != NULL){ /** Prevent recursion */ if(parent == child){ @@ -123,7 +124,8 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){ } } -void NOD_NodeUnparent(NOD_Node *node){ +/* You call this by calling NOD_NodeUnparent, see header */ +void _nod_unparent (NOD_Node *node){ if(node != NULL && node->parent != NULL){ NOD_Node *parent = node->parent; @@ -190,7 +192,8 @@ void nodetree_print_branch(NOD_Node *node, char* level){ } } -void NOD_PrintNodeTree(NOD_Node *node){ +/* You call this by calling NOD_PrintNodeTree, see header */ +void _nod_print_tree(NOD_Node *node){ if(node != NULL){ char* level = strdup(""); @@ -203,7 +206,8 @@ void NOD_PrintNodeTree(NOD_Node *node){ } } -int NOD_NodeChildCount(NOD_Node *node){ +/* You call this by calling NOD_NodeChildCount, see header */ +int _nod_child_count(NOD_Node *node){ int count = 0; for(int i = 0; i < node->childc; i++){ if(node->childv[i] != NULL) count++; diff --git a/src/node/node.h b/src/node/node.h index 2ae159b..6519abf 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -16,15 +16,26 @@ typedef struct node{ } NOD_Node; +#define NOD(x) ((NOD_Node*)x) + NOD_Node *NOD_CreateNode(char *name); NOD_Node *NOD_CreateNodeEX(char *type, char *name); void NOD_DestroyNode(NOD_Node *node); void NOD_DestroyNodeBranch(NOD_Node *node); -void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child); -void NOD_NodeUnparent(NOD_Node *child); +/* Passing a derivative of node will throw + * a warning if not casted to a NOD_Node pointer, + * so we're doing doing that + */ -void NOD_PrintNodeTree(NOD_Node *node); -int NOD_NodeChildCount(NOD_Node *node); +#define NOD_NodeAddChild(x, y) _nod_add_child(NOD(x), NOD(y)) +#define NOD_NodeUnparent(x) _nod_unparent(NOD(x)) +#define NOD_PrintNodeTree(x) _nod_print_tree(NOD(x)) +#define NOD_NodeChildCount(x) _nod_child_count(NOD(x)); + +void _nod_add_child(NOD_Node *parent, NOD_Node *child); +void _nod_unparent(NOD_Node *child); +void _nod_print_tree(NOD_Node *node); +int _nod_child_count(NOD_Node *node); #endif // __NODE_H_ diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..7f48b7e --- /dev/null +++ b/src/types.h @@ -0,0 +1,9 @@ +#ifndef __TYPES_H_ +#define __TYPES_H_ + +typedef struct vector_2d_i{ + int x; + int y; +} vector2Di; + +#endif // __TYPES_H_