mirror of
https://github.com/sigmasternchen/crap-libs
synced 2025-03-15 07:38:56 +00:00
instanceof is now working
This commit is contained in:
parent
c3886fb0b4
commit
5a0e743b5d
3 changed files with 43 additions and 14 deletions
|
@ -23,18 +23,18 @@ void method(test, destruct)(test_t* this) {
|
|||
this->super.destruct((object_t*) this);
|
||||
}
|
||||
|
||||
void method(test, populate)(test_t* obj, const char* string) {
|
||||
void method(test, populate)(test_t* obj) {
|
||||
populate(object)((object_t*) obj, test_class);
|
||||
|
||||
obj->string = string;
|
||||
|
||||
add_method(obj, test, destruct);
|
||||
add_method(obj, test, print);
|
||||
}
|
||||
|
||||
test_t* method(test, construct)(const char* string) {
|
||||
test_t* obj = malloc(sizeof(test_t));
|
||||
populate(test)(obj, string);
|
||||
populate(test)(obj);
|
||||
|
||||
obj->string = string;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -42,9 +42,13 @@ test_t* method(test, construct)(const char* string) {
|
|||
int main(void) {
|
||||
test_t* obj = new test("Hallo Welt");
|
||||
|
||||
class_t c = oop_get_class_from_obj((object_t*) obj);
|
||||
printf("class: %s\n", oop_get_class_name(c));
|
||||
printf("superclass: %s\n", oop_get_class_name(oop_get_super_class(c)));
|
||||
printf("instanceof test: %i\n", instanceof(obj, test_class));
|
||||
printf("instanceof object: %i\n", instanceof(obj, object_class));
|
||||
|
||||
obj->print(obj);
|
||||
|
||||
obj->destruct(obj);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
32
oop/oop.c
32
oop/oop.c
|
@ -2,12 +2,16 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class_id_t class_ids = 0;
|
||||
|
||||
meta_class_t classes[MAX_CLASSES];
|
||||
|
||||
class_id_t oop_add_class(const char* name, class_t super, bool instanceable) {
|
||||
if (oop_class_exists(name))
|
||||
return oop_id_from_name(name);
|
||||
|
||||
classes[class_ids] = (meta_class_t) {
|
||||
.name = name,
|
||||
.super = super.id,
|
||||
|
@ -17,14 +21,20 @@ class_id_t oop_add_class(const char* name, class_t super, bool instanceable) {
|
|||
}
|
||||
|
||||
bool oop_instance_of_id(void* object, class_id_t id) {
|
||||
if (sizeof (object) < sizeof (object_t))
|
||||
return false; // not an object
|
||||
object_t* obj = (object_t*) object;
|
||||
return obj->meta_obj.type.id == id;
|
||||
class_id_t cid = ((object_t*) object)->meta_obj.type.id;
|
||||
meta_class_t c = classes[cid];
|
||||
if (cid == id)
|
||||
return true;
|
||||
// iterate through superclasses of object
|
||||
for (; c.super != NO_CLASS_ID; c = classes[c.super]) {
|
||||
if (c.super == id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool oop_instance_of(void* object, class_t type) {
|
||||
return oop_instance_of_id(object, type.id);
|
||||
return oop_instance_of_id(object, type.id);
|
||||
}
|
||||
|
||||
const char* oop_get_class_name_by_id(class_id_t id) {
|
||||
|
@ -47,6 +57,18 @@ bool oop_class_exists(const char* class_name) {
|
|||
return oop_id_from_name(class_name) != NO_CLASS_ID;
|
||||
}
|
||||
|
||||
class_t oop_class_from_id(class_id_t id) {
|
||||
return (class_t) {.id = id};
|
||||
}
|
||||
|
||||
class_t oop_get_super_class(class_t type) {
|
||||
return oop_class_from_id(classes[type.id].super);
|
||||
}
|
||||
|
||||
class_t oop_get_class_from_obj(object_t* obj) {
|
||||
return oop_class_from_id(((object_t*) obj)->meta_obj.type.id);
|
||||
}
|
||||
|
||||
|
||||
class_t object_class;
|
||||
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#define class(name, type, superclass, instanceable) __attribute__ ((constructor)) static void add_##name##_class(void) { type.id = oop_add_class(#name, superclass, instanceable); } typedef struct name
|
||||
#define class(name, type, superclass, instanceable) __attribute__ ((constructor)) static void add_##name##_class(void) { type.id = oop_add_class(#name, superclass, instanceable);} typedef struct name
|
||||
#define method(class, method) class ##_method_## method
|
||||
#define add_method(object, class, method) object->method = class ##_method_## method
|
||||
|
||||
|
@ -14,6 +15,7 @@
|
|||
#define construct(name) name ##_method_construct
|
||||
#define populate(name) name ##_method_populate
|
||||
|
||||
#define instanceof(obj, class) oop_instance_of(obj, class)
|
||||
|
||||
#define call(obj, method) (obj)->method((obj))
|
||||
#define new
|
||||
|
@ -22,8 +24,6 @@
|
|||
#define NO_CLASS_ID -1
|
||||
#define NO_SUPER_CLASS (class_t){.id = NO_CLASS_ID}
|
||||
|
||||
|
||||
|
||||
typedef int class_id_t;
|
||||
|
||||
typedef struct meta_class {
|
||||
|
@ -56,6 +56,9 @@ bool oop_instance_of(void*, class_t);
|
|||
const char* oop_get_class_name(class_t);
|
||||
class_id_t oop_id_from_name(const char*);
|
||||
bool oop_class_exists(const char*);
|
||||
class_t oop_class_from_id(class_id_t);
|
||||
class_t oop_get_super_class(class_t);
|
||||
class_t oop_get_class_from_obj(object_t*);
|
||||
|
||||
object_t* method(object, construct)(void);
|
||||
void method(object, populate)(object_t* obj, class_t);
|
||||
|
|
Loading…
Reference in a new issue