2015-05-17 17:07:48 -05:00
|
|
|
// 17 may 2015
|
|
|
|
#include "out/ui.h"
|
|
|
|
#include "uipriv.h"
|
|
|
|
|
|
|
|
struct typeinfo {
|
|
|
|
const char *name;
|
|
|
|
uintmax_t parent;
|
2015-05-29 13:56:11 -05:00
|
|
|
size_t size;
|
2015-05-17 17:07:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct ptrArray *types = NULL;
|
|
|
|
|
2015-05-29 13:56:11 -05:00
|
|
|
uintmax_t uiRegisterType(const char *name, uintmax_t parent, size_t size)
|
2015-05-17 17:07:48 -05:00
|
|
|
{
|
|
|
|
struct typeinfo *ti;
|
|
|
|
|
|
|
|
if (types == NULL) {
|
|
|
|
types = newPtrArray();
|
|
|
|
// reserve ID 0
|
|
|
|
ptrArrayAppend(types, NULL);
|
|
|
|
}
|
|
|
|
ti = uiNew(struct typeinfo);
|
|
|
|
ti->name = name;
|
|
|
|
ti->parent = parent;
|
2015-05-29 13:56:11 -05:00
|
|
|
ti->size = size;
|
2015-05-17 17:07:48 -05:00
|
|
|
ptrArrayAppend(types, ti);
|
|
|
|
return types->len - 1;
|
|
|
|
}
|
|
|
|
|
2015-05-17 17:33:18 -05:00
|
|
|
void *uiIsA(void *p, uintmax_t id, int fail)
|
2015-05-17 17:07:48 -05:00
|
|
|
{
|
|
|
|
uiTyped *t;
|
2015-05-17 18:19:51 -05:00
|
|
|
struct typeinfo *ti, *ti2;
|
2015-05-17 17:07:48 -05:00
|
|
|
uintmax_t compareTo;
|
|
|
|
|
|
|
|
if (id == 0 || id >= types->len)
|
|
|
|
complain("invalid type ID given to uiIsA()");
|
|
|
|
t = (uiTyped *) p;
|
|
|
|
compareTo = t->Type;
|
2015-05-17 17:53:06 -05:00
|
|
|
if (compareTo == 0)
|
|
|
|
complain("object %p has no type in uiIsA()", t);
|
2015-05-17 17:07:48 -05:00
|
|
|
for (;;) {
|
2015-05-17 17:53:06 -05:00
|
|
|
if (compareTo >= types->len)
|
2015-05-17 17:07:48 -05:00
|
|
|
complain("invalid type ID in uiIsA()", t);
|
|
|
|
if (compareTo == id)
|
|
|
|
return t;
|
2015-05-17 18:19:51 -05:00
|
|
|
ti = ptrArrayIndex(types, struct typeinfo *, compareTo);
|
2015-05-17 17:07:48 -05:00
|
|
|
if (ti->parent == 0)
|
|
|
|
break;
|
|
|
|
compareTo = ti->parent;
|
|
|
|
}
|
2015-05-17 17:33:18 -05:00
|
|
|
if (fail) {
|
2015-05-17 18:19:51 -05:00
|
|
|
ti = ptrArrayIndex(types, struct typeinfo *, id);
|
|
|
|
ti2 = ptrArrayIndex(types, struct typeinfo *, t->Type);
|
|
|
|
complain("object %p not a %s in uiIsA() (is a %s)", t, ti->name, ti2->name);
|
2015-05-17 17:33:18 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
2015-05-17 17:07:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO free type info
|
2015-05-29 13:56:11 -05:00
|
|
|
|
|
|
|
uiTyped *newTyped(uintmax_t type)
|
|
|
|
{
|
|
|
|
struct typeinfo *ti;
|
|
|
|
uiTyped *instance;
|
|
|
|
|
|
|
|
if (type == 0 || id >= types->len)
|
|
|
|
complain("invalid type ID given to newTyped()");
|
|
|
|
ti = ptrArrayIndex(types, struct typeinfo *, type);
|
|
|
|
instance = (uiTyped *) uiAlloc(ti->size, ti->name);
|
|
|
|
instance->Type = type;
|
|
|
|
return instance;
|
|
|
|
}
|