Improve inheriting system

This commit is contained in:
Dendy 2021-02-07 07:26:57 +01:00
parent 01e4d7c9ec
commit fcd3e90b8b
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
5 changed files with 43 additions and 11 deletions

View File

@ -6,8 +6,10 @@
CC="tcc" CC="tcc"
main: src/main.c src/node/node.c src/node2d/node2d.c SRC=src/main.c src/node/node.c src/node2d/node2d.c
tcc -o $@ -I src $^
main: $(SRC)
tcc -o $@ -I src $(SRC)
clean: clean:
rm -rf main rm -rf main

View File

@ -22,10 +22,16 @@ int main(int argc, char* *argv){
NOD_Node *debianbsd = NOD_CreateNode("Debian GNU/kFreeBSD"); NOD_Node *debianbsd = NOD_CreateNode("Debian GNU/kFreeBSD");
NOD_Node2D *point = NOD_CreateNode2D("Point2D"); NOD_Node2D *point = NOD_CreateNode2D("Point2D");
NOD_Node2D *point2 = NOD_CreateNode2D("Point2D2");
point->pos.x = 2; point->pos.x = 2;
printf("The X value of '%s' is %d!\n", point->node.name, 3); 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, linoox);
NOD_NodeAddChild(distros, bsd); NOD_NodeAddChild(distros, bsd);

View File

@ -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){ if(parent != NULL && child != NULL){
/** Prevent recursion */ /** Prevent recursion */
if(parent == child){ 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){ if(node != NULL && node->parent != NULL){
NOD_Node *parent = node->parent; 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){ if(node != NULL){
char* level = strdup(""); 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; int count = 0;
for(int i = 0; i < node->childc; i++){ for(int i = 0; i < node->childc; i++){
if(node->childv[i] != NULL) count++; if(node->childv[i] != NULL) count++;

View File

@ -16,15 +16,26 @@ typedef struct node{
} NOD_Node; } NOD_Node;
#define NOD(x) ((NOD_Node*)x)
NOD_Node *NOD_CreateNode(char *name); NOD_Node *NOD_CreateNode(char *name);
NOD_Node *NOD_CreateNodeEX(char *type, char *name); NOD_Node *NOD_CreateNodeEX(char *type, char *name);
void NOD_DestroyNode(NOD_Node *node); void NOD_DestroyNode(NOD_Node *node);
void NOD_DestroyNodeBranch(NOD_Node *node); void NOD_DestroyNodeBranch(NOD_Node *node);
void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child); /* Passing a derivative of node will throw
void NOD_NodeUnparent(NOD_Node *child); * a warning if not casted to a NOD_Node pointer,
* so we're doing doing that
*/
void NOD_PrintNodeTree(NOD_Node *node); #define NOD_NodeAddChild(x, y) _nod_add_child(NOD(x), NOD(y))
int NOD_NodeChildCount(NOD_Node *node); #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_ #endif // __NODE_H_

9
src/types.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __TYPES_H_
#define __TYPES_H_
typedef struct vector_2d_i{
int x;
int y;
} vector2Di;
#endif // __TYPES_H_