use crate::{raw, AtmiCtx, TypedBuffer, TypedUbf};
use core::ffi::c_char;
use std::ffi::CStr;
#[derive(Debug)]
pub struct TpSvcInfo<'ctx> {
raw: *mut raw::TPSVCINFO,
ctx: &'ctx AtmiCtx,
data: Option<TypedBuffer<'ctx>>,
}
impl<'ctx> TpSvcInfo<'ctx> {
pub(crate) unsafe fn from_raw(ctx: &'ctx AtmiCtx, raw: *mut raw::TPSVCINFO) -> Self {
let data_ptr = (*raw).data as *mut c_char;
let data = if data_ptr.is_null() {
None
} else {
Some(TypedBuffer::from_raw(ctx, data_ptr))
};
TpSvcInfo { raw, ctx, data }
}
#[inline]
fn raw(&self) -> &raw::TPSVCINFO {
unsafe { &*self.raw }
}
#[inline]
fn raw_mut(&mut self) -> &mut raw::TPSVCINFO {
unsafe { &mut *self.raw }
}
pub fn name(&self) -> &str {
unsafe {
CStr::from_ptr(self.raw().name.as_ptr())
.to_str()
.unwrap_or("")
}
}
pub fn fname(&self) -> &str {
unsafe {
CStr::from_ptr(self.raw().fname.as_ptr())
.to_str()
.unwrap_or("")
}
}
pub fn len(&self) -> i64 {
self.raw().len
}
pub fn set_len(&mut self, len: i64) {
self.raw_mut().len = len;
}
pub fn flags(&self) -> i64 {
self.raw().flags
}
pub fn set_flags(&mut self, flags: i64) {
self.raw_mut().flags = flags;
}
pub fn cd(&self) -> i32 {
self.raw().cd
}
pub fn appkey(&self) -> i64 {
self.raw().appkey
}
pub fn cltid(&self) -> crate::ClientId {
self.raw().cltid
}
pub(crate) fn data_ptr(&self) -> *mut c_char {
self.data
.as_ref()
.map(|b| b.as_ptr())
.unwrap_or(std::ptr::null_mut())
}
pub fn take_data(&mut self) -> Option<TypedBuffer<'ctx>> {
self.data.take()
}
pub fn take_data_ubf(&mut self) -> Option<TypedUbf<'ctx>> {
self.data.take().map(TypedUbf::from_typed)
}
}