Because I like programming in objects/classes, I got myself in a situation to use timers and callbacks. This is generally an issue in c++ (member function pointers)

I found this Delegate system that covers a whole lot (too much for standard Arduino programming)http://www.codeproject.com/KB/cpp/FastDelegate.aspx

I works very nice, changed the SimpleTimer lib (Author: mromani@ottotecnica.com) to be used with this Delegate system.

What I was thinking, since Arduino programming is getting more and more professional on one-side, having a standard low-footprint Delegate system could be a nice addition.

I already got it working and the extra memory needed is very little, the guy who made it states that in most cases it would only compile to two lines of assembly. What still needs to be done is cutting it up (i guess 80% of the code could be discarded since Arduino uses a single compiler) and making it integegrate easy (without API breaks)

this is my compile output for a small program with this Delegate system:

Author: mromani@ottotecnica.com using this system and some other advantages.

I'll make it a arduino lib with an example attached and post it for download in this thread. I'll try to get this done before the weekend.

@westfw: its for member function pointers, a simple function pointer wouldnt need a delegate system ofcourse. Although my current setup does all the magic in the background so you could easily throw in a simple function pointer or get more freaky and use a delegate for a member function pointer like so:

With no changes in the timerlib object. This is ussually a hackle because non-member functions dont get a this operator, where member functions do get these. If your this operator gets screwed the called function has no clue whats going on in its own class/object.

But, I'll get back on this thread with some descent examples so its all getting a little clearer.

"pointers to functions that are defined as part of a class" ?Like: getting a pointer to digitalRead() is easy (because it's just a function), but getting a pointer to the function that implements Serial.write() would be difficult (because it's encapsulated inside the class)?

All these new-age terms! "Delegate", "member", "class"!Pretend I'm a dinosaur who's more familiar with "AOBJN", "JCL", and "Chaff." :-)

The problem is that "regular" C callbacks do not carry any state information. All class member functions have an implicit "this" parameter which means they can't be called by a C callback. Using delegates gets round the problem as it creates a callback that captures the "this" parameter.

It allows you to callback to a specific object. If you had a Mega with 4 serial ports in use, a C callback couldn't differentiate between the four serial ports but a delegate could, as it will capture the "this" parameter identifying which serial port it's related to.

I've made it into a small library that you can unpack in the Arduino folder, I made it in 022, not 1.0. Have not tested it in 1.0 to (don't think it will work in the new one yet cause of the class differences) something for later on.

So, just unpack in the libraries folder of the arduino install, start/restart arduino software and you can look for it in the examples and libraries menu.

I did not made the delegate system API friendly yet but, its not that difficult as you will see in my code (kept it small). If it starts interesting more people I'll consider making the Delegate system more friendly.

Great solution, a day of trying to figure this pointer crap out, and got it done in no time with this, once I figured out that you have to update his modified SimpleTimer.h to Arduino 1.0 by changing the <Wprogram.h> include to <Arduino.h>