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-09 15:24:02 +00:00
|
|
|
|
2017-03-10 22:30:48 +00:00
|
|
|
#define class(name, superclass, instanceable) class_t name##_class; __attribute__ ((constructor)) static void add_##name##_class(void) { name##_class.id = oop_add_class(#name, superclass, instanceable);} typedef struct name
|
2017-03-09 15:24:02 +00:00
|
|
|
#define method(class, method) class ##_method_## method
|
|
|
|
#define add_method(object, class, method) object->method = class ##_method_## method
|
|
|
|
|
|
|
|
#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))
|
|
|
|
#define new
|
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-09 15:24:02 +00:00
|
|
|
#define NO_CLASS_ID -1
|
|
|
|
#define NO_SUPER_CLASS (class_t){.id = NO_CLASS_ID}
|
|
|
|
|
2017-03-07 22:52:11 +00:00
|
|
|
typedef int class_id_t;
|
|
|
|
|
|
|
|
typedef struct meta_class {
|
|
|
|
const char* name;
|
|
|
|
class_id_t super;
|
|
|
|
bool instanceable;
|
|
|
|
} meta_class_t;
|
|
|
|
|
|
|
|
typedef struct class {
|
|
|
|
class_id_t id;
|
|
|
|
} class_t;
|
|
|
|
|
|
|
|
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-09 15:24:02 +00:00
|
|
|
class_id_t oop_add_class(const char*, class_t, bool);
|
|
|
|
|
|
|
|
#define object construct(object)
|
|
|
|
|
2017-03-10 22:30:48 +00:00
|
|
|
extern class(object, NO_SUPER_CLASS, true) {
|
2017-03-07 22:52:11 +00:00
|
|
|
meta_object_t meta_obj;
|
2017-03-09 17:36:36 +00:00
|
|
|
void (*destruct)(defclass object*);
|
2017-03-07 22:52:11 +00:00
|
|
|
} object_t;
|
|
|
|
|
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);
|
|
|
|
class_t oop_get_class_from_obj(object_t*);
|
2017-03-09 15:24:02 +00:00
|
|
|
|
|
|
|
object_t* method(object, construct)(void);
|
|
|
|
void method(object, populate)(object_t* obj, class_t);
|
2017-03-07 22:52:11 +00:00
|
|
|
|
|
|
|
#endif
|