Last active
December 20, 2015 05:59
-
-
Save walac/6082365 to your computer and use it in GitHub Desktop.
A crazy libffi test.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ffi.h> | |
#include <iostream> | |
using namespace std; | |
class IInterface { | |
public: | |
virtual void fn1(int a, int b) = 0; | |
virtual void fn2(int a, int b) = 0; | |
~IInterface() {} | |
}; | |
class A : public IInterface | |
{ | |
public: | |
A(int c, int d) : c(c), d(d) {} | |
virtual void fn1(int a, int b); | |
virtual void fn2(int a, int b); | |
private: | |
int c,d; | |
}; | |
void A::fn1(int a, int b) | |
{ | |
cout << "fn1(" << a << ", " << b << ")" << endl; | |
cout << "c=" << c << " d=" << d << endl; | |
} | |
void A::fn2(int a, int b) | |
{ | |
cout << "fn2(" << a << ", " << b << ")" << endl; | |
cout << "c=" << c << " d=" << d << endl; | |
} | |
void call(void(**p)(void), IInterface *po) | |
{ | |
ffi_cif cif; | |
ffi_type *args[3]; | |
void *values[3]; | |
int rc; | |
int a=1, b=2; | |
/* Initialize the argument info vectors */ | |
args[0] = &ffi_type_pointer; | |
values[0] = &po; | |
args[1] = &ffi_type_sint32; | |
values[1] = &a; | |
args[2] = &ffi_type_sint32; | |
values[2] = &b; | |
/* Initialize the cif */ | |
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, | |
&ffi_type_void, args) == FFI_OK) | |
{ | |
ffi_call(&cif, p[0], NULL, values); | |
ffi_call(&cif, p[1], NULL, values); | |
} | |
} | |
int main(void) | |
{ | |
A o(3,4); | |
call(*reinterpret_cast<void(***)(void)>(dynamic_cast<IInterface*>(&o)), &o); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment