🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Rich Hickey Sat, 02 Mar 1996 16:56
Re: Generic Object Callbacks
#3569
Author: Rich Hickey
Date: Sat, 02 Mar 1996 16:56
36 lines
2401 bytes
In message <4h5j7b$1ur@bcarh8ab.bnr.ca> - "ian (i.) willmott" 
<willmott@bnr.ca> writes:

:>The original article (this has expired on my site and possibly others
:>as well; maybe I should repost it) suggests a new type
:>"pointer-to-bound-member-function" which would encapsulate an object
:>pointer and a pointer to a member function of that class.

Having previously considered the suggestion you proposed and forced to solve 
the problem in current language, and with a lot of experience using a
current-language solution, I feel it is actually preferable to the extension 
you propose in the following areas:

A)It is more type-flexible. By that I mean it is possible to produce a 
callback system that is type-safe, yet tolerant of type differences in the 
caller/callee signatures that are either implicitly convertible or can be 
ignored. For example if the caller requires a function to which it can pass 
an int and from which it expects nothing, using my library it is possible to 
bind the callback to a function that takes an int and returns an int (return 
value is ignored), or takes a long and returns void (compiler implicitly 
converts int->long). Once you get involved with polymorphism this becomes 
critical: caller passes ptr-to-derived, callee accepts ptr-to-base. Any 
language extension approach would have the rigidity normally associated with 
pointers-to-functions, i.e. exactly matching signatures required. 

B)It is easier to extend; to things like callbacks with stored arguments 
etc.

Rich
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Re: Generic Object Callbacks
#3581
Author: ian (i.) willmott
Date: Mon, 04 Mar 1996 20:32
88 lines
4270 bytes
References: <4h9ii0$3mlq@news-s01.ny.us.ibm.net>

hickeyr@ibm.net (Rich Hickey) wrote:

>Having previously considered the suggestion you proposed and forced to solve 
>the problem in current language, and with a lot of experience using a
>current-language solution, I feel it is actually preferable to the extension 
>you propose in the following areas:
>
>A)It is more type-flexible. By that I mean it is possible to produce a 
>callback system that is type-safe, yet tolerant of type differences in the 
>caller/callee signatures that are either implicitly convertible or can be 
>ignored. For example if the caller requires a function to which it can pass 
>an int and from which it expects nothing, using my library it is possible to 
>bind the callback to a function that takes an int and returns an int (return 
>value is ignored), or takes a long and returns void (compiler implicitly 
>converts int->long). Once you get involved with polymorphism this becomes 
>critical: caller passes ptr-to-derived, callee accepts ptr-to-base. Any 
>language extension approach would have the rigidity normally associated with 
>pointers-to-functions, i.e. exactly matching signatures required. 

I'm not sure what you mean by this. I've never heard of any requirement
for "exactly matching signatures" for calls through pointers-to-function.
As far as I know the matching rules are exactly the same as for ordinary
function calls. The following code is accepted by Cfront v3.0
without complaint:

class A {
    int x;
};

class B: public A {
    int y;
};

int (*fp)(long,A *);

long func(int i,B *p)
{
    return(fp(i,p));
}

The proposed language extension would work the same way. It's true
that this is illegal (I assume this is what you were referring to):

fp=&func;  // never mind the infinite recursion here

However, I don't see why this is a problem. You just declare your
callback functions to take the most general parameters; in this
example, (long,A *) instead of (int,B *). The proposed type
"pointer-to-bound-member-function" is the natural analog for class
member functions of the existing pointer-to-function type, and will
be useful in the same contexts.

>B)It is easier to extend; to things like callbacks with stored arguments 
>etc.

How so? Here's the PBMF solution:

class MyCallback {
    int (class::*pbmf)(int,A *);  // language extension
    int i;
    A *p;
public:
    MyCallback((class::*pb)(int,A *),int ii,A *pp):
        pbmf(pb),i(ii),p(pp) {}
    int operator()
        { return(pbmf(i,p)); }
};

No dynamic allocation, virtual functions, casts to void *, or templates
(although you could use them if you wanted to) needed. Of course, if this
extension were adopted, this would not prevent other solutions from being
used anywhere they were appropriate. For most cases, the built-in datatype
is more convenient and type-safe.

Ian Willmott
Bell-Northern Research
Ottawa, Ontario
(613)-763-9688
willmott@bnr.ca
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads