#[vtable]
Expand description
Defines a struct that will act as a VTable to a C++ class. It can also take data as to make sure the class functions as expected.
ยงExample
#define interface class __declspec(novtable)
interface MathEngine {
public:
virtual int add(int x, int y) = 0;
virtual int add2(int x, int y) = 0;
};
class MyEngine: public MathEngine {
public:
int mynum;
MyEngine(int b) {
mynum = b;
}
virtual int add(int x, int y) {
return x + y;
}
virtual int add2(int x, int y) {
return mynum + x + y;
}
};
extern "C" {
MyEngine* getMath(int b) {
return new MyEngine(b);
}
};
use std::os::raw::c_int;
use viable_impl::*;
extern "C" {
fn getMath(b: c_int) -> *mut MathIFace;
}
use viable_impl::vtable;
#[vtable]
struct MathIFace {
internal: i32,
#[offset(0)]
add: fn(a: c_int, b: c_int) -> c_int,
add2: fn(a: c_int, b: c_int) -> c_int,
}
pub fn main() {
let iface = unsafe { getMath(10) };
let iface = unsafe { iface.as_mut().unwrap() };
let value = iface.add2(5, 5);
assert_eq!(value, 10 + 5 + 5);
}