use std::ffi::c_void;
use std::os::raw::c_char;
use std::pin::Pin;
use std::ptr::null;
use urid::*;
pub struct HostMap<M: Map + Unmap + Unpin> {
internal_map: M,
}
impl<M: Map + Unmap + Unpin> From<M> for HostMap<M> {
fn from(map: M) -> Self {
HostMap { internal_map: map }
}
}
impl<M: Map + Unmap + Unpin> HostMap<M> {
pub unsafe extern "C" fn extern_map(
handle: crate::sys::LV2_URID_Map_Handle,
uri: *const c_char,
) -> crate::sys::LV2_URID {
match (*(handle as *const Self))
.internal_map
.map_uri(Uri::from_ptr(uri))
{
Some(urid) => urid.get(),
_ => 0,
}
}
pub fn make_map_interface(self: Pin<&mut Self>) -> sys::LV2_URID_Map {
sys::LV2_URID_Map {
handle: self.get_mut() as *mut Self as *mut c_void,
map: Some(Self::extern_map),
}
}
pub unsafe extern "C" fn extern_unmap(
handle: crate::sys::LV2_URID_Map_Handle,
urid: crate::sys::LV2_URID,
) -> *const c_char {
match URID::new(urid) {
Some(urid) => match (*(handle as *const Self)).internal_map.unmap(urid) {
Some(uri) => uri.as_ptr(),
None => null(),
},
None => null(),
}
}
pub fn make_unmap_interface(self: Pin<&mut Self>) -> sys::LV2_URID_Unmap {
sys::LV2_URID_Unmap {
handle: self.get_mut() as *mut Self as *mut c_void,
unmap: Some(Self::extern_unmap),
}
}
}