home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!sunic!liuida!isy!isy.liu.se!svan
- From: svan@isy.liu.se (Jonas Svanberg)
- Subject: Callbacks - C++ needs an extension?
- Message-ID: <1992Aug28.165108.17479@isy.liu.se>
- Keywords: callback event eventdriven extension
- Sender: news@isy.liu.se (Lord of the News)
- Reply-To: svan@isy.liu.se
- Organization: Linkoping University
- Date: Fri, 28 Aug 1992 16:51:08 GMT
- Lines: 88
-
- Hi guys!
-
- Maybe some of you have, like me, done some windowprogramming in
- C++. There are a lot of toolkits to choose from, but whatever
- you choose, wether it is a C++-package like TNT or a C-package
- like xview, you'll probably stumble upon the notion of call-
- backs.
-
- Callbacks are essencially a functionpointer you deliver to
- the windowing toolkit when creating a widget, for instance
- a button. This function is then supposed to be called when
- the user have taken some action, like pressing the button.
-
- Their existance is motified by the nature of event-driven
- programming and that it is most unpractical to have to edit
- sourcecode in the toolkit when you want a special function in
- your source to be called.
-
- Most often a button is connected to some sort of other
- object, like a window, and it is most often the window that
- knows how to response when the button is pressed.
-
- The problem is that it seems impossible in C++ to implement
- a callback strategy which calls this window-object directly...
-
- As I see things there are only two (=2) ways to implement callbacks in
- C++. Suppose we want to create a Button. A Button is probably
- configurable in many sorts of ways: position, looks, colour and
- more. *BUT* it is when you want to configure the way it
- respondes to a pushdown the trouble begins:
-
- 1) Cloning & modifying.
- You create a Button-object and then modifies it by
- registering your c-style callbackfunction.
- The callback *HAS* to be a static memberfunction or an
- ordinary c-style function (possibly a friend). The only
- other way I can think of is that a callback itself is an
- object with a method "exec" or whatever. That just pushes
- the problem further down the road though.
- The annoying part about this is that your toolkit probably
- has information on both the callback and the object that
- wants it and *still* you can't be called back the
- proper way (directly instead of through a static stubfunction.
- C++ just can't offer a portable way to implement such direct
- callbacks. Why has Borland C++ for Windows extended
- their language just to handle callbacks from Windows?
-
- 2) Creating a new class.
- You create a new Button-derived class where you redefine
- the default buttonPressed-method (virtual). This won't do.
- Probably one has a number of buttons in an application and
- each press will trig some different action. It's a bore
- to create one new class for every button, and if you create
- a reusable Button-derived-class you have the same problem:
- How to distribute the pushdownevent to the right handler. You
- can't put all handlers inside the button can you?
-
- What I really would like is:
-
- To have the possibility to make calls to a memberfunction of
- an object without having to know that objects particular class.
- A sort of blindcall. But this feature could be made safe
- enough. (That is: impossible to fail such a call as long as one
- doesn't use explicit typecasts and the object still exists). It
- would probably be a first-order object/wrapper: pointer-to-member-
- -of-specifik-type.
- Such a wrapper can be used as an ordinary functionpointer when
- calling but it can't be possible to (implicitely) cast it to one.
- Though the reversed casting should be possible. A function
- which takes a wrapper as an argument should also be able to
- take an ordinary functionpointer as long as the parameter-
- profile is the same. The resulting wrapper could be thought
- of as a membercall to the global environment.
- Care must be taken when handling such wrappers since they
- just like pointers could be obsolete.
-
- All this was thought of when constructing C++ but of some strange
- reason it wasn't included.
-
- Anybody agree?
-
- --------------------------------------------------------------
- Jonas Svanberg
- Department of Electrical Engineering
- Link÷ping University
- S-581 83 Link÷ping, Sweden Email: svan@isy.liu.se
- --------------------------------------------------------------
-
-