Skip to content

Instantly share code, notes, and snippets.

@zeux
Created June 28, 2013 05:19
Show Gist options
  • Save zeux/5882630 to your computer and use it in GitHub Desktop.
Save zeux/5882630 to your computer and use it in GitHub Desktop.
#include <stdio.h>
template <typename R, typename F> R callf(void* buffer, F func)
{
return func();
}
template <typename R, typename F, typename ArgHead, typename ...ArgTail> R callf(void* buffer, F func)
{
ArgHead* head = static_cast<ArgHead*>(buffer);
auto next = [=](ArgTail... args) { return func(*head, args...); };
return callf<R, decltype(next), ArgTail...>(head + 1, next);
}
template <typename R, typename T, typename ...Args> R call(void* buffer, T* obj, R (T::*method)(Args...))
{
auto run = [=](Args... args) { return (obj->*method)(args...); };
return callf<R, decltype(run), Args...>(buffer, run);
}
struct Foo
{
int bar(int a, float b, char c)
{
printf("Foo::bar %d %f %c\n", a, b, c);
return 42;
}
};
int main()
{
char buf[12] = {15, 1, 0, 0, 0, 0, 0x80, 0x3f, 'x'};
Foo foo;
int res = call(buf, &foo, &Foo::bar);
printf("=> %d\n", res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment