1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #ifndef _CALLBACKS_H_ |
---|
6 | #define _CALLBACKS_H_ |
---|
7 | |
---|
8 | #include "list.h" |
---|
9 | #include "statrick.h" |
---|
10 | |
---|
11 | //#define USEMEMBERCALLBACK |
---|
12 | |
---|
13 | class CallbackNode |
---|
14 | { |
---|
15 | public: |
---|
16 | virtual ~CallbackNode() {} |
---|
17 | virtual void action(long calldata)=0; |
---|
18 | virtual int equals(CallbackNode*n) {return (this==n);} |
---|
19 | }; |
---|
20 | |
---|
21 | #ifdef USEMEMBERCALLBACK |
---|
22 | class CallBase; |
---|
23 | class MemberCallbackNode :public CallbackNode |
---|
24 | { |
---|
25 | void *userdata; |
---|
26 | CallBase *object; |
---|
27 | void (CallBase::*member)(void*,long); |
---|
28 | public: |
---|
29 | MemberCallbackNode(CallBase *o,void (CallBase::*m)(void*,long),void *d):object(o),member(m),userdata(d) {} |
---|
30 | void action(long calldata) {(object->*member)(userdata,calldata);} |
---|
31 | int equals(CallbackNode*); |
---|
32 | }; |
---|
33 | #define MEMBERCALLBACK(obj,mem,dat) new MemberCallbackNode((CallBase*)(obj),(void (CallBase::*)(void*,long))(mem),(void*)(dat)) |
---|
34 | #endif |
---|
35 | |
---|
36 | class FunctionCallbackNode :public CallbackNode |
---|
37 | { |
---|
38 | void *userdata; |
---|
39 | void (*fun)(void*,long); |
---|
40 | public: |
---|
41 | FunctionCallbackNode(void (*f)(void*,long),void *d):userdata(d),fun(f) {} |
---|
42 | void action(long calldata) {(*fun)(userdata,calldata);} |
---|
43 | int equals(CallbackNode*); |
---|
44 | }; |
---|
45 | #define FUNCTIONCALLBACK(fun,dat) new FunctionCallbackNode((void (*)(void*,long))(fun),(void*)(dat)) |
---|
46 | |
---|
47 | class StatrickCallbackNode :public CallbackNode |
---|
48 | { |
---|
49 | void *object; |
---|
50 | void *userdata; |
---|
51 | void (*fun)(void*,void*,long); |
---|
52 | public: |
---|
53 | StatrickCallbackNode(void *o,void (*f)(void*,void*,long),void *d):object(o),userdata(d),fun(f) {} |
---|
54 | void action(long calldata) {(*fun)(object,userdata,calldata);} |
---|
55 | int equals(CallbackNode*); |
---|
56 | }; |
---|
57 | #define STATRICKCALLBACK(obj,name,dat) new StatrickCallbackNode((void*)(obj),(void (*)(void*,void*,long))STATRICKNAME(name),(void*)(dat)) |
---|
58 | |
---|
59 | /** |
---|
60 | Like in old 'DuoList' you can register for an event giving function pointer |
---|
61 | add(Function* fun, void* anydata) |
---|
62 | 'fun' will be called with your pointer as the first argument (void*) |
---|
63 | and event specific value as the second argument (long) |
---|
64 | fun(void* anydata,long eventdata) |
---|
65 | |
---|
66 | 'StatrickCallbackNode' uses static functions to emulate object member calls. |
---|
67 | @see statrick.h |
---|
68 | |
---|
69 | Unregistering callbacks: |
---|
70 | The old remove(...) still works, but removeNode() is more efficient. |
---|
71 | To use it you have to store what you get from add(...); |
---|
72 | CallbackNode* node=thelist.l_add.add(&fun,data); |
---|
73 | // thelist.l_add.remove(&fun,data); |
---|
74 | thelist.l_add.removeNode(node); // this is better! |
---|
75 | |
---|
76 | */ |
---|
77 | |
---|
78 | class Callback: protected SList |
---|
79 | { |
---|
80 | public: |
---|
81 | ~Callback(); |
---|
82 | CallbackNode* add(CallbackNode*n); |
---|
83 | CallbackNode* add(void (*f)(void*,long),void *d) |
---|
84 | {return add(new FunctionCallbackNode(f,d));} |
---|
85 | void remove(void (*f)(void*,long),void *d) |
---|
86 | {remove(new FunctionCallbackNode(f,d));} |
---|
87 | void remove(CallbackNode*n); |
---|
88 | void removeNode(CallbackNode*n); |
---|
89 | void operator+=(void* fun) {add((void (*)(void*,long))fun,0);} |
---|
90 | void operator-=(void* fun) {remove((void (*)(void*,long))fun,0);} |
---|
91 | void action(long data); |
---|
92 | void action() {action(0);} |
---|
93 | int size() {return SList::size();} |
---|
94 | void clear() {SList::clear();} |
---|
95 | }; |
---|
96 | |
---|
97 | /////////////////// |
---|
98 | |
---|
99 | #define STCALLBACKDEF(name) STATRICKDEF2(name,void*,long) |
---|
100 | #define STCALLBACKDEFC(cls,name) STATRICKSTUB2(cls,name,void*,long) \ |
---|
101 | void name(void* arg1,long arg2) |
---|
102 | #define VIRTCALLBACKDEF(name) STATRICKSTUB2(STATRICKCLASS,name,void*,long) \ |
---|
103 | virtual void name(void* arg1,long arg2) |
---|
104 | #define VIRTCALLBACKDEFC(cls,name) STATRICKSTUB2(cls,name,void*,long) \ |
---|
105 | virtual void name(void* arg1,long arg2) |
---|
106 | |
---|
107 | /* STCALLBACKDEFC(Class,name) |
---|
108 | |
---|
109 | | |
---|
110 | v |
---|
111 | |
---|
112 | #define STATRICKCLASS Class |
---|
113 | STCALLBACKDEF(name) |
---|
114 | #undef STATRICKCLASS |
---|
115 | */ |
---|
116 | |
---|
117 | #define CALLBACKARGS void* arg1,long arg2 |
---|
118 | |
---|
119 | #endif |
---|
120 | |
---|