objects

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

commit c13ad0f7746f002b5048fe8eea403c5616c73e4a
parent 6f001d48eefe4847e791690c89851e8652233b7e
Author: Remy Noulin (Spartatek) <remy.noulin@spartatek.se>
Date:   Sat,  1 Apr 2017 08:42:46 +0200

class functions are now in an independent struct

Size of classt: 32 bytes

The size of classt doesnt increase as functions are added to the class

And object are created faster since the function pointer are initiliazed
once for the class.

main.c | 61 +++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 41 insertions(+), 20 deletions(-)

Diffstat:
Mmain.c | 61+++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 41 insertions(+), 20 deletions(-)

diff --git a/main.c b/main.c @@ -3,9 +3,9 @@ #include <string.h> // Generics -#define freeO(obj) obj.free(&obj) -#define freeOPointer(obj) obj->free(obj) -#define deleteO(obj) obj->terminate(&obj) +#define freeO(obj) obj.f->free(&obj) +#define freeOPointer(obj) obj->f->free(obj) +#define deleteO(obj) obj->f->terminate(&obj) #define isOType(obj, className) ((baset *) obj)->type == className // Class: base @@ -13,11 +13,17 @@ typedef struct base baset; typedef void (*basePrintt)(baset *self, char *string); +typedef struct { + basePrintt print; +} baseFunctionst; + struct base { const char *type; - basePrintt print; + baseFunctionst *f; }; +// END base + // Class: class typedef struct class classt; static const char classtName[] = "class"; @@ -27,17 +33,26 @@ typedef void (*freeClasstt)(classt *self); typedef void (*terminateClasstt)(classt **self); typedef classt* (*duplicateClasstt)(classt **self); -struct class { +typedef struct { // base class - const char *type; classPrintt print; // base class end - int a; - char *s; terminateClasstt terminate; duplicateClasstt duplicate; freeClasstt free; +} classFunctionst; + +static classFunctionst *classF = NULL; + +struct class { + // base class + const char *type; + classFunctionst *f; + // base class end + + int a; + char *s; }; // Initiate/Terminate Objects @@ -59,12 +74,16 @@ void initiateAllocateclasst(classt **self) { void initiateclasst(classt *self) { - self->free = freeclasst; - self->terminate = terminateclasst; - self->duplicate = duplicateclasst; + if (!classF) { + classF = malloc(sizeof(classFunctionst)); + classF->free = freeclasst; + classF->terminate = terminateclasst; + classF->duplicate = duplicateclasst; + classF->print = print; + } + self->f = classF; self->a = 0; self->s = NULL; - self->print = print; // class name self->type = classtName; @@ -77,7 +96,7 @@ void freeclasst(classt *self) { void terminateclasst(classt **self) { - (*self)->free(*self); + (*self)->f->free(*self); free((*self)); *self = NULL; } @@ -98,28 +117,30 @@ void print(classt *self, char *s) { printf(self->s); } +// END class + void main() { printf("Size of class: %d", sizeof(classt)); createclasst(obj1); - obj1.print(&obj1, "\nRegular Object\n"); + obj1.f->print(&obj1, "\nRegular Object\n"); printf("\ndirect access %s", obj1.s); - obj1.free(&obj1); + obj1.f->free(&obj1); createAllocateclasst(obj2); - obj2->print(obj2, "\nObject in heap\n"); - obj2->terminate(&obj2); + obj2->f->print(obj2, "\nObject in heap\n"); + obj2->f->terminate(&obj2); // initiate alternative classt obj3; initiateclasst(&obj3); - obj3.print(&obj3, "\n\nObj 3\n"); + obj3.f->print(&obj3, "\n\nObj 3\n"); freeO(obj3); classt *obj4; initiateAllocateclasst(&obj4); - obj4->print(obj4, "\nOBJ 4\n"); + obj4->f->print(obj4, "\nOBJ 4\n"); deleteO(obj4); // unknown object @@ -127,7 +148,7 @@ void main() { baset *o; o = (baset *) unObj; printf("\nObject type: %s\n", o->type); - o->print(o, "\nOBJ UNKNOWN\n"); + o->f->print(o, "\nOBJ UNKNOWN\n"); if (isOType(unObj, classtName)) { printf("Object type: %s\n", ((baset *) unObj)->type);