home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!lax.pe-nelson.com!lax!twbrown
- From: twbrown@PE-Nelson.COM (Tom W. Brown)
- Newsgroups: comp.lang.c++
- Subject: Re: Callbacks - C++ needs an extension?
- Message-ID: <599@lax.lax.pe-nelson.com>
- Date: 1 Sep 92 20:21:24 GMT
- References: <1992Aug28.165108.17479@isy.liu.se> <DAVIDM.92Aug31105639@consilium.com>
- Sender: news@lax.pe-nelson.com
- Followup-To: comp.lang.c++
- Distribution: comp
- Organization: PE-Nelson
- Lines: 61
-
- In article <DAVIDM.92Aug31105639@consilium.com>, davidm@consilium.com (David S. Masterson) writes:
- |> >>>>> On 29 Aug 92 19:26:35 GMT, checker@acf5.NYU.EDU (checker) said:
- |>
- |> > There is a simple way of doing blind callbacks in a typesafe manner using an
- |> > intermediary class and a pure virtual function:
- |>
-
- [ example omitted ]
-
- |> 3. Given a windowing system based on C, its likely that the information
- |> coming from the windowing system will not be C++ object oriented (ie. it won't
- |> follow a C++ inheritance model). How do you convert?
-
- Most windowing systems provide for some type of closure to be supplied. In
- the specific case of Xt programming, one provides an untyped pointer along
- with the address of a normal C callback function. This can be relatively
- type-safe if done as follows:
-
- void button_callback(Widget, button_class*, XtPointer);
- .
- .
- .
- button_class* a_button = new button_class("OK"); // hypothetical UI object
-
- XtAddCallback(button_widget, // This would usually
- XmNactivate, // be done in the
- (XtCallbackProc) button_callback, // constructor for the
- (XtPointer) a_button); // button_class
- .
- .
- .
-
- void button_callback(Widget, button_class* button, XtPointer)
- {
- button->push_function(); // dispatch to our C++ hierarchy
- }
-
- .
- .
- .
-
- So, a single, global, non-member function receives all callbacks for all
- button objects, but since the object pointer was tied to each callback as
- closure for the callback, this one function is able to dispatch to the
- appropriate object member function. For MS Windows one could stuff the
- "this" pointer in the extra bytes that can be made available on a window
- basis.
-
- I don't believe there's a way to avoid this kind of indirection when the
- underlying system making the callback knows nothing about C++, a "this"
- pointer, or the difference between member and non-member functions.
-
- Note, there was a posting in comp.windows.x (and possibly this group also)
- regarding how to fit C++ in with Xt/Motif programming.
-
-
- ----------------------------------------------------------------------------
- Tom Brown | "Strange women, lying in ponds, distributing
- PE Nelson Systems | swords is no basis for a system of government."
- twbrown@pe-nelson.com | Monty Python and the Holy Grail
- ----------------------------------------------------------------------------
-