Skip to main content

forge_sys/
cpp.rs

1use core::ffi::c_void;
2
3/// Declares a type as having a C++ virtual function table.
4/// Structs that implement this trait should only ever exist as pointers/references
5/// from the game itself. They should never be instantiated directly.
6///
7/// This trait can be implemented with a `derive`
8/// ```ignore
9/// #[derive(forge::HasVtable)]
10/// pub struct MyStruct;
11/// ```
12pub trait HasVtable {
13    fn vtable_ptr(&self) -> *const *const c_void;
14
15    fn get_virtual_function(&self, index: usize) -> *const c_void {
16        unsafe { *self.vtable_ptr().add(index) }
17    }
18}