use super::base::Id;
use objc::rc::StrongPtr;
use objc::{class, msg_send, sel, sel_impl};
pub trait VZPlatformConfiguration {
fn id(&self) -> Id;
}
pub struct VZGenericPlatformConfiguration(StrongPtr);
impl Default for VZGenericPlatformConfiguration {
fn default() -> Self {
Self::new()
}
}
impl VZGenericPlatformConfiguration {
pub fn new() -> Self {
unsafe {
let p = StrongPtr::new(msg_send![class!(VZGenericPlatformConfiguration), new]);
VZGenericPlatformConfiguration(p)
}
}
pub fn set_machine_identifier(&mut self, identifier: &VZGenericMachineIdentifier) {
unsafe {
let _: () = msg_send![*self.0, setMachineIdentifier:*identifier.0];
}
}
}
impl VZPlatformConfiguration for VZGenericPlatformConfiguration {
fn id(&self) -> Id {
*self.0
}
}
pub struct VZGenericMachineIdentifier(StrongPtr);
impl Default for VZGenericMachineIdentifier {
fn default() -> Self {
Self::new()
}
}
impl VZGenericMachineIdentifier {
pub fn new() -> Self {
unsafe {
let p = StrongPtr::new(msg_send![class!(VZGenericMachineIdentifier), new]);
VZGenericMachineIdentifier(p)
}
}
pub fn from_data(data: &[u8]) -> Option<Self> {
unsafe {
let nsdata_alloc: Id = msg_send![class!(NSData), alloc];
let nsdata: Id = msg_send![nsdata_alloc, initWithBytes:data.as_ptr() length:data.len()];
if nsdata.is_null() {
return None;
}
let nsdata = StrongPtr::new(nsdata);
let alloc: Id = msg_send![class!(VZGenericMachineIdentifier), alloc];
let p: Id = msg_send![alloc, initWithDataRepresentation:*nsdata];
if p.is_null() {
None
} else {
Some(VZGenericMachineIdentifier(StrongPtr::new(p)))
}
}
}
pub fn data_representation(&self) -> Vec<u8> {
unsafe {
let data: Id = msg_send![*self.0, dataRepresentation];
if data.is_null() {
return Vec::new();
}
let length: usize = msg_send![data, length];
let bytes: *const u8 = msg_send![data, bytes];
std::slice::from_raw_parts(bytes, length).to_vec()
}
}
}