| 1 |
pcg |
1.1 |
/* |
| 2 |
|
|
callback.h -- C++ callback mechanism |
| 3 |
|
|
|
| 4 |
|
|
This program is free software; you can redistribute it and/or modify |
| 5 |
|
|
it under the terms of the GNU General Public License as published by |
| 6 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 7 |
|
|
(at your option) any later version. |
| 8 |
|
|
|
| 9 |
|
|
This program is distributed in the hope that it will be useful, |
| 10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
|
|
GNU General Public License for more details. |
| 13 |
|
|
|
| 14 |
|
|
You should have received a copy of the GNU General Public License |
| 15 |
|
|
along with this program; if not, write to the Free Software |
| 16 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
|
|
*/ |
| 18 |
|
|
|
| 19 |
|
|
#ifndef VPE_CALLBACK_H__ |
| 20 |
|
|
#define VPE_CALLBACK_H__ |
| 21 |
|
|
|
| 22 |
|
|
template<class R, class A> |
| 23 |
|
|
class callback { |
| 24 |
|
|
struct object { }; |
| 25 |
|
|
|
| 26 |
|
|
void *obj; |
| 27 |
|
|
R (object::*meth)(A arg); |
| 28 |
|
|
|
| 29 |
|
|
// a proxy is a kind of recipe on how to call a specific class method |
| 30 |
|
|
struct proxy_base { |
| 31 |
|
|
virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
| 32 |
|
|
}; |
| 33 |
|
|
template<class O1, class O2> |
| 34 |
|
|
struct proxy : proxy_base { |
| 35 |
|
|
virtual R call (void *obj, R (object::*meth)(A), A arg) |
| 36 |
|
|
{ |
| 37 |
|
|
((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
| 38 |
|
|
(arg); |
| 39 |
|
|
} |
| 40 |
|
|
}; |
| 41 |
|
|
|
| 42 |
|
|
proxy_base *prxy; |
| 43 |
|
|
|
| 44 |
|
|
public: |
| 45 |
|
|
template<class O1, class O2> |
| 46 |
|
|
callback (O1 *object, R (O2::*method)(A)) |
| 47 |
|
|
{ |
| 48 |
|
|
static proxy<O1,O2> p; |
| 49 |
|
|
obj = reinterpret_cast<void *>(object); |
| 50 |
|
|
meth = reinterpret_cast<R (object::*)(A)>(method); |
| 51 |
|
|
prxy = &p; |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
|
|
R call(A arg) const |
| 55 |
|
|
{ |
| 56 |
|
|
return prxy->call (obj, meth, arg); |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
R operator ()(A arg) const |
| 60 |
|
|
{ |
| 61 |
|
|
return call (arg); |
| 62 |
|
|
} |
| 63 |
|
|
}; |
| 64 |
|
|
|
| 65 |
|
|
#endif |
| 66 |
|
|
|