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 » PROPOSAL: delegate like separation of method and args in Callbacks
PROPOSAL: delegate like separation of method and args in Callbacks [message #27411] Fri, 16 July 2010 08:09 Go to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
hi folks,

here comes an interesting mod for the Callback layer.

In the current Callback implementation, the arguments (or parameters) to the stored method informations are kept together in a class.

template <class OBJECT_, class METHOD_, class T>
struct CallbackMethodActionArgPte : public CallbackActionArg<T> {
	Ptr<OBJECT_>  object;
	METHOD_       method;
	T             arg;

	void    Execute() { if(object) (object->*method)(arg); }

	CallbackMethodActionArgPte(OBJECT_ *object, METHOD_ method, T arg)
	: object(object), method(method), arg(arg)  {}
};


this is not veeery handy, if one thinks of Callbacks beeing a 'almost' delegate (not too strictly though, here, our delegates can only be named methods, no anonymous code). one key feature about delegates (in C#) i.e. is that they actually dont care about the class type (or only at the beginning maybe). sth like "i dont care the class type instance as long as the method matches my signature". thus it is possible to reuse a delegate, bind it to a another (maybe even different) class type method (with same signature) and continue. but this is not what its about here Smile

imagine you created a Callback somewhere in a templated function, but dont keep the information about the class type up to upper layer because you dont need it there, the code is too general. but you know the method types and want to change the parameters stored in a Callback. what do? here a proposal that makes cool stuff possible: (example for 1 parameter, others analogue)

Callback.h
//new class for separated arguments from class/method types
template <class T>
struct CallbackActionArg : public CallbackAction {
	T             arg;
	CallbackActionArg(T arg) : arg(arg) {}
};

template <class OBJECT_, class METHOD_, class T>
struct CallbackMethodActionArg : public CallbackActionArg<T> {
	OBJECT_  *object;
	METHOD_   method;
//	T         arg;

	void    Execute() { (object->*method)(arg); }

	CallbackMethodActionArg(OBJECT_ *object, METHOD_ method, T arg_)
	: CallbackActionArg<T>(arg_), object(object), method(method) {}
};


this makes the following possible

	Callback cb4;

	CallbackActionArg<int> * pcba = NULL; //no class information here
	pcba = new CallbackMethodActionArg<MyCallback, void (MyCallback::*)(int), int>(this, &MyCallback::Action1, 45); //created stronlgy typed and assigned a method, delegate like :) could be created later with a DELEGATE() helper or sth.
	
	cb4 = Callback(pcba);
	cb4();
	pcba->arg = 65; //here we can change the argument, but dont need to know which class type / instance it was bound to
	cb4();


i tried / tested it. it works fine. please analyze the aproach, give feedback, i think it is pretty usefull. a usecase was described above. i can provide a fully edited proposal Callback.h

[Updated on: Fri, 16 July 2010 08:09]

Report message to a moderator

Re: PROPOSAL: delegate like separation of method and args in Callbacks [message #27412 is a reply to message #27411] Fri, 16 July 2010 09:29 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
here comes an edited version of Callback.h, next comes a Test Package, current revision 2535 or so.
  • Attachment: Callback.h
    (Size: 16.68KB, Downloaded 324 times)

[Updated on: Fri, 16 July 2010 09:29]

Report message to a moderator

Re: PROPOSAL: delegate like separation of method and args in Callbacks [message #27413 is a reply to message #27412] Fri, 16 July 2010 09:32 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
here a small test package..
Re: PROPOSAL: delegate like separation of method and args in Callbacks [message #27467 is a reply to message #27413] Mon, 19 July 2010 12:38 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
the previous version was not tested with GCC and had compile errors cause of template stuff Smile (in this case gcc is strict).

here comes a working version on gcc.

  • Attachment: Callback.h
    (Size: 17.74KB, Downloaded 435 times)
Re: PROPOSAL: delegate like separation of method and args in Callbacks [message #27538 is a reply to message #27467] Thu, 22 July 2010 11:45 Go to previous message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
i managed to overcome stuff with current behaviour. analyzing Callbacks deeper came visible the beatuy Smile

i'll post stuff concering this later, explaining the shortcomings of my first ideas.. maybe this will be of help.
Previous Topic: partial parametrizing of Callback<>
Next Topic: Date iterator
Goto Forum:
  


Current Time: Fri Mar 29 15:58:09 CET 2024

Total time taken to generate the page: 0.01350 seconds