The recent release of libnds went with many cool changes in the way things are handled. Some of them even include the possibility of registering callback functions to customize the behavior of the new functionality.
It's about the callbacks that I want to talk right now. For us C++ developers, using callbacks the C way almost always means we have to use, either global variables, or singletons, for making the callback functions do something useful for us. While not bad *per se*, we sometimes feel uncomfortable adding "global" stuff in programs that usually do not need it.
This has been tackled in many ways in the different frameworks that I have had the chance to work with. My favorite way, thus far, is including a "state" parameter as part of the callback declaration:
Code: Select all
void VBlankHandler(void* state);
Code: Select all
VBlankHandlerClass* vbh = new VBlankHandlerClass( /* ... */ );
/* ... */
irqSetHandler(IRQ_VBLANK, VBlankHandler, vbh);
Code: Select all
void VBlankHandler(void* state)
{
VBlankHandlerClass* vbh = (VBlankHandlerClass*)state; /* ... or your favorite way of casting */
vbh->DoSomething( /* or other */ );
/* ... */
}
- Izhido