1 | /* CrO2 datassette emulator
|
---|
2 | * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
---|
3 | *
|
---|
4 | * Distributed under the terms of the MIT licence.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <iup.h>
|
---|
8 |
|
---|
9 | template<class Handler, typename ret = int, typename... Args> class Callback
|
---|
10 | {
|
---|
11 | typedef ret(Handler::*T)(Args... args);
|
---|
12 |
|
---|
13 | public:
|
---|
14 | static int destroy(Ihandle* that)
|
---|
15 | {
|
---|
16 | Callback* call = (Callback*)IupGetAttribute(that, "LCALLBACK");
|
---|
17 | delete call;
|
---|
18 | return IUP_DEFAULT;
|
---|
19 | }
|
---|
20 |
|
---|
21 | static void create(Ihandle* handle, const char* name, Handler* self, T what)
|
---|
22 | {
|
---|
23 | Callback* cb = new Callback(self, what);
|
---|
24 | IupSetAttribute(handle, "LCALLBACK", (char*)cb);
|
---|
25 | IupSetCallback(handle, name, (Icallback)Callback::call);
|
---|
26 | IupSetCallback(handle, "LDESTROY_CB", Callback::destroy);
|
---|
27 | }
|
---|
28 |
|
---|
29 | static ret call(Ihandle* that, Args... args)
|
---|
30 | {
|
---|
31 | Callback* call = (Callback*)IupGetAttribute(that, "LCALLBACK");
|
---|
32 | return ((call->self)->*(call->what))(args...);
|
---|
33 | }
|
---|
34 |
|
---|
35 | private:
|
---|
36 | Callback(Handler* self, T what)
|
---|
37 | {
|
---|
38 | this->self = self;
|
---|
39 | this->what = what;
|
---|
40 | }
|
---|
41 |
|
---|
42 | Callback(); // do not use
|
---|
43 |
|
---|
44 | Handler* self;
|
---|
45 | T what;
|
---|
46 | };
|
---|
47 |
|
---|