use crate::ffi::{ExternPointer, OwnedPtr}; use crate::static_data::{StaticStatisticSet, Statistic, StatisticSet}; use std::ptr::drop_in_place; macro_rules! statistic_set { ($num_type:ident) => { paste::paste!{ #[no_mangle] extern "C" fn []( hp: $num_type, attack: $num_type, defense: $num_type, special_attack: $num_type, special_defense: $num_type, speed: $num_type, ) -> OwnedPtr> { Box::into_raw(Box::new(StatisticSet::new( hp, attack, defense, special_attack, special_defense, speed, ))) } #[no_mangle] unsafe extern "C" fn [](ptr: OwnedPtr>) { drop_in_place(ptr) } #[no_mangle] extern "C" fn [](ptr: ExternPointer>, stat: Statistic) -> $num_type { ptr.as_ref().get_stat(stat) } #[no_mangle] extern "C" fn [](ptr: ExternPointer>, stat: Statistic, value: $num_type) { ptr.as_ref().set_stat(stat, value) } #[no_mangle] extern "C" fn [](ptr: ExternPointer>, stat: Statistic, value: $num_type) { ptr.as_ref().increase_stat(stat, value) } #[no_mangle] extern "C" fn [](ptr: ExternPointer>, stat: Statistic, value: $num_type) { ptr.as_ref().decrease_stat(stat, value) } } }; } statistic_set!(u8); statistic_set!(u16); statistic_set!(u32); statistic_set!(i8); statistic_set!(i16); statistic_set!(i32); macro_rules! static_statistic_set { ($num_type:ident) => { paste::paste!{ #[no_mangle] extern "C" fn []( hp: $num_type, attack: $num_type, defense: $num_type, special_attack: $num_type, special_defense: $num_type, speed: $num_type, ) -> OwnedPtr> { Box::into_raw(Box::new(StaticStatisticSet::new( hp, attack, defense, special_attack, special_defense, speed, ))) } #[no_mangle] unsafe extern "C" fn [](ptr: OwnedPtr>) { drop_in_place(ptr) } #[no_mangle] extern "C" fn [](ptr: ExternPointer>, stat: Statistic) -> $num_type { ptr.as_ref().get_stat(stat) } } }; } static_statistic_set!(u8); static_statistic_set!(u16); static_statistic_set!(u32); static_statistic_set!(i8); static_statistic_set!(i16); static_statistic_set!(i32);