objects

objects in c
git clone https://noulin.net/git/objects.git
Log | Files | Refs | README | LICENSE

commit 6de4c43116f611d7d09773df0fc94715a7acc7c2
parent c13ad0f7746f002b5048fe8eea403c5616c73e4a
Author: Remy Noulin <loader2x@gmail.com>
Date:   Sat,  9 Dec 2017 14:35:56 +0100

add class inheritance concept

main.c | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)

Diffstat:
Mmain.c | 35++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/main.c b/main.c @@ -17,7 +17,7 @@ typedef struct { basePrintt print; } baseFunctionst; -struct base { +struct base { const char *type; baseFunctionst *f; }; @@ -26,6 +26,10 @@ struct base { // Class: class typedef struct class classt; + +// for object inheriting class, cast to classt to be able to use this class functions and generics +#define cClass(self) ( (classt*) self ) + static const char classtName[] = "class"; typedef void (*classPrintt)(classt *self, char *string); @@ -33,14 +37,17 @@ typedef void (*freeClasstt)(classt *self); typedef void (*terminateClasstt)(classt **self); typedef classt* (*duplicateClasstt)(classt **self); +/* use this define in child classes and add the new function after this class functions */ +#define CLASSFUNCTIONST \ + terminateClasstt terminate;\ + duplicateClasstt duplicate;\ + freeClasstt free + typedef struct { // base class classPrintt print; // base class end - - terminateClasstt terminate; - duplicateClasstt duplicate; - freeClasstt free; + CLASSFUNCTIONST; } classFunctionst; static classFunctionst *classF = NULL; @@ -58,6 +65,10 @@ struct class { // Initiate/Terminate Objects #define createclasst(obj) classt obj; initiateclasst(&obj) #define createAllocateclasst(obj) classt *obj; initiateAllocateclasst(&obj) + +// initialize class methods, call registerMethodsClass from classes inheriting this class +void registerMethodsClass(classFunctionst *f); + void initiateclasst(classt *self); void freeclasst(classt *self); void terminateclasst(classt **self); @@ -76,10 +87,7 @@ void initiateclasst(classt *self) { if (!classF) { classF = malloc(sizeof(classFunctionst)); - classF->free = freeclasst; - classF->terminate = terminateclasst; - classF->duplicate = duplicateclasst; - classF->print = print; + registerMethodsClass(classF); } self->f = classF; self->a = 0; @@ -89,6 +97,15 @@ void initiateclasst(classt *self) { self->type = classtName; } +void registerMethodsClass(classFunctionst *f) { + + f->free = freeclasst; + f->terminate = terminateclasst; + f->duplicate = duplicateclasst; + f->print = print; + +} + void freeclasst(classt *self) { if (self->s) free(self->s);