crap-libs/oop.h

96 lines
3 KiB
C
Raw Normal View History

2017-03-07 22:52:11 +00:00
#ifndef OOP_H
#define OOP_H
#include <stdbool.h>
#include <stdarg.h>
2017-03-09 17:02:18 +00:00
#include <stdio.h>
2017-03-07 22:52:11 +00:00
2017-03-11 02:36:07 +00:00
#define interface(name) class_t name##_class; __attribute__ ((constructor)) static void add_##name##_interface(void) { name##_class.id = oop_add_class(#name, true, NO_SUPER_CLASS, NO_INTERFACES, false);}
#define class(name, superclass, interfaces, instanceable) class_t name##_class; __attribute__ ((constructor)) static void add_##name##_class(void) { name##_class.id = oop_add_class(#name, false, superclass, interfaces, instanceable);} typedef struct name
2017-03-09 15:24:02 +00:00
#define method(class, method) class ##_method_## method
2017-04-14 22:12:54 +00:00
#define add_method(object, class, method) override_method(object, class, class, method)
#define override_method(object, class, mclass, method) ((class ## _t*) object)->method = mclass ##_method_## method
2017-03-09 15:24:02 +00:00
2017-03-11 02:36:07 +00:00
#define implements(i) i##_interface
2017-03-09 15:24:02 +00:00
#define extends(type) type super
2017-03-07 22:52:11 +00:00
#define construct(name) name ##_method_construct
2017-03-09 15:24:02 +00:00
#define populate(name) name ##_method_populate
2017-03-09 17:02:18 +00:00
#define instanceof(obj, class) oop_instance_of(obj, class)
2017-03-07 22:52:11 +00:00
#define call(obj, method) (obj)->method((obj))
2017-04-14 22:12:54 +00:00
#define new(class) construct(class)
2017-03-09 17:36:36 +00:00
#define defclass struct
2017-03-07 22:52:11 +00:00
#define MAX_CLASSES 1024
2017-03-11 02:36:07 +00:00
#define MAX_INTERFACES 20 // per class
2017-03-09 15:24:02 +00:00
#define NO_CLASS_ID -1
#define NO_SUPER_CLASS (class_t){.id = NO_CLASS_ID}
2017-03-11 02:36:07 +00:00
#define NO_INTERFACES (interfaces){.nr = 0}
#define namely ,
#define and ,
2017-03-09 15:24:02 +00:00
2017-03-07 22:52:11 +00:00
typedef int class_id_t;
typedef struct meta_class {
const char* name;
2017-03-11 02:36:07 +00:00
bool interface;
2017-03-07 22:52:11 +00:00
class_id_t super;
2017-03-11 02:36:07 +00:00
class_id_t interfaces[MAX_INTERFACES];
int nrinterfaces;
2017-03-07 22:52:11 +00:00
bool instanceable;
} meta_class_t;
typedef struct class {
class_id_t id;
} class_t;
2017-03-11 02:36:07 +00:00
typedef struct iflist {
int nr;
class_t interfaces[MAX_INTERFACES];
} iflist_t;
typedef iflist_t interfaces;
2017-03-07 22:52:11 +00:00
typedef struct meta_object {
2017-03-09 15:24:02 +00:00
class_t type;
2017-03-07 22:52:11 +00:00
} meta_object_t;
2017-03-11 02:36:07 +00:00
class_id_t oop_add_class(const char*, bool, class_t, iflist_t, bool);
2017-03-09 15:24:02 +00:00
2017-04-14 22:12:54 +00:00
//#define Object construct(Object)
extern class(Object, NO_SUPER_CLASS, NO_INTERFACES, false) {
2017-03-07 22:52:11 +00:00
meta_object_t meta_obj;
void (*destruct)(defclass Object*);
2017-04-14 22:12:54 +00:00
int (*hashCode)(void*);
} Object_t;
2017-03-07 22:52:11 +00:00
2017-03-11 02:36:07 +00:00
extern interface(Cloneable)
#define Cloneable_interface void*(*clone)(void*)
#define Cloneable(...) NULL; _Pragma("GCC error \"Cannot make instance of interface Cloneable.\"");
extern interface(Compareable)
#define Compareable_interface int(*compare)(void*)
#define Compareable(...) NULL; _Pragma("GCC error \"Cannot make instance of interface Compareable.\"");
2017-03-09 15:24:02 +00:00
2017-04-14 22:12:54 +00:00
#define def_hashcode() int(*hashCode)(void*)
#define add_hashcode(obj) obj->hashCode = DefMethods_method_hashCode
int method(DefMethods, hashCode)(void*);
2017-03-09 15:24:02 +00:00
bool oop_instance_of_id(void*, class_id_t);
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*);
2017-03-09 17:02:18 +00:00
class_t oop_class_from_id(class_id_t);
class_t oop_get_super_class(class_t);
bool oop_is_instanceable(class_t);
class_t oop_get_class_from_obj(Object_t*);
void oop_check_interface(class_t, Object_t*);
2017-03-09 15:24:02 +00:00
Object_t* method(Object, construct)(void);
2017-04-14 22:12:54 +00:00
void method(Object, populate)(Object_t*, class_t);
2017-03-07 22:52:11 +00:00
#endif