virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
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" explic

Re: virtual method pointer

2012-05-03 Thread Alex Rønne Petersen
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 virtua

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
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 wrote: > On

Re: virtual method pointer

2012-05-03 Thread Mehrdad
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 behav

Re: virtual method pointer

2012-05-03 Thread Timon Gehr
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 sho

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
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 wrote: > On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote: >> >> I need to get a

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
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, 20

Re: virtual method pointer

2012-05-03 Thread Timon Gehr
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

Re: virtual method pointer

2012-05-03 Thread Steven Schveighoffer
On Thu, 03 May 2012 14:46:58 -0400, 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. Not exactly. There is a workaround: http://www.drdobbs.com/blogs/cpp/231600610 -Steve

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
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 wrote: > On Thu, 03 May 2012 14:46:58 -0400, Gor Gyolch

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
No, I intend to call these methods very frequently and I can't afford any performance loss. What I expect to have is something like a virtual table entry. I tied looking at the virtual tables and searching for the method delegate's .funcptr in the vtable, but didn't find it. Having that vtable en

Re: virtual method pointer

2012-05-03 Thread deadalnix
Le 03/05/2012 22:22, Gor Gyolchanyan a écrit : 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? Well : 1/ Such a trivial thing is surely inlined by any comp

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
Good point. That raises the question: How should I make the fastest possible dynamic dispatch of this kind? On Fri, May 4, 2012 at 12:57 AM, deadalnix wrote: > Le 03/05/2012 22:22, Gor Gyolchanyan a écrit : > >> That workaround is pretty obvious, but I can't afford to make an extra >> call every

Re: virtual method pointer

2012-05-03 Thread Steven Schveighoffer
On Thu, 03 May 2012 16:22:56 -0400, Gor Gyolchanyan wrote: 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? Well, you can use the ABI to determine the _

Re: virtual method pointer

2012-05-03 Thread Steven Schveighoffer
On Thu, 03 May 2012 16:57:01 -0400, deadalnix wrote: Le 03/05/2012 22:22, Gor Gyolchanyan a écrit : 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? 1/ S

Re: virtual method pointer

2012-05-03 Thread Gor Gyolchanyan
Basically I want to make a dynamic typing subsystem in D. I want to take a given object of a static type, construct a dynamic interface for it and later use that interface to event handling. To construct a dynamic interface, I need to extract the virtual methods without resolving them prematurely.

Re: virtual method pointer

2012-05-03 Thread Mehrdad
class Foo { void test() { } } void main(string[] args) { auto f = new Foo(); stderr.writeln(f.__vptr[6]); auto del = (&f.test); stderr.writeln(del.funcptr); }

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
Cool! Thanks! So all I need to do is remember the indices of functions in the vtable and extract them manually every time like this, right? On Fri, May 4, 2012 at 5:31 AM, Mehrdad wrote: > class Foo > { >        void test() { } > } > > void main(string[] args) > { >        auto f = new Foo(); >  

Re: virtual method pointer

2012-05-04 Thread Jacob Carlborg
On 2012-05-03 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 virtua

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
Thanks, I'll look into it. On Fri, May 4, 2012 at 11:25 AM, Jacob Carlborg wrote: > On 2012-05-03 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

Re: virtual method pointer

2012-05-04 Thread jerro
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 behav

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
Does this have an overhead over calling virtual method directly? On Fri, May 4, 2012 at 1:30 PM, jerro 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 bi

Re: virtual method pointer

2012-05-04 Thread jerro
On Friday, 4 May 2012 at 09:51:51 UTC, Gor Gyolchanyan wrote: Does this have an overhead over calling virtual method directly? If you call the function directly, it probably gets inlined. If you call it through a function pointer, it does have some overhead over calling the virtual method direc

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
So, the only overhead in making a virtual call this way over calling the method directly is exactly 1 extra function call? On Fri, May 4, 2012 at 2:02 PM, jerro wrote: > On Friday, 4 May 2012 at 09:51:51 UTC, Gor Gyolchanyan wrote: >> >> Does this have an overhead over calling virtual method dire

Re: virtual method pointer

2012-05-04 Thread jerro
On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote: So, the only overhead in making a virtual call this way over calling the method directly is exactly 1 extra function call? I guess so. You are calling a function that does a virtual call and doesn't do anything else, so what other

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
Great! Thanks! After I'm done with this, I'll propose adding it to Phobos. A genuine dynamic dispatch mechanism would be very useful. On Fri, May 4, 2012 at 6:04 PM, jerro wrote: > On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote: >> >> So, the only overhead in making a virtual call t

Re: virtual method pointer

2012-05-04 Thread Mehrdad
Did you see my solution? I think it's what you're looking for... On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote: So, the only overhead in making a virtual call this way over calling the method directly is exactly 1 extra function call? On Fri, May 4, 2012 at 2:02 PM, jerro wrote

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
Yes! Your solution looks exactly like what I wanted. The reason why I considered additional alternatives is because your solutions looks very fast (YES!!!), but not very portable and safe, so after testing, if it turns out to be inconsistent, I'll have to use something else. On Fri, May 4, 2012 at

Re: virtual method pointer

2012-05-04 Thread Mehrdad
Ah okay. Yeah it's not 'safe' at all... but I think the '6' comes from the number of members that Foo has. If you figure out how many methods there are in the v-table, then that should get you the index (though don't quote me on this... you should look at the compiler source code if you want to

Re: virtual method pointer

2012-05-04 Thread Gor Gyolchanyan
I'm not going to cherry-pick methods in any case. I'll have all methods analyzed at compile time, by iterating over the overloads of each method and have them searched in the vtable to get their indices at launch time. It'll be easy to construct a dynamic virtual method call. On Fri, May 4, 2012 a