Unix Programming - Getting the instance pointer from a called function

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > Getting the instance pointer from a called function





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Getting the instance pointer from a called function
Elias Athanasopoulos

2004-03-23, 10:36 am


Hello!

Consider I have a Foo class, which ctor's takes as an argument a pointer
to function, i.e.:

Foo *foo = new Foo(bar);

Where:

int bar(int);

bar() will eventually get called somewhen inside Foo (it maybe called N
times).

Now, I want to grab the pointer of Foo's instance (i.e. the 'this'
pointer) which called bar(), from inside bar(), when that happens.

(I can't pass the pointer in bar() as an argument because Foo's
implementation is third party.)

I don't mind about portability. I would be happy if I find a way to do
this on Linux/ix86/gcc.

I tried using the GNU backtrace() extension with no luck. Any hints?

Regards,
Elias

John Smith

2004-03-24, 10:39 am

> Consider I have a Foo class, which ctor's takes as an argument a pointer
> to function, i.e.:
>
> Foo *foo = new Foo(bar);
>
> Where:
>
> int bar(int);
>
> bar() will eventually get called somewhen inside Foo (it maybe called N
> times).
>
> Now, I want to grab the pointer of Foo's instance (i.e. the 'this'
> pointer) which called bar(), from inside bar(), when that happens.
>
> (I can't pass the pointer in bar() as an argument because Foo's
> implementation is third party.)
>
> I don't mind about portability. I would be happy if I find a way to do
> this on Linux/ix86/gcc.
>
> I tried using the GNU backtrace() extension with no luck. Any hints?
>
> Regards,
> Elias
>


If I understood your problem correctly then you can solve it by making the
Foo instance a global variable.
E.g.

Foo *pFoo;

int bar(int i)
{
.... mess with pFoo

return i;
}

int main()
{
pFoo = new Foo(bar);
....
delete pFoo;
return 0;
}

Foo class will get bar function pointer and since the Foo instance pFoo is
global you can reach it from bar().

-- John


Elias Athanasopoulos

2004-03-24, 10:39 am



On Wed, 24 Mar 2004, John Smith wrote:

>
> If I understood your problem correctly then you can solve it by making the
> Foo instance a global variable.
> E.g.

[snipped]
> Foo class will get bar function pointer and since the Foo instance pFoo is
> global you can reach it from bar().


The problem is that I have multiple instances of Foo that will eventually
call bar(), and bar() must have a per-instance behaviour.

Also, Foo can't be modified by me. I have only access to the library wich
provides Foo.

Is there a way to constuct something like a heap monitor? I.e. to monitor
the heap and signal events when specific elements (addresses) are
accessed?

Regards,
Elias
Lorinczy Zsigmond / Domonyik Mariann

2004-03-25, 12:43 pm



Elias Athanasopoulos wrote:
>
> Hello!
>
> Consider I have a Foo class, which ctor's takes as an argument a pointer
> to function, i.e.:
>
> Foo *foo = new Foo(bar);
>
> Where:
>
> int bar(int);
>
> bar() will eventually get called somewhen inside Foo (it maybe called N
> times).
>
> Now, I want to grab the pointer of Foo's instance (i.e. the 'this'
> pointer) which called bar(), from inside bar(), when that happens.
>
> (I can't pass the pointer in bar() as an argument because Foo's
> implementation is third party.)
>
> I don't mind about portability. I would be happy if I find a way to do
> this on Linux/ix86/gcc.
>
> I tried using the GNU backtrace() extension with no luck. Any hints?


"Foo" is badly designed... contact the author to add an extra-parameter
to the callback, like this:

typedef int callback (int param, void *extrapar);

extern void Foo (callback cbfun, void *extrapar);

typedef struct {
...
} extra_data_for_callback;

int mycallback (int param, void *extrapar)
{
extra_data_for_callback edp = extrapar;
/* do anything with extrapar */
}

main ()
{
extra_data_for_callback ed;

Foo (my_callback, &ed);
}

Note: this is a well know standard technic
should no cause problem to extend Foo this way
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com