]> git.decadent.org.uk Git - ion3.git/blob - libtu/objp.h
[svn-inject] Installing original source of ion3
[ion3.git] / libtu / objp.h
1 /*
2  * libtu/objp.h
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2004. 
5  *
6  * You may distribute and modify this library under the terms of either
7  * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
8  */
9
10 #ifndef LIBTU_OBJP_H
11 #define LIBTU_OBJP_H
12
13 #include "types.h"
14 #include "obj.h"
15
16 typedef void DynFun();
17
18 INTRSTRUCT(DynFunTab);
19
20 DECLSTRUCT(DynFunTab){
21     DynFun *func, *handler;
22 };
23
24 DECLSTRUCT(ClassDescr){
25     const char *name;
26     ClassDescr *ancestor;
27     int funtab_n;
28     DynFunTab *funtab;
29     void (*destroy_fn)();
30 };
31
32 #define OBJ_TYPESTR(OBJ) ((OBJ) ? ((Obj*)OBJ)->obj_type->name : NULL)
33
34 #define IMPLCLASS(CLS, ANCESTOR, DFN, DYN)                         \
35         ClassDescr CLASSDESCR(CLS)={                              \
36             #CLS, &CLASSDESCR(ANCESTOR), -1, DYN, (void (*)())DFN}
37
38 #define OBJ_INIT(O, TYPE) {((Obj*)(O))->obj_type=&CLASSDESCR(TYPE); \
39     ((Obj*)(O))->obj_watches=NULL; ((Obj*)(O))->flags=0;}
40
41 #define CREATEOBJ_IMPL(OBJ, LOWOBJ, INIT_ARGS)                     \
42     OBJ *p;  p=ALLOC(OBJ); if(p==NULL){ warn_err(); return NULL; } \
43     OBJ_INIT(p, OBJ);                                             \
44     if(!LOWOBJ ## _init INIT_ARGS) { free((void*)p); return NULL; } return p
45
46 #define SIMPLECREATEOBJ_IMPL(OBJ, LOWOBJ)                          \
47     OBJ *p;  p=ALLOC(OBJ); if(p==NULL){ warn_err(); return NULL; } \
48     OBJ_INIT(p, OBJ);                                             \
49     return p;
50
51 #define END_DYNFUNTAB {NULL, NULL}
52
53 extern DynFun *lookup_dynfun(const Obj *obj, DynFun *func,
54                              bool *funnotfound);
55 extern bool has_dynfun(const Obj *obj, DynFun *func);
56
57 #define CALL_DYN(FUNC, OBJ, ARGS)                                \
58     bool funnotfound;                                            \
59     lookup_dynfun((Obj*)OBJ, (DynFun*)FUNC, &funnotfound) ARGS;
60
61 #define CALL_DYN_RET(RETV, RET, FUNC, OBJ, ARGS)                 \
62     typedef RET ThisDynFun();                                    \
63     bool funnotfound;                                            \
64     ThisDynFun *funtmp;                                          \
65     funtmp=(ThisDynFun*)lookup_dynfun((Obj*)OBJ, (DynFun*)FUNC, \
66                                       &funnotfound);             \
67     if(!funnotfound)                                             \
68         RETV=funtmp ARGS;
69
70 #define HAS_DYN(OBJ, FUNC) has_dynfun((Obj*)OBJ, (DynFun*)FUNC)
71
72 #endif /* LIBTU_OBJP_H */