summaryrefslogtreecommitdiff
path: root/include/wrapable.h
blob: bcc37762f3691d2a7b7c9f63f9c34072004fb8c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef _WRAPABLE_H_
#define _WRAPABLE_H_

#include <vector>

#define WRAPABLE_DEF(rtype, func, ...) public: virtual rtype func (__VA_ARGS__); bool func ## _enabled; public:

#define WRAPABLE_DEF_FUNC(func, ...) \
{ \
    if (mHandler) \
    { \
	func ## _enabled = false; \
	mHandler-> func (__VA_ARGS__); \
    } \
}

#define WRAPABLE_DEF_FUNC_RETURN(func, ...) \
{ \
    if (mHandler) \
    { \
	func ## _enabled = false; \
	return mHandler-> func (__VA_ARGS__); \
    } \
    return 0; \
}

#define WRAPABLE_DEF_FUNC_WITH_RETURN(ret, func, ...) \
{ \
    if (mHandler) \
    { \
	func ## _enabled = false; \
	return mHandler-> func (__VA_ARGS__); \
    } \
    return ret; \
}

#define WRAPABLE_HND(rtype, func, ...) public: rtype func (__VA_ARGS__); private: unsigned int mCurr_ ## func ; public:

#define WRAPABLE_HND_FUNC(func, ...) \
{ \
    unsigned int curr = mCurr_ ## func; \
    while (mCurr_ ## func < ifce.size() && \
           !ifce[mCurr_ ## func]->func ## _enabled) \
        mCurr_ ## func ++; \
    if (mCurr_ ## func < ifce.size()) \
    { \
	ifce[mCurr_ ## func++]-> func (__VA_ARGS__); \
	mCurr_ ## func = curr; \
	return; \
    } \
    mCurr_ ## func = curr; \
}

#define WRAPABLE_HND_FUNC_RETURN(rtype, func, ...) \
{ \
    int   curr = mCurr_ ## func; \
    rtype rv; \
    while (mCurr_ ## func < ifce.size() && \
           !ifce[mCurr_ ## func]->func ## _enabled) \
        mCurr_ ## func ++; \
    if (mCurr_ ## func < ifce.size()) \
    { \
	rv = ifce[mCurr_ ## func++]-> func (__VA_ARGS__); \
	mCurr_ ## func = curr; \
	return rv; \
    } \
    mCurr_ ## func = curr; \
}

#define WRAPABLE_INIT_FUNC(func) \
    func ## _enabled = true

#define WRAPABLE_INIT_HND(func) \
    mCurr_ ## func = 0

template <typename T>
class WrapableInterface {
    protected:
	WrapableInterface () : mHandler(0) {};
	virtual ~WrapableInterface () {};

	void setHandler (T *handler)
	{
	    mHandler = handler;
	}
        T *mHandler;
};

template <typename T>
class WrapableHandler : public T {
    public:
	void add (T *);
	void remove (T *);
	
    protected:
	
        std::vector<T *> ifce;
};

template <typename T>
void WrapableHandler<T>::add (T *obj)
{
    ifce.insert (ifce.begin(), obj);
};

template <typename T>
void WrapableHandler<T>::remove (T *obj)
{
    typename std::vector<T *>::iterator it;
    for (it = ifce.begin(); it != ifce.end(); it++)
	if ((*it) == obj)
	{
	    ifce.erase (it);
	    break;
	}
}

#endif