home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Archive Magazine 1996
/
ARCHIVE_96.iso
/
discs
/
mag_discs
/
volume_9
/
issue_04
/
cpp
/
callback
Wrap
Text File
|
1995-10-28
|
2KB
|
68 lines
// callback.h
// by Tony Houghton
// version 1.00 (28 Oct 1995)
#ifndef __callback_h
#define __callback_h
#ifndef __event_h
#include "event.h"
#endif
/*
Any class to be called by a TboxCallback must have a method:
int callback(int event_code, EventStruct *event, IdBlock *);
Note lack of final void *handle argument, any additional data
can be contained within handler object.
Ideally we should be able to pass a method as an argument
of the TboxCallback class, but a bug in Acorn C++ prevents it.
A fixed function name is not too restricting, it can be overloaded
for different EventStructs
*/
template<class HandlerClass, class EventStruct>
// HandlerClass is the class of object the event will be passed to.
// EventStruct is the type the ToolboxEvent will be cast to
class TboxCallback {
int event_code;
int object_id;
HandlerClass *handler;
// It is usually more convenient to pass a pointer to the handler
// object than a reference, so you can create a TboxCallback
static int f(int code, ToolboxEvent *event, IdBlock *id_block, void *handle)
{
// handle is used to point to the relevant TboxCallback
TboxCallback<HandlerClass, EventStruct> &callback =
*((TboxCallback<HandlerClass, EventStruct> *) handle);
return callback.handler->callback(code, (EventStruct *) event, id_block);
}
public:
TboxCallback(HandlerClass *that, int code, ObjectId id = -1)
: event_code(code), object_id(id), handler(that)
{
event_register_toolbox_handler(id, event_code, f, this);
}
~TboxCallback()
{
event_deregister_toolbox_handler(object_id, event_code, f, this);
}
};
/*
So to register a callback for Window_AboutToBeShown for example,
from a method of a class called MainWindow with members:
ObjectId win_id;
and
TboxCallback<MainWindow, WindowAboutToBeShownEvent> *cb;
you could use:
cb = new TboxCallback<MainWindow, WindowAboutToBeShownEvent>
(this, Window_AboutToBeShown, win_id);
To deregister the callback simply delete cb
*/
#endif