digitalmars.D - vtbl jamming
- BCS (2/2) Sep 09 2007 can we get some way to do this?
- Vladimir Panteleev (5/7) Sep 10 2007 Modifying VTBLs is effectively the same as using method pointers (as a s...
- Kirk McDonald (23/36) Sep 10 2007 It sort of does. You can fake them with delegates:
- Jarrett Billingsley (39/56) Sep 10 2007 Here's another way, which does it all at compile time. It also has the
- BCS (5/15) Sep 10 2007 The first use case that came to mind for me was a caching system. The fi...
- Lutger (13/18) Sep 10 2007 We can do it the same way in D, perhaps it can be done better. Or is
can we get some way to do this? http://www.artima.com/cppsource/backyard2.html
Sep 09 2007
On Mon, 10 Sep 2007 09:40:59 +0300, BCS <ao pathlink.com> wrote:can we get some way to do this? http://www.artima.com/cppsource/backyard2.htmlModifying VTBLs is effectively the same as using method pointers (as a static field). The advantage is when we don't have access to the base class. Of course, D doesn't have method pointers (like C++), so... -- Best regards, Vladimir mailto:thecybershadow gmail.com
Sep 10 2007
Vladimir Panteleev wrote:On Mon, 10 Sep 2007 09:40:59 +0300, BCS <ao pathlink.com> wrote:It sort of does. You can fake them with delegates: class Foo { void bar() { writefln("Foo.bar"); } } void ptr_call(void function() fn, Object o) { void delegate() dg; dg.ptr = o; dg.funcptr = fn; dg(); } void main() { auto f = new Foo; void function() fn = &Foo.bar; ptr_call(fn, f); } I write this without testing it, and may have left out a cast or two. But the idea is sound. (Indeed, Pyd relies on it.) -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.orgcan we get some way to do this? http://www.artima.com/cppsource/backyard2.htmlModifying VTBLs is effectively the same as using method pointers (as a static field). The advantage is when we don't have access to the base class. Of course, D doesn't have method pointers (like C++), so...
Sep 10 2007
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message
news:fc35na$30pq$1 digitalmars.com...
It sort of does. You can fake them with delegates:
class Foo {
void bar() { writefln("Foo.bar"); }
}
void ptr_call(void function() fn, Object o) {
void delegate() dg;
dg.ptr = o;
dg.funcptr = fn;
dg();
}
void main() {
auto f = new Foo;
void function() fn = &Foo.bar;
ptr_call(fn, f);
}
I write this without testing it, and may have left out a cast or two. But
the idea is sound. (Indeed, Pyd relies on it.)
Here's another way, which does it all at compile time. It also has the
advantage of doing a polymorphic call.
struct PTM(Type, char[] name)
{
const fullName = Type.stringof ~ "." ~ name;
alias ReturnTypeOf!(mixin(fullName)) Ret;
alias ParameterTupleOf!(mixin(fullName)) Params;
Ret opCall(Type obj, Params params)
{
mixin("return obj." ~ name ~ "(params);");
}
}
class A
{
void foo()
{
Stdout.formatln("Foo!");
}
int bar(int x)
{
Stdout.formatln("Bar: {}", x);
return x * 2;
}
}
void main()
{
scope a = new A();
PTM!(A, "foo") f;
PTM!(A, "bar") b;
f(a);
Stdout.formatln("Got: {}", b(a, 6));
}
It also has fairly similar semantics to C++'s pointers-to-methods: each PTM
is associated with a single class and with a single method. I'm sure you
could make it a little more flexible though, at the cost of having to
manually specify the parameter types when creating the PTM.
(Also note that the PTM struct is 1 byte.)
Sep 10 2007
Reply to Vladimir,On Mon, 10 Sep 2007 09:40:59 +0300, BCS <ao pathlink.com> wrote:The first use case that came to mind for me was a caching system. The first constructed object is a proxy with no data in it. The first time you ask for data, the data gets loaded and then the type gets switched so only a simple get is done next time. No ifs needed.can we get some way to do this? http://www.artima.com/cppsource/backyard2.htmlModifying VTBLs is effectively the same as using method pointers (as a static field). The advantage is when we don't have access to the base class. Of course, D doesn't have method pointers (like C++), so...
Sep 10 2007
BCS wrote:can we get some way to do this? http://www.artima.com/cppsource/backyard2.htmlWe can do it the same way in D, perhaps it can be done better. Or is that what you're asking? If so, ignore this then: At least the caveats mentioned in the article either do not exist (standard ABI) or can be checked at compile time. This seems to work in D as well: class Collection { void toSingle() { *(cast(void**)this) = *(cast(void**)SingleThreadedCollection.tmp); } }
Sep 10 2007









"Jarrett Billingsley" <kb3ctd2 yahoo.com> 