use super::{id, Class, NSUInteger, Object, BOOL, SEL};
use std::{ops::Deref, ptr::NonNull};
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct NSObject(id);
impl Deref for NSObject {
type Target = id;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<NSObject> for NSObject {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl NSObject {
#[inline]
pub fn class() -> &'static Class {
extern "C" {
#[link_name = "OBJC_CLASS_$_NSObject"]
static CLASS: Class;
}
unsafe { &CLASS }
}
#[inline]
pub const unsafe fn from_ptr(ptr: *mut Object) -> Self {
Self(id::from_ptr(ptr))
}
#[inline]
pub const unsafe fn from_non_null_ptr(ptr: NonNull<Object>) -> Self {
Self(id::from_non_null_ptr(ptr))
}
#[inline]
pub fn as_id(&self) -> &id {
&self.0
}
#[inline]
pub fn as_ptr(&self) -> *mut Object {
self.0.as_ptr()
}
#[inline]
pub fn as_non_null_ptr(&self) -> NonNull<Object> {
self.0.as_non_null_ptr()
}
#[inline]
pub fn responds_to_selector(&self, selector: SEL) -> bool {
extern "C" {
fn objc_msgSend(obj: &Object, sel: SEL, selector: SEL) -> BOOL;
}
let sel = selector!(respondsToSelector:);
unsafe { objc_msgSend(self, sel, selector) != 0 }
}
#[inline]
pub fn is_kind_of_class(&self, class: &Class) -> bool {
extern "C" {
fn objc_msgSend(obj: &Object, sel: SEL, class: &Class) -> BOOL;
}
let sel = selector!(isKindOfClass:);
unsafe { objc_msgSend(self, sel, class) != 0 }
}
#[inline]
pub fn is_member_of_class(&self, class: &Class) -> bool {
extern "C" {
fn objc_msgSend(obj: &Object, sel: SEL, class: &Class) -> BOOL;
}
let sel = selector!(isMemberOfClass:);
unsafe { objc_msgSend(self, sel, class) != 0 }
}
#[inline]
pub fn hash(&self) -> NSUInteger {
extern "C" {
fn objc_msgSend(obj: &Object, sel: SEL) -> NSUInteger;
}
let sel = selector!(hash);
unsafe { objc_msgSend(self, sel) }
}
#[inline]
pub fn copy(&self) -> NSObject {
extern "C" {
fn objc_msgSend(obj: &Object, sel: SEL) -> NSObject;
}
let sel = selector!(copy);
unsafe { objc_msgSend(self, sel) }
}
#[inline]
pub fn mutable_copy(&self) -> NSObject {
extern "C" {
fn objc_msgSend(obj: &Object, sel: SEL) -> NSObject;
}
let sel = selector!(mutableCopy);
unsafe { objc_msgSend(self, sel) }
}
}