Go Down

Topic: Delegate system for member function pointers (Read 4 times) previous topic - next topic


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.

Excellent. I look forward to trying it out.



its for member function pointers

"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.


Go Up