Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!think!mintaka!bloom-beacon!CTC.CONTEL.COM!gary From: gary@CTC.CONTEL.COM (Gary Bisaga x4219) Newsgroups: comp.windows.x Subject: Re: Defining Callbacks from resource files Message-ID: <8912211313.AA03596@ctc.contel.com> Date: 21 Dec 89 13:13:36 GMT Sender: daemon@athena.mit.edu (Mr Background) Organization: The Internet Lines: 46 Martin Brunecky writes: > Someone may complain that using registeration routines is not very elegant. > But since a programmer has to write most of his callbacks, one registration > call per callback seems fair to me. The problem here, of course, is not only that the programmer must write a second function as an "initialization" for the callback registration, but also that this function must be called essentially automatically at process startup time. This type of thing is a pain to remember, especially when there are multiple programmers and many such functions -- it's exceedingly easy to forget to do consistently. With C++, there is a simple and automatic way of doing this. Define a class called, for example, XXX as follows: class XXX { int dummy; public: XXX(void (*callbackFunction)(), char *registrationName) {registerCallback(callbackFunction, registrationName);} }; The registerCallback function, invoked from the constructor, should make an entry into a table (linked list, hashed table, fill in the blank) registering the connection between the function and string. Then define a macro DEFINE_CALLBACK which calls the XXX constructor as follows: #define DEFINE_CALLBACK(functionName) \ void functionName(); \ static class XXX functionName_Callback(functionName, "functionName"); \ void functionName Every function which must be registered as a callback should be defined using the DEFINE_CALLBACK macro. Usage is as follows: DEFINE_CALLBACK(MyCallbackFunc)(int firstArg, void *secondArg) { ... } This works, of course, because of C++'s automatic calls to constructors at the beginning of the scope of the variable, which in this case is process startup. Gary Bisaga (gary@ctc.contel.com)