Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Callbacks and Timers » How to use callback with 3 parameters
How to use callback with 3 parameters [message #13473] Thu, 10 January 2008 12:35 Go to next message
malya is currently offline  malya
Messages: 27
Registered: June 2007
Promising Member

void MyFunc(int i, int i1, int i2)
{
    ...
}

void main()
{
    PostCallback(callback(MyFunc, 1, 2, 3)); // ?????
}
Re: How to use callback with 3 parameters [message #13479 is a reply to message #13473] Thu, 10 January 2008 17:38 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11830
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 #13490 is a reply to message #13479] Thu, 10 January 2008 20:30 Go to previous messageGo to next message
malya is currently offline  malya
Messages: 27
Registered: June 2007
Promising Member

Quote:


PostCallback(callback3(MyFunc, 1, 2, 3)); // ?????



There is an error:
error C3861: 'callback3': identifier not found, even with argument-dependent lookup

[Updated on: Thu, 10 January 2008 20:32]

Report message to a moderator

Re: How to use callback with 3 parameters [message #13498 is a reply to message #13479] Fri, 11 January 2008 09:47 Go to previous messageGo to next message
malya is currently offline  malya
Messages: 27
Registered: June 2007
Promising Member

Please post me a small example.
Thanks!
Re: How to use callback with 3 parameters [message #13499 is a reply to message #13498] Fri, 11 January 2008 10:17 Go to previous messageGo to next message
mrjt is currently offline  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 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11830
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
Re: How to use callback with 3 parameters [message #13509 is a reply to message #13501] Fri, 11 January 2008 14:11 Go to previous message
malya is currently offline  malya
Messages: 27
Registered: June 2007
Promising Member

Thanks for quick reply!
Previous Topic: Question about PostCallback from Child Thread
Next Topic: A new [Ctrl] timer id strategy
Goto Forum:
  


Current Time: Mon Apr 22 00:05:55 CEST 2019

Total time taken to generate the page: 0.01501 seconds