use crate::{sys, SBBroadcaster, SBStream};
use std::ffi::CStr;
use std::fmt;
pub struct SBEvent {
pub raw: sys::SBEventRef,
}
impl SBEvent {
pub(crate) fn wrap(raw: sys::SBEventRef) -> SBEvent {
SBEvent { raw }
}
#[allow(dead_code)]
pub(crate) fn maybe_wrap(raw: sys::SBEventRef) -> Option<SBEvent> {
if unsafe { sys::SBEventIsValid(raw) } {
Some(SBEvent { raw })
} else {
None
}
}
#[allow(missing_docs)]
pub fn new() -> SBEvent {
Self::wrap(unsafe { sys::CreateSBEvent() })
}
pub fn is_valid(&self) -> bool {
unsafe { sys::SBEventIsValid(self.raw) }
}
#[allow(missing_docs)]
pub fn data_flavor(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBEventGetDataFlavor(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}
#[allow(missing_docs)]
pub fn event_type(&self) -> u32 {
unsafe { sys::SBEventGetType(self.raw) }
}
#[allow(missing_docs)]
pub fn broadcaster(&self) -> SBBroadcaster {
SBBroadcaster::wrap(unsafe { sys::SBEventGetBroadcaster(self.raw) })
}
#[allow(missing_docs)]
pub fn broadcaster_class(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBEventGetBroadcasterClass(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}
#[allow(missing_docs)]
pub fn broadcaster_matches_ref(&self, broadcaster: &SBBroadcaster) -> bool {
unsafe { sys::SBEventBroadcasterMatchesRef(self.raw, broadcaster.raw) }
}
}
impl Clone for SBEvent {
fn clone(&self) -> SBEvent {
SBEvent {
raw: unsafe { sys::CloneSBEvent(self.raw) },
}
}
}
impl fmt::Debug for SBEvent {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let stream = SBStream::new();
unsafe { sys::SBEventGetDescription(self.raw, stream.raw) };
write!(fmt, "SBEvent {{ {} }}", stream.data())
}
}
impl Default for SBEvent {
fn default() -> Self {
Self::new()
}
}
impl Drop for SBEvent {
fn drop(&mut self) {
unsafe { sys::DisposeSBEvent(self.raw) };
}
}
unsafe impl Send for SBEvent {}
unsafe impl Sync for SBEvent {}