objc_sys/
class.rs

1use std::os::raw::{c_char, c_int, c_uint};
2
3#[cfg(any(doc, not(feature = "unstable-objfw")))]
4use crate::{objc_ivar, objc_method, objc_object, objc_property, objc_property_attribute_t};
5use crate::{objc_protocol, objc_selector, OpaqueData, BOOL, IMP};
6
7/// An opaque type that represents an Objective-C class.
8#[repr(C)]
9pub struct objc_class {
10    // `isa` field is deprecated and not available on GNUStep, so we don't
11    // expose it here. Use `class_getSuperclass` instead.
12    _priv: [u8; 0],
13    _p: OpaqueData,
14}
15
16#[cfg(any(doc, not(feature = "unstable-objfw")))]
17/// This is `c_char` in GNUStep's libobjc2 and `uint8_t` in Apple's objc4.
18///
19/// The pointer represents opaque data, and is definitely not just an integer,
20/// so its signedness (i8 vs. u8) is not applicable.
21///
22/// So we assign it here as a private alias to u8, to not document the
23/// difference.
24type ivar_layout_type = u8;
25
26// May call `resolveClassMethod:` or `resolveInstanceMethod:`.
27extern_c_unwind! {
28    #[cfg(any(doc, not(feature = "unstable-objfw")))]
29    pub fn class_getClassMethod(
30        cls: *const objc_class,
31        name: *const objc_selector,
32    ) -> *const objc_method;
33    #[cfg(any(doc, not(feature = "unstable-objfw")))] // Available in newer versions
34    pub fn class_getInstanceMethod(
35        cls: *const objc_class,
36        name: *const objc_selector,
37    ) -> *const objc_method;
38
39    pub fn class_respondsToSelector(cls: *const objc_class, sel: *const objc_selector) -> BOOL;
40
41    // #[deprecated = "use class_getMethodImplementation instead"]
42    // #[cfg(any(doc, target_vendor = "apple"))]
43    // pub fn class_lookupMethod
44    // #[deprecated = "use class_respondsToSelector instead"]
45    // #[cfg(any(doc, target_vendor = "apple"))]
46    // pub fn class_respondsToMethod
47}
48
49// TODO: Hooks registered with objc_setHook_getClass may be allowed to unwind?
50extern_c! {
51    pub fn objc_getClass(name: *const c_char) -> *const objc_class;
52    pub fn objc_getRequiredClass(name: *const c_char) -> *const objc_class;
53    pub fn objc_lookUpClass(name: *const c_char) -> *const objc_class;
54    #[cfg(any(doc, not(feature = "unstable-objfw")))]
55    pub fn objc_getMetaClass(name: *const c_char) -> *const objc_class;
56    /// The returned array is deallocated with [`free`][crate::free].
57    pub fn objc_copyClassList(out_len: *mut c_uint) -> *mut *const objc_class;
58    pub fn objc_getClassList(buffer: *mut *const objc_class, buffer_len: c_int) -> c_int;
59
60    pub fn objc_allocateClassPair(
61        superclass: *const objc_class,
62        name: *const c_char,
63        extra_bytes: usize,
64    ) -> *mut objc_class;
65    #[cfg(any(doc, target_vendor = "apple"))]
66    pub fn objc_duplicateClass(
67        original: *const objc_class,
68        name: *const c_char,
69        extra_bytes: usize,
70    ) -> *mut objc_class;
71    #[cfg(any(doc, not(feature = "unstable-objfw")))]
72    pub fn objc_disposeClassPair(cls: *mut objc_class);
73    pub fn objc_registerClassPair(cls: *mut objc_class);
74
75    #[cfg(any(doc, not(feature = "unstable-objfw")))]
76    pub fn class_addIvar(
77        cls: *mut objc_class,
78        name: *const c_char,
79        size: usize,
80        alignment: u8,
81        types: *const c_char,
82    ) -> BOOL;
83    pub fn class_addMethod(
84        cls: *mut objc_class,
85        name: *const objc_selector,
86        imp: IMP,
87        types: *const c_char,
88    ) -> BOOL;
89    #[cfg(any(doc, not(feature = "unstable-objfw")))]
90    pub fn class_addProperty(
91        cls: *mut objc_class,
92        name: *const c_char,
93        attributes: *const objc_property_attribute_t,
94        attributes_count: c_uint,
95    ) -> BOOL;
96    #[cfg(any(doc, not(feature = "unstable-objfw")))]
97    pub fn class_addProtocol(cls: *mut objc_class, protocol: *const objc_protocol) -> BOOL;
98    pub fn class_conformsToProtocol(cls: *const objc_class, protocol: *const objc_protocol)
99        -> BOOL;
100
101    #[cfg(any(doc, not(feature = "unstable-objfw")))] // Available in newer versions
102    /// The return value is deallocated with [`free`][crate::free].
103    pub fn class_copyIvarList(
104        cls: *const objc_class,
105        out_len: *mut c_uint,
106    ) -> *mut *const objc_ivar;
107    #[cfg(any(doc, not(feature = "unstable-objfw")))] // Available in newer versions
108    /// The returned array is deallocated with [`free`][crate::free].
109    pub fn class_copyMethodList(
110        cls: *const objc_class,
111        out_len: *mut c_uint,
112    ) -> *mut *const objc_method;
113    #[cfg(any(doc, not(feature = "unstable-objfw")))] // Available in newer versions
114    /// The returned array is deallocated with [`free`][crate::free].
115    pub fn class_copyPropertyList(
116        cls: *const objc_class,
117        out_len: *mut c_uint,
118    ) -> *mut *const objc_property;
119    #[cfg(any(doc, not(feature = "unstable-objfw")))]
120    /// The returned array is deallocated with [`free`][crate::free].
121    pub fn class_copyProtocolList(
122        cls: *const objc_class,
123        out_len: *mut c_uint,
124    ) -> *mut *const objc_protocol;
125
126    #[cfg(any(doc, not(feature = "unstable-objfw")))]
127    pub fn class_createInstance(cls: *const objc_class, extra_bytes: usize) -> *mut objc_object;
128    #[cfg(any(doc, not(feature = "unstable-objfw")))]
129    pub fn class_getClassVariable(cls: *const objc_class, name: *const c_char) -> *const objc_ivar;
130    #[cfg(any(doc, target_vendor = "apple"))]
131    pub fn class_getImageName(cls: *const objc_class) -> *const c_char;
132    pub fn class_getInstanceSize(cls: *const objc_class) -> usize;
133    #[cfg(any(doc, not(feature = "unstable-objfw")))]
134    pub fn class_getInstanceVariable(
135        cls: *const objc_class,
136        name: *const c_char,
137    ) -> *const objc_ivar;
138    #[cfg(any(doc, not(feature = "unstable-objfw")))]
139    pub fn class_getIvarLayout(cls: *const objc_class) -> *const ivar_layout_type;
140    pub fn class_getName(cls: *const objc_class) -> *const c_char;
141    #[cfg(any(doc, not(feature = "unstable-objfw")))]
142    pub fn class_getProperty(cls: *const objc_class, name: *const c_char) -> *const objc_property;
143    pub fn class_getSuperclass(cls: *const objc_class) -> *const objc_class;
144    #[cfg(any(doc, not(feature = "unstable-objfw")))]
145    pub fn class_getVersion(cls: *const objc_class) -> c_int;
146    #[cfg(any(doc, target_vendor = "apple"))]
147    pub fn class_getWeakIvarLayout(cls: *const objc_class) -> *const ivar_layout_type;
148    pub fn class_isMetaClass(cls: *const objc_class) -> BOOL;
149    pub fn class_replaceMethod(
150        cls: *mut objc_class,
151        name: *const objc_selector,
152        imp: IMP,
153        types: *const c_char,
154    ) -> IMP;
155    #[cfg(any(doc, not(feature = "unstable-objfw")))]
156    pub fn class_replaceProperty(
157        cls: *mut objc_class,
158        name: *const c_char,
159        attributes: *const objc_property_attribute_t,
160        attributes_len: c_uint,
161    );
162    #[cfg(any(doc, not(feature = "unstable-objfw")))]
163    pub fn class_setIvarLayout(cls: *mut objc_class, layout: *const ivar_layout_type);
164    #[cfg(any(doc, not(feature = "unstable-objfw")))]
165    pub fn class_setVersion(cls: *mut objc_class, version: c_int);
166    #[cfg(any(doc, target_vendor = "apple"))]
167    pub fn class_setWeakIvarLayout(cls: *mut objc_class, layout: *const ivar_layout_type);
168
169    // #[deprecated = "not recommended"]
170    // pub fn class_setSuperclass
171}