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