Home » U++ Library support » U++ Callbacks and Timers » How to use callback with 3 parameters
|
Re: How to use callback with 3 parameters [message #13479 is a reply to message #13473] |
Thu, 10 January 2008 17:38 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
malya wrote on Thu, 10 January 2008 06:35 |
void MyFunc(int i, int i1, int i2)
{
...
}
void main()
{
PostCallback(callback(MyFunc, 1, 2, 3)); // ?????
}
|
PostCallback(callback3(MyFunc, 1, 2, 3)); // ?????
|
|
|
|
|
Re: How to use callback with 3 parameters [message #13499 is a reply to message #13498] |
Fri, 11 January 2008 10:17 |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
callback3 does not exits at the moment, the closest is callback2. You will have to use another technique, such as packing the data into a structure and passing that instead.
struct int3
{
int3(int _a, int _b, int _c) : a(_a), b(_b), c(_c) { }
int a, b, c;
};
void MyFunc(int3 i)
{
...
}
void main()
{
PostCallback(callback1(MyFunc, int3(1, 2, 3))); // ?????
}
There are other ways around this also.
[Updated on: Fri, 11 January 2008 10:18] Report message to a moderator
|
|
|
Re: How to use callback with 3 parameters [message #13501 is a reply to message #13498] |
Fri, 11 January 2008 11:11 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
malya wrote on Fri, 11 January 2008 03:47 | Please post me a small example.
Thanks!
|
Suprisingly, callback3 was somehow forgotten, while callback4 is there...
Well, it is now fixed. Quick fix
template <class OBJECT_, class METHOD_, class T1, class T2, class T3>
struct CallbackMethodActionArg3Pte : public CallbackAction {
Ptr<OBJECT_> object;
METHOD_ method;
T1 arg1;
T2 arg2;
T3 arg3;
void Execute() { if(object) (object->*method)(arg1, arg2, arg3); }
CallbackMethodActionArg3Pte(OBJECT_ *object, METHOD_ method, T1 arg1, T2 arg2, T3 arg3)
: object(object), method(method), arg1(arg1), arg2(arg2), arg3(arg3) {}
};
template <class Object, class R, class O, class A, class B,class C, class T1, class T2, class T3>
Callback pteback3(Object *object, R (O::*method)(A, B, C), T1 arg1, T2 arg2, T3 arg3) {
return Callback(new CallbackMethodActionArg3Pte<Object, R (O::*)(A,B,C), T1, T2, T3>
(object, method, arg1, arg2, arg3));
}
template <class OBJECT_, class METHOD_, class T1, class T2, class T3>
struct CallbackMethodActionArg3 : public CallbackAction
{
OBJECT_ *object;
METHOD_ method;
T1 arg1;
T2 arg2;
T3 arg3;
void Execute() { (object->*method)(arg1, arg2, arg3); }
CallbackMethodActionArg3(OBJECT_ *object, METHOD_ method, T1 arg1, T2 arg2, T3 arg3)
: object(object), method(method), arg1(arg1), arg2(arg2), arg3(arg3) {}
};
template <class Object, class R, class O, class A, class B, class C, class T1, class T2, class T3>
Callback callback3(Object *object, R (O::*method)(A, B, C), T1 arg1, T2 arg2, T3 arg3)
{
return Callback(
new CallbackMethodActionArg3<Object, R (O::*)(A, B, C), T1, T2, T3>(object, method, arg1, arg2, arg3));
}
template <class Object, class R, class O, class A, class B, class C, class T1, class T2, class T3>
Callback callback3(const Object *object, R (O::*method)(A, B, C) const, T1 arg1, T2 arg2, T3 arg3) {
return Callback(new CallbackMethodActionArg4<Object, R (O::*)(A, B, C) const, T1, T2, T3>
(object, method, arg1, arg2, arg3));
}
template <class X, class T1, class T2, class T3, class HC = X>
struct CallbackActionCallArg3 : public CallbackAction {
X x;
T1 arg1;
T2 arg2;
T3 arg3;
void Execute() { x(arg1, arg2, arg3); }
CallbackActionCallArg3(X x, T1 arg1, T2 arg2, T3 arg3)
: x(x), arg1(arg1), arg2(arg2), arg3(arg3) {}
};
template <class R, class A, class B, class C, class T1, class T2, class T3>
Callback callback3(R (*fn)(A, B, C), T1 arg1, T2 arg2, T3 arg3) {
return Callback(
new CallbackActionCallArg3<R (*)(A, B, C), T1, T2, T3, uintptr_t>(fn, arg1, arg2, arg3));
}
template <class A, class B, class C, class T1, class T2, class T3>
Callback callback3(Callback3<A, B, C> cb, T1 arg1, T2 arg2, T3 arg3) {
return Callback(
new CallbackActionCallArg3<Callback4<A, B,C,D>, T1, T2, T3>(cb, arg1, arg2, arg3));
}
add to Core/Callback.h.
Note: In fact, the reason why this was unnoticed until now is that for "higher order callbacks", it is usually better to pass the struct as single parameter. We were reluctant to extend the number of parameters beyond 2, which is the maximum that was ever needed for CtrlLib or any application we did.
Mirek
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 00:17:36 CEST 2024
Total time taken to generate the page: 0.07477 seconds
|