home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13159 < prev    next >
Encoding:
Internet Message Format  |  1992-09-01  |  2.9 KB

  1. Path: sparky!uunet!lax.pe-nelson.com!lax!twbrown
  2. From: twbrown@PE-Nelson.COM (Tom W. Brown)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Callbacks - C++ needs an extension?
  5. Message-ID: <599@lax.lax.pe-nelson.com>
  6. Date: 1 Sep 92 20:21:24 GMT
  7. References: <1992Aug28.165108.17479@isy.liu.se> <DAVIDM.92Aug31105639@consilium.com>
  8. Sender: news@lax.pe-nelson.com
  9. Followup-To: comp.lang.c++
  10. Distribution: comp
  11. Organization: PE-Nelson
  12. Lines: 61
  13.  
  14. In article <DAVIDM.92Aug31105639@consilium.com>, davidm@consilium.com (David S. Masterson) writes:
  15. |> >>>>> On 29 Aug 92 19:26:35 GMT, checker@acf5.NYU.EDU (checker) said:
  16. |> 
  17. |> > There is a simple way of doing blind callbacks in a typesafe manner using an
  18. |> > intermediary class and a pure virtual function:
  19. |> 
  20.  
  21. [ example omitted ]
  22.  
  23. |> 3.  Given a windowing system based on C, its likely that the information
  24. |> coming from the windowing system will not be C++ object oriented (ie. it won't
  25. |> follow a C++ inheritance model).  How do you convert?
  26.  
  27. Most windowing systems provide for some type of closure to be supplied.  In
  28. the specific case of Xt programming, one provides an untyped pointer along
  29. with the address of a normal C callback function.  This can be relatively
  30. type-safe if done as follows:
  31.  
  32.    void button_callback(Widget, button_class*, XtPointer);
  33.    .
  34.    .
  35.    .
  36.    button_class* a_button = new button_class("OK");  // hypothetical UI object
  37.  
  38.    XtAddCallback(button_widget,                      // This would usually
  39.                  XmNactivate,                        // be done in the
  40.                 (XtCallbackProc) button_callback,    // constructor for the
  41.                 (XtPointer) a_button);               // button_class
  42.    .
  43.    .
  44.    .
  45.  
  46.    void button_callback(Widget, button_class* button, XtPointer)
  47.    {
  48.       button->push_function();  // dispatch to our C++ hierarchy
  49.    }
  50.  
  51.    .
  52.    .
  53.    .
  54.  
  55. So, a single, global, non-member function receives all callbacks for all
  56. button objects, but since the object pointer was tied to each callback as
  57. closure for the callback, this one function is able to dispatch to the
  58. appropriate object member function.  For MS Windows one could stuff the
  59. "this" pointer in the extra bytes that can be made available on a window
  60. basis.
  61.  
  62. I don't believe there's a way to avoid this kind of indirection when the
  63. underlying system making the callback knows nothing about C++, a "this"
  64. pointer, or the difference between member and non-member functions.
  65.  
  66. Note, there was a posting in comp.windows.x (and possibly this group also)
  67. regarding how to fit C++ in with Xt/Motif programming.
  68.  
  69.  
  70. ----------------------------------------------------------------------------
  71. Tom Brown               |  "Strange women, lying in ponds, distributing
  72. PE Nelson Systems       |   swords is no basis for a system of government."
  73. twbrown@pe-nelson.com   |                    Monty Python and the Holy Grail
  74. ----------------------------------------------------------------------------
  75.