Getting the instance pointer from a called function
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Getting the instance pointer from a called function




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Getting the instance pointer from a called function  
Elias Athanasopoulos


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-23-04 03:36 PM


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






[ Post a follow-up to this message ]



    Re: Getting the instance pointer from a called function  
John Smith


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-24-04 03:39 PM

> 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







[ Post a follow-up to this message ]



    Re: Getting the instance pointer from a called function  
Elias Athanasopoulos


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-24-04 03:39 PM



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





[ Post a follow-up to this message ]



    Re: Getting the instance pointer from a called function  
Lorinczy Zsigmond / Domonyik Mariann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-25-04 05: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:08 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register