use cyclonedds_sys::{dds_error::DDSError, DdsDomainId, DdsEntity};
use std::convert::From;
use std::ffi::CString;
pub struct DdsDomain(DdsEntity);
impl DdsDomain {
pub fn create(domain: DdsDomainId, config: Option<&str>) -> Result<Self, DDSError> {
unsafe {
if let Some(cfg) = config {
let domain_name = CString::new(cfg).expect("Unable to create new config string");
let d = cyclonedds_sys::dds_create_domain(domain, domain_name.as_ptr());
if d > 0 {
Ok(DdsDomain(DdsEntity::new(d)))
} else {
Err(DDSError::from(d))
}
} else {
let d = cyclonedds_sys::dds_create_domain(domain, std::ptr::null());
if d > 0 {
Ok(DdsDomain(DdsEntity::new(d)))
} else {
Err(DDSError::from(d))
}
}
}
}
}
impl PartialEq for DdsDomain {
fn eq(&self, other: &Self) -> bool {
unsafe { self.0.entity() == other.0.entity() }
}
}
impl Eq for DdsDomain {}
impl Drop for DdsDomain {
fn drop(&mut self) {
unsafe {
let ret: DDSError = cyclonedds_sys::dds_delete(self.0.entity()).into();
if DDSError::DdsOk != ret {
panic!("cannot delete domain: {}", ret);
}
}
}
}
#[cfg(test)]
mod dds_domain_tests {
use cyclonedds_sys::{DDSError};
use crate::dds_domain::DdsDomain;
#[test]
fn test_create_domain_with_bad_config() {
assert!(Err(DDSError::DdsOk) != DdsDomain::create(0, Some("blah")));
}
}