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