Topic: Help in setting up callback function...


Author: pchan@ubitrex.mb.ca (Pat Chan)
Date: 2 Mar 92 17:21:48 GMT
Raw View
I've a question on C++ and callback function.

I've set up a callback function on XVIEW such
that the callback function is a member function
of an object.

For instance,

 Object myObject;

 // set up XVIEW callback...
 xv_set(...., PANEL_NOTIFY_PROC, myObject.function);


Of course, the above mechanism fails.

The question is: in general, how to setup a
callback function such that the callback function
is a member function of an object.

All suggestions are welcome. Thanx in advance..

Pat Chan
pchan@ubitrex.mb.ca





Author: rickg@eng.sun.com (Richard Goldstein)
Date: 2 Mar 92 20:06:20 GMT
Raw View
In article <1992Mar2.172148.8975@ccu.umanitoba.ca> pchan@ubitrex.mb.ca (Pat Chan) writes:

   From: pchan@ubitrex.mb.ca (Pat Chan)
   Newsgroups: comp.std.c++,comp.windows.open-look,comp.windows.x
   Date: 2 Mar 92 17:21:48 GMT
   Sender: news@ccu.umanitoba.ca
   Organization: Ubitrex Corporation, Winnipeg, Manitoba, Canada
   Lines: 26
   Originator: pchan@ubitrex.mb.ca


   I've a question on C++ and callback function.

   I've set up a callback function on XVIEW such
   that the callback function is a member function
   of an object.

   For instance,

    Object myObject;

    // set up XVIEW callback...
    xv_set(...., PANEL_NOTIFY_PROC, myObject.function);


   Of course, the above mechanism fails.


how is it failing?  if the C++ complier is griping, Mark is probably
right.  if it's failing from a core dump, it's probably because
you're calling xv_set() with a non-NULL terminated list.


<<rick




Author: warsaw@nlm.nih.gov (Barry A. Warsaw)
Date: 2 Mar 92 21:01:13 GMT
Raw View
>>>>> "Rick" == Richard Goldstein <rickg@eng.sun.com> writes:

    Rick> how is it failing?  if the C++ complier is griping, Mark
    Rick> is probably right.  if it's failing from a core dump,
    Rick> it's probably because you're calling xv_set() with a
    Rick> non-NULL terminated list.

even if the list is null-terminated correctly, i believe using a
(non-static) member function as a notify-proc (or any callback) will
most likely result in a core dump.

-barry




Author: zoo@aps1.spa.umn.edu (david d 'zoo' zuhn)
Date: 2 Mar 92 22:07:29 GMT
Raw View
>    I've a question on C++ and callback function.

>    I've set up a callback function on XVIEW such
>    that the callback function is a member function
>    of an object.

>    For instance,
>     Object myObject;

>     // set up XVIEW callback...
>     xv_set(...., PANEL_NOTIFY_PROC, myObject.function);


>    Of course, the above mechanism fails.


>> how is it failing?  if the C++ complier is griping, Mark is probably
>> right.  if it's failing from a core dump, it's probably because
>> you're calling xv_set() with a non-NULL terminated list.

The problem is that unless 'function' is a static member function of
the Object class, it's not call compatible with C code, which is what
XView expects to be able to call.

The best thing to do in such situations (imho) is to write a "redraw"
function which gets a pointer to the object and calls its' redraw
member:

   static int redraw (/* whatever params it takes */)
   {
     Object *object;
     object = xv_get (xview_object, XV_KEY_DATA, THIS_POINTER);
     return object->redraw(/* params */);
   }

and then when you create the object,

   xv_set(...., PANEL_REDRAW_PROC, redraw,
                XV_KEY_DATA, THIS_POINTER, &object,
         NULL);


This has worked well for me in the past when I've needed to use Xview
with C++.  Kinda kludgy but it works.

david d [zoo] zuhn  Univ. of Minnesota Dept. of Astronomy
zoo@aps1.spa.umn.edu        Automated Plate Scanner Project
 -- member of the League for Programming Freedom -- email me for info




Author: nazgul@alphalpha.com (Kee Hinckley)
Date: Tue, 3 Mar 92 14:55:37 GMT
Raw View
In article <1992Mar2.172148.8975@ccu.umanitoba.ca> pchan@ubitrex.mb.ca (Pat Chan) writes:
>The question is: in general, how to setup a
>callback function such that the callback function
>is a member function of an object.

Make it a static member function, put the "this" pointer in the
callback data, or under a rock somewhere.  If you want to use
virtual functions and the like then have the static function
call the "real" one and be real careful with your casts.

--
Alfalfa Software, Inc.          |       Poste:  The EMail for Unix
nazgul@alfalfa.com              |       Send Anything... Anywhere
617/497-2922                    |       info@alfalfa.com

I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.