Thread overview | |||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 03, 2012 virtual method pointer | ||||
---|---|---|---|---|
| ||||
I need to get a pointer to a virtual method, which is in turn a
function pointer, being set by virtual method binding.
Can anyone, please, tell me how to get it? Taking the delegate of the
method won't do, because I need it to behave exactly as a virtual
method call, except I pass the "this" explicitly.
I need this in an event handling mechanism I'm making. You derive from
the Sink class, passing your static type to the constructor, which
scans your virtual methods, that conform to specific requirements and
extracts them into an array, which later uses to dispatch the incoming
events.
It will feel much like a run-time virtual template method.
--
Bye,
Gor Gyolchanyan.
|
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On 03-05-2012 20:46, Gor Gyolchanyan wrote: > I need to get a pointer to a virtual method, which is in turn a > function pointer, being set by virtual method binding. > Can anyone, please, tell me how to get it? Taking the delegate of the > method won't do, because I need it to behave exactly as a virtual > method call, except I pass the "this" explicitly. > I need this in an event handling mechanism I'm making. You derive from > the Sink class, passing your static type to the constructor, which > scans your virtual methods, that conform to specific requirements and > extracts them into an array, which later uses to dispatch the incoming > events. > It will feel much like a run-time virtual template method. > import std.stdio; class A { void foo() { writeln("foo called"); } } void main() { auto a = new A(); auto fn = &a.foo; auto ptr = fn.funcptr; (cast(void function(A))ptr)(a); } Prints "foo called". -- - Alex |
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Rønne Petersen | class B: A { void foo() { writeln("B.foo called"); } } void main() { auto a = new A(); auto fn = &a.foo; auto ptr = fn.funcptr; auto b = new B(); (cast(void function(A))ptr)(b); } will this work? On Thu, May 3, 2012 at 10:54 PM, Alex Rønne Petersen <xtzgzorex@gmail.com> wrote: > On 03-05-2012 20:46, Gor Gyolchanyan wrote: >> >> I need to get a pointer to a virtual method, which is in turn a >> function pointer, being set by virtual method binding. >> Can anyone, please, tell me how to get it? Taking the delegate of the >> method won't do, because I need it to behave exactly as a virtual >> method call, except I pass the "this" explicitly. >> I need this in an event handling mechanism I'm making. You derive from >> the Sink class, passing your static type to the constructor, which >> scans your virtual methods, that conform to specific requirements and >> extracts them into an array, which later uses to dispatch the incoming >> events. >> It will feel much like a run-time virtual template method. >> > > import std.stdio; > > class A > { > void foo() > { > writeln("foo called"); > } > } > > void main() > { > auto a = new A(); > auto fn = &a.foo; > auto ptr = fn.funcptr; > (cast(void function(A))ptr)(a); > } > > Prints "foo called". > > -- > - Alex -- Bye, Gor Gyolchanyan. |
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote: > I need to get a pointer to a virtual method, which is in turn a > function pointer, being set by virtual method binding. > Can anyone, please, tell me how to get it? Taking the delegate of the > method won't do, because I need it to behave exactly as a virtual > method call, except I pass the "this" explicitly. > I need this in an event handling mechanism I'm making. You derive from > the Sink class, passing your static type to the constructor, which > scans your virtual methods, that conform to specific requirements and > extracts them into an array, which later uses to dispatch the incoming > events. > It will feel much like a run-time virtual template method. Are you asking for the same thing I was asking for? http://forum.dlang.org/thread/jn9ops$13bs$1@digitalmars.com |
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On 05/03/2012 09:01 PM, Gor Gyolchanyan wrote:
> class B: A
> {
> void foo()
> {
> writeln("B.foo called");
> }
> }
>
> void main()
> {
> auto a = new A();
> auto fn =&a.foo;
> auto ptr = fn.funcptr;
> auto b = new B();
> (cast(void function(A))ptr)(b);
> }
>
> will this work?
>
It should work (at least) with the D calling convention, yes.
A somewhat more portable way would probably be:
auto fn = &a.foo;
fn.ptr = cast(void*)b;
fn();
|
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | No, because I'm not supposed to get delegates in the first place. What I want is to have a pointer to a pointer to a function, so I can make a true virtual call. On Thu, May 3, 2012 at 11:09 PM, Mehrdad <wfunction@hotmail.com> wrote: > On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote: >> >> I need to get a pointer to a virtual method, which is in turn a >> function pointer, being set by virtual method binding. >> Can anyone, please, tell me how to get it? Taking the delegate of the >> method won't do, because I need it to behave exactly as a virtual >> method call, except I pass the "this" explicitly. >> I need this in an event handling mechanism I'm making. You derive from >> the Sink class, passing your static type to the constructor, which >> scans your virtual methods, that conform to specific requirements and >> extracts them into an array, which later uses to dispatch the incoming >> events. >> It will feel much like a run-time virtual template method. > > > Are you asking for the same thing I was asking for? http://forum.dlang.org/thread/jn9ops$13bs$1@digitalmars.com -- Bye, Gor Gyolchanyan. |
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | So, you're saying, that the foo function actually takes the *this, which we ass manually, extracts the real foo method and calls it? AFAIK, that shouldn't be the case. The delegate extracts the A's foo and call to the delegate should be a direct call to A.foo, not a virtual call. On Thu, May 3, 2012 at 11:11 PM, Timon Gehr <timon.gehr@gmx.ch> wrote: > On 05/03/2012 09:01 PM, Gor Gyolchanyan wrote: >> >> class B: A >> { >> void foo() >> { >> writeln("B.foo called"); >> >> } >> } >> >> void main() >> { >> auto a = new A(); >> auto fn =&a.foo; >> auto ptr = fn.funcptr; >> auto b = new B(); >> (cast(void function(A))ptr)(b); >> } >> >> will this work? >> > > It should work (at least) with the D calling convention, yes. > > A somewhat more portable way would probably be: > > auto fn = &a.foo; > fn.ptr = cast(void*)b; > fn(); -- Bye, Gor Gyolchanyan. |
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On 05/03/2012 09:18 PM, Gor Gyolchanyan wrote:
> So, you're saying, that the foo function actually takes the *this,
> which we ass manually, extracts the real foo method and calls it?
> AFAIK, that shouldn't be the case. The delegate extracts the A's foo
> and call to the delegate should be a direct call to A.foo, not a
> virtual call.
>
I just gave an alternative to Alex' solution, but it seems both do not actually do what you are after.
Is this good enough?
auto fn = function(A a){return a.foo();}
fn(a);
fn(b);
|
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On Thu, 03 May 2012 14:46:58 -0400, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> wrote: > I need to get a pointer to a virtual method, which is in turn a > function pointer, being set by virtual method binding. Not exactly. There is a workaround: http://www.drdobbs.com/blogs/cpp/231600610 -Steve |
May 03, 2012 Re: virtual method pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | That workaround is pretty obvious, but I can't afford to make an extra call every time. This event system is supposed to be ultra-fast. Isn't there a way to get to the vtable etry itself? On Thu, May 3, 2012 at 11:26 PM, Steven Schveighoffer <schveiguy@yahoo.com> wrote: > On Thu, 03 May 2012 14:46:58 -0400, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> wrote: > >> I need to get a pointer to a virtual method, which is in turn a function pointer, being set by virtual method binding. > > > Not exactly. There is a workaround: > > http://www.drdobbs.com/blogs/cpp/231600610 > > -Steve -- Bye, Gor Gyolchanyan. |
Copyright © 1999-2021 by the D Language Foundation