pub mod cppbridge;
pub use cppbridge::*;
pub use std::pin::Pin;
pub use std::sync::Arc;
pub use std::cell::RefCell;
pub use std::borrow::BorrowMut;
pub trait TBridgeGlobal {
fn void() -> Self;
}
impl TBridgeGlobal for bridge_global {
fn void() -> Self {
bridge_global{num: 0, buf: [0; 8]}
}
}
#[macro_export]
macro_rules! any_pinned_init_slots {
($n: expr) => {
unsafe {
let r = bridge_global_init_slots($n);
if r == 0 {
panic!("not allocated area: bridge_global_init_slots");
}
r
}
}
}
pub use any_pinned_init_slots;
#[macro_export]
macro_rules! any_pinned_dispose_slots {
() => {
unsafe {
let r = bridge_global_dispose_slots();
if r == 0 {
panic!("something wrong to free: bridge_global_dispose_slots");
}
r
}
}
}
pub use any_pinned_dispose_slots;
#[macro_export]
macro_rules! any_pinned_set_bg_mut {
($i: ty, $n: expr) => {
let mut abg = Arc::new(RefCell::new(<$i>::void()));
let mut pbg = Pin::new(&mut abg); pbg.set_bg_mut($n);
}
}
pub use any_pinned_set_bg_mut;
#[macro_export]
macro_rules! any_pinned_with_bg_mut {
($i: ty, $n: expr, $f: expr) => {
Pin::<&mut Arc<RefCell<$i>>>::with_bg_mut($n, $f)
}
}
pub use any_pinned_with_bg_mut;
pub trait TGlobalSetGet<T> {
fn set_bg_mut(&mut self, n: usize);
fn with_bg_mut<F>(n: usize, f: F) where F: FnMut(&mut T) -> ();
}
impl<T> TGlobalSetGet<T> for Pin<&mut Arc<RefCell<T>>> {
fn set_bg_mut(&mut self, n: usize) {
unsafe {
match bridge_global_setter(n, self.borrow_mut().as_ptr()
as *mut T as *mut bridge_global) {
0 => panic!("not allocated area: bridge_global_setter"),
_ => ()
}
}
}
fn with_bg_mut<F>(n: usize, mut f: F) where F: FnMut(&mut T) -> () {
f(unsafe {
let p = bridge_global_getter(n);
if p == 0 as *mut bridge_global {
panic!("not allocated area: bridge_global_getter");
}
&mut std::slice::from_raw_parts_mut(p as *mut T, 1)[0] })
}
}