Boost logo

Boost :

Subject: Re: [boost] Determining interest in Delegates submission
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2011-11-05 00:11:05


On Thu, Sep 8, 2011 at 11:48 AM, Jared McKee <jared.mckee_at_[hidden]> wrote:
> On Thu, Sep 8, 2011 at 11:29 AM, Jeffrey Lee Hellrung, Jr.
> <jeffrey.hellrung_at_[hidden]> wrote:
>> On Thu, Sep 8, 2011 at 7:46 AM, Jared McKee <jared.mckee_at_[hidden]> wrote:
>>
>>> On Thu, Sep 8, 2011 at 10:19 AM, Jeffrey Lee Hellrung, Jr.
>>> <jeffrey.hellrung_at_[hidden]> wrote:
>>> >
>>> > On Wed, Sep 7, 2011 at 7:48 PM, Jared McKee <jared.mckee_at_[hidden]>
>>> wrote:
>>> >
>>> > > I would like to determine interest in a possible library submission.
>>> > >
>>> > > I have written some code which converts a member function and object
>>> > > pointer
>>> > > combination to a free function for use in C style callbacks. It has
>>> some
>>> > > limitations which I will describe shortly.
>>> > >
>>> > > The basic idea is to be able to convert:
>>> > >
>>> > > R (B::*)(T0,T1,…) and B*
>>> > >
>>> > > to
>>> > >
>>> > > R (*)(T0,T1,…)
>>> > >
>>> > > Here's an example usage:
>>> > >
>>> > > class foo {
>>> > >       int x;
>>> > >       int test(int v) {return x + v;}
>>> > > };
>>> > >
>>> > > foo* a = new foo;
>>> > > a->x = 3;
>>> > > int (*f)(int) = delegate(a, &foo::inc);
>>> > > cout << f(4) << endl; // outputs "7"
>>> > >
>>> > [...snip implementation notes...]
>>> >
>>>
>>> Here's a discussion about the topic:
>>>
>>> http://stackoverflow.com/questions/1840029/passing-functor-as-function-pointer
>>>
>>
>> Oh...dear.
>>

>From stackoverflow there is a link to
http://fscked.org/proj/minihax/autocode/functorptr.cc, where the idea
is you have a "mold" (I'd call it a template, but...) for the function
signature you want to call like

static bool closure() {
    Functor *thisptr = (Functor *)MAX_INT;
    return (*thisptr)();
}

And then allocate some memory, copy the executable code from that
function mold (ie memcpy(newptr, closure, guess_at_size) and look
through that memory, find MAX_INT and replace it with the
runtime-obtained 'this' pointer.

When I was crazy enough to consider this in the past, I used
0xAD0BEA1D instead of MAX_INT. (ie Adobe Aid, as the photoshop plugin
callback spec uses function pointers without the ever-so-useful void *
extraData param. Disclaimer: this was NOT when I worked at Adobe, but
at a place where we were writing a paint package that supported PS
plugins). I would just worry about MAX_INT being too common of a
thing to find in bytes of executable. But maybe 0xAD0BEA1D is a valid
instruction sequence on some platform. Anyhow....

Now I can _imagine_ doing the same thing, but templatized to get any
function signature, but I can't say I'd recommend it. Not for a boost
library. Or outside a boost library, for that matter.

(I think in our case we eventually went with to thread-local storage,
as we just needed a function pointer per thread.)

Tony


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk