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

Last change on this file since 1101 was 793, checked in by Maciej Komosinski, 6 years ago

Code formatting

  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[109]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
14class CallbackNode
15{
16public:
[793]17        virtual ~CallbackNode() {}
18        virtual void action(intptr_t calldata) = 0;
19        virtual int equals(CallbackNode*n) { return (this == n); }
[109]20};
21
22#ifdef USEMEMBERCALLBACK
23class CallBase;
24class MemberCallbackNode :public CallbackNode
25{
[793]26        void *userdata;
27        CallBase *object;
28        void (CallBase::*member)(void*, intptr_t);
29public:
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); }
32        int equals(CallbackNode*);
[109]33};
[247]34#define MEMBERCALLBACK(obj,mem,dat) new MemberCallbackNode((CallBase*)(obj),(void (CallBase::*)(void*,intptr_t))(mem),(void*)(dat))
[109]35#endif
36
37class FunctionCallbackNode :public CallbackNode
38{
[793]39        void *userdata;
40        void(*fun)(void*, intptr_t);
41public:
42        FunctionCallbackNode(void(*f)(void*, intptr_t), void *d) :userdata(d), fun(f) {}
43        void action(intptr_t calldata) { (*fun)(userdata, calldata); }
44        int equals(CallbackNode*);
[109]45};
[247]46#define FUNCTIONCALLBACK(fun,dat) new FunctionCallbackNode((void (*)(void*,intptr_t))(fun),(void*)(dat))
[109]47
48class StatrickCallbackNode :public CallbackNode
49{
[793]50        void *object;
51        void *userdata;
52        void(*fun)(void*, void*, intptr_t);
53public:
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); }
56        int equals(CallbackNode*);
[109]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
[793]62   add(Function* fun, void* anydata)
[109]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)
[793]65   fun(void* anydata,intptr_t eventdata)
[109]66
67   'StatrickCallbackNode' uses static functions to emulate object member calls.
68   @see statrick.h
[793]69
[109]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(...);
[793]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!
[109]76
[793]77   */
[109]78
[793]79class Callback : protected SList
[109]80{
81public:
[793]82        ~Callback();
83        CallbackNode* add(CallbackNode*n);
84        CallbackNode* add(void(*f)(void*, intptr_t), void *d)
85        {
86                return add(new FunctionCallbackNode(f, d));
87        }
88        void remove(void(*f)(void*, intptr_t), void *d)
89        {
90                remove(new FunctionCallbackNode(f, d));
91        }
92        void remove(CallbackNode*n);
93        void removeNode(CallbackNode*n);
94        void operator+=(void* fun) { add((void(*)(void*, intptr_t))fun, 0); }
95        void operator-=(void* fun) { remove((void(*)(void*, intptr_t))fun, 0); }
96        void action(intptr_t data);
97        void action() { action(0); }
98        int size() { return SList::size(); }
99        void clear() { SList::clear(); }
[109]100};
101
102///////////////////
103
[247]104#define STCALLBACKDEF(name) STATRICKDEF2(name,void*,intptr_t)
105#define STCALLBACKDEFC(cls,name) STATRICKSTUB2(cls,name,void*,intptr_t)  \
106        void name(void* arg1,intptr_t arg2)
107#define VIRTCALLBACKDEF(name) STATRICKSTUB2(STATRICKCLASS,name,void*,intptr_t)  \
108        virtual void name(void* arg1,intptr_t arg2)
109#define VIRTCALLBACKDEFC(cls,name) STATRICKSTUB2(cls,name,void*,intptr_t)  \
110        virtual void name(void* arg1,intptr_t arg2)
[109]111
112/* STCALLBACKDEFC(Class,name)
113
[793]114         |
115         v
[109]116
[793]117         #define STATRICKCLASS Class
118         STCALLBACKDEF(name)
119         #undef STATRICKCLASS
120         */
[109]121
[247]122#define CALLBACKARGS void* arg1,intptr_t arg2
[109]123
124#endif
Note: See TracBrowser for help on using the repository browser.