Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
Created April 16, 2012 23:09
Show Gist options
  • Save JakobOvrum/2402242 to your computer and use it in GitHub Desktop.
Save JakobOvrum/2402242 to your computer and use it in GitHub Desktop.
thiscall
import std.traits;
// Simplified version; does not work with a large number of possible types for F
private extern(C++) interface Thiscall(F)
{
ReturnType!F cppfunc(ParameterTypeTuple!F args);
}
auto thiscall(F, Args...)(void* _this, F func, Args args)
{
void*[2] vtable;
vtable[0] = _this;
vtable[1] = func;
auto i = cast(Thiscall!F)&vtable;
return i.cppfunc(args);
}
void main()
{
void* obj = null;
int function(int) member = null;
int a = thiscall(obj, member, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment