home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:19892 comp.std.c++:2120
- Path: sparky!uunet!cimshop!davidm
- From: davidm@consilium.com (David S. Masterson)
- Newsgroups: comp.lang.c++,comp.std.c++
- Subject: Callbacks -- suggested approaches??
- Message-ID: <DAVIDM.93Jan25171343@consilium.com>
- Date: 26 Jan 93 01:13:43 GMT
- Sender: root@cimshop.UUCP
- Organization: Consilium Inc., Mountain View, California
- Lines: 63
- X-Posting-Software: GNUS 3.13 [ NNTP-based News Reader for GNU Emacs ]
-
- I'm looking for suggestions on how to handle callback functions in C++ that
- will be more standard than the one we're currently using.
-
- Our application is a distributed system composed of many processes performing
- specialized functions. Each process is represented as a Process object with
- specializations derived from this base. Processes communicate by passing
- Message objects that contains any necessary data. The receiving Process
- object looks into the incoming Message, determines its type (a number), and
- dispatches it (via table lookup) to a callback function on the Process object
- that has been previously registered for that type of Message. The table where
- messages are looked up is (basically) of the following definition:
-
- typedef void (Process::*Method)(Message*);
- struct Callback {
- Method method
- Process* methodthis; // could be multi-threaded
- } table[MAXCALLBACKS];
-
- Methods would be dispatched like:
-
- void Process::Dispatch(Message* m) {
- return (table[Lookup(m)].methodthis->*
- table[Lookup(m)].method)(m);
- }
-
- The registration method for callbacks looks like:
-
- void Process::Register(Method m, Process* p) {
- table[i].method = m;
- table[i].methodthis = p;
- }
-
- and is called like:
-
- mainproc->Register((Method) MYProcess::callback1, myproc);
-
- My problem that precipitated this posting was the Method cast in that last
- statement. Our applications could have potentially hundreds of callbacks in
- any one process (especially the database interface), so keeping the callbacks
- that a process could handle with the process seemed to be a win for
- maintenance. However, that last statement doesn't seem to be a legal one as
- callback1 is not necessarily declared as a Method and the explicit cast is
- overriding the warning that you would get. Much of this design was done
- originally with a C++ 2.0 compiler, but we are now moving to a C++ 2.1
- compiler and trying to clean up the design in the process. Is there a more
- appropriate design for the above (that, hopefully, doesn't mean scrapping
- everything)? I've been looking at the idea of Functors, but that seems like a
- maintenance nightmare in that there would be potentially hundreds of
- single-function functors lying around in our system (a table lookup seems a
- much more straightforward approach).
-
- Any ideas?
- --
- ====================================================================
- David Masterson Consilium, Inc.
- (415) 691-6311 640 Clyde Ct.
- davidm@consilium.com Mtn. View, CA 94043
- ====================================================================
- I try to stay away from liberalism. I know how dangerous it is for
- our... Don't ask me to take a liberal position where I am right now.
- It's contrary to the administration's viewpoints. It would not be
- terribly helpful to anyone, especially me.
- -- Vice President Dan Quayle.
-