objc2/ffi/
class.rs

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