source: cpp/frams/util/callbacks.h @ 121

Last change on this file since 121 was 121, checked in by sz, 11 years ago

updated file headers and makefiles

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
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
13class CallbackNode
14{
15public:
16virtual ~CallbackNode() {}
17virtual void action(long calldata)=0;
18virtual int equals(CallbackNode*n) {return (this==n);}
19};
20
21#ifdef USEMEMBERCALLBACK
22class CallBase;
23class MemberCallbackNode :public CallbackNode
24{
25void *userdata;
26CallBase *object;
27void (CallBase::*member)(void*,long);
28  public:
29MemberCallbackNode(CallBase *o,void (CallBase::*m)(void*,long),void *d):object(o),member(m),userdata(d) {}
30void action(long calldata) {(object->*member)(userdata,calldata);}
31int equals(CallbackNode*);
32};
33#define MEMBERCALLBACK(obj,mem,dat) new MemberCallbackNode((CallBase*)(obj),(void (CallBase::*)(void*,long))(mem),(void*)(dat))
34#endif
35
36class FunctionCallbackNode :public CallbackNode
37{
38void *userdata;
39void (*fun)(void*,long);
40  public:
41FunctionCallbackNode(void (*f)(void*,long),void *d):userdata(d),fun(f) {}
42void action(long calldata) {(*fun)(userdata,calldata);}
43int equals(CallbackNode*);
44};
45#define FUNCTIONCALLBACK(fun,dat) new FunctionCallbackNode((void (*)(void*,long))(fun),(void*)(dat))
46
47class StatrickCallbackNode :public CallbackNode
48{
49void *object;
50void *userdata;
51void (*fun)(void*,void*,long);
52  public:
53StatrickCallbackNode(void *o,void (*f)(void*,void*,long),void *d):object(o),userdata(d),fun(f) {}
54void action(long calldata) {(*fun)(object,userdata,calldata);}
55int 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
78class Callback: protected SList
79{
80public:
81~Callback();
82CallbackNode* add(CallbackNode*n);
83CallbackNode* add(void (*f)(void*,long),void *d)
84        {return add(new FunctionCallbackNode(f,d));}
85void remove(void (*f)(void*,long),void *d)
86        {remove(new FunctionCallbackNode(f,d));}
87void remove(CallbackNode*n);
88void removeNode(CallbackNode*n);
89void operator+=(void* fun) {add((void (*)(void*,long))fun,0);}
90void operator-=(void* fun) {remove((void (*)(void*,long))fun,0);}
91void action(long data);
92void action() {action(0);}
93int size() {return SList::size();}
94void 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
Note: See TracBrowser for help on using the repository browser.