objects

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

commit 905864d9cc2dabc9f7465dcf8c0cae9a8fed8ed9
parent bebbad2b753d78de1ab7c075ba1bd67090fced7f
Author: Remy Noulin (Spartatek) <remy.noulin@spartatek.se>
Date:   Sun, 26 Feb 2017 10:23:47 +0100

add base class to detect class of unknown objects

main.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)

Diffstat:
Mmain.c | 27++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/main.c b/main.c @@ -6,17 +6,29 @@ #define freeO(obj) obj.free(&obj) #define freeOPointer(obj) obj->free(&obj) #define deleteO(obj) obj->terminate(&obj) +#define isOType(obj, className) ((baset *) obj)->type == className +// Class: base +typedef struct base baset; + +struct base { + const char *type; +}; // Class: class typedef struct class classt; +static const char classtName[] = "class"; typedef void (*printt)(classt *self, char *string); typedef void (*freeClasstt)(classt *self); typedef void (*terminateClasstt)(classt **self); typedef classt* (*duplicateClasstt)(classt **self); -struct class { +struct class { + // base class + const char *type; + // base class end + int a; char *s; printt print; @@ -50,6 +62,9 @@ void initiateclasst(classt *self) { self->a = 0; self->s = NULL; self->print = print; + + // class name + self->type = classtName; } void freeclasst(classt *self) { @@ -101,4 +116,14 @@ void main() { initiateAllocateclasst(&obj4); obj4->print(obj4, "\nOBJ 4\n"); deleteO(obj4); + + // unknown object + void *unObj = &obj3; + baset *o; + o = (baset *) unObj; + printf("\nObject type: %s\n", o->type); + + if (isOType(unObj, classtName)) { + printf("Object type: %s\n", ((baset *) unObj)->type); + } }