objc_rs/
declare.rs

1/*!
2Functionality for declaring Objective-C classes.
3
4Classes can be declared using the `ClassDecl` struct. Instance variables and
5methods can then be added before the class is ultimately registered.
6
7# Example
8
9The following example demonstrates declaring a class named `MyNumber` that has
10one ivar, a `u32` named `_number` and a `number` method that returns it:
11
12``` no_run
13# #[macro_use] extern crate objc;
14# use objc::declare::ClassDecl;
15# use objc::runtime::{Class, Object, Sel};
16# fn main() {
17let superclass = class!(NSObject);
18let mut decl = ClassDecl::new("MyNumber", superclass).unwrap();
19
20// Add an instance variable
21decl.add_ivar::<u32>("_number");
22
23// Add an ObjC method for getting the number
24extern fn my_number_get(this: &Object, _cmd: Sel) -> u32 {
25    unsafe { *this.get_ivar("_number") }
26}
27unsafe {
28    decl.add_method(sel!(number),
29        my_number_get as extern fn(&Object, Sel) -> u32);
30}
31
32decl.register();
33# }
34```
35*/
36
37use std::ffi::CString;
38use std::mem;
39use std::ptr;
40
41use runtime::{BOOL, Class, Imp, NO, Object, Protocol, Sel, self};
42use {Encode, EncodeArguments, Encoding, Message};
43
44/// Types that can be used as the implementation of an Objective-C method.
45pub trait MethodImplementation {
46    /// The callee type of the method.
47    type Callee: Message;
48    /// The return type of the method.
49    type Ret: Encode;
50    /// The argument types of the method.
51    type Args: EncodeArguments;
52
53    /// Returns self as an `Imp` of a method.
54    fn imp(self) -> Imp;
55}
56
57macro_rules! method_decl_impl {
58    (-$s:ident, $r:ident, $f:ty, $($t:ident),*) => (
59        impl<$s, $r $(, $t)*> MethodImplementation for $f
60                where $s: Message, $r: Encode $(, $t: Encode)* {
61            type Callee = $s;
62            type Ret = $r;
63            type Args = ($($t,)*);
64
65            fn imp(self) -> Imp {
66                unsafe { mem::transmute(self) }
67            }
68        }
69    );
70    ($($t:ident),*) => (
71        method_decl_impl!(-T, R, extern fn(&T, Sel $(, $t)*) -> R, $($t),*);
72        method_decl_impl!(-T, R, extern fn(&mut T, Sel $(, $t)*) -> R, $($t),*);
73    );
74}
75
76method_decl_impl!();
77method_decl_impl!(A);
78method_decl_impl!(A, B);
79method_decl_impl!(A, B, C);
80method_decl_impl!(A, B, C, D);
81method_decl_impl!(A, B, C, D, E);
82method_decl_impl!(A, B, C, D, E, F);
83method_decl_impl!(A, B, C, D, E, F, G);
84method_decl_impl!(A, B, C, D, E, F, G, H);
85method_decl_impl!(A, B, C, D, E, F, G, H, I);
86method_decl_impl!(A, B, C, D, E, F, G, H, I, J);
87method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K);
88method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
89
90fn count_args(sel: Sel) -> usize {
91    sel.name().chars().filter(|&c| c == ':').count()
92}
93
94fn method_type_encoding(ret: &Encoding, args: &[Encoding]) -> CString {
95    let mut types = ret.as_str().to_owned();
96    // First two arguments are always self and the selector
97    types.push_str(<*mut Object>::encode().as_str());
98    types.push_str(Sel::encode().as_str());
99    types.extend(args.iter().map(|e| e.as_str()));
100    CString::new(types).unwrap()
101}
102
103fn log2_align_of<T>() -> u8 {
104    let align = mem::align_of::<T>();
105    // Alignments are required to be powers of 2
106    debug_assert!(align.count_ones() == 1);
107    // log2 of a power of 2 is the number of trailing zeros
108    align.trailing_zeros() as u8
109}
110
111/// A type for declaring a new class and adding new methods and ivars to it
112/// before registering it.
113pub struct ClassDecl {
114    cls: *mut Class,
115}
116
117impl ClassDecl {
118    fn with_superclass(name: &str, superclass: Option<&Class>)
119            -> Option<ClassDecl> {
120        let name = CString::new(name).unwrap();
121        let super_ptr = superclass.map_or(ptr::null(), |c| c);
122        let cls = unsafe {
123            runtime::objc_allocateClassPair(super_ptr, name.as_ptr(), 0)
124        };
125        if cls.is_null() {
126            None
127        } else {
128            Some(ClassDecl { cls: cls })
129        }
130    }
131
132    /// Constructs a `ClassDecl` with the given name and superclass.
133    /// Returns `None` if the class couldn't be allocated.
134    pub fn new(name: &str, superclass: &Class) -> Option<ClassDecl> {
135        ClassDecl::with_superclass(name, Some(superclass))
136    }
137
138    /**
139    Constructs a `ClassDecl` declaring a new root class with the given name.
140    Returns `None` if the class couldn't be allocated.
141
142    An implementation for `+initialize` must also be given; the runtime calls
143    this method for all classes, so it must be defined on root classes.
144
145    Note that implementing a root class is not a simple endeavor.
146    For example, your class probably cannot be passed to Cocoa code unless
147    the entire `NSObject` protocol is implemented.
148    Functionality it expects, like implementations of `-retain` and `-release`
149    used by ARC, will not be present otherwise.
150    */
151    pub fn root(name: &str, intitialize_fn: extern fn(&Class, Sel))
152            -> Option<ClassDecl> {
153        let mut decl = ClassDecl::with_superclass(name, None);
154        if let Some(ref mut decl) = decl {
155            unsafe {
156                decl.add_class_method(sel!(initialize), intitialize_fn);
157            }
158        }
159        decl
160    }
161
162    /// Adds a method with the given name and implementation to self.
163    /// Panics if the method wasn't sucessfully added
164    /// or if the selector and function take different numbers of arguments.
165    /// Unsafe because the caller must ensure that the types match those that
166    /// are expected when the method is invoked from Objective-C.
167    pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F)
168            where F: MethodImplementation<Callee=Object> {
169        let encs = F::Args::encodings();
170        let encs = encs.as_ref();
171        let sel_args = count_args(sel);
172        assert!(sel_args == encs.len(),
173            "Selector accepts {} arguments, but function accepts {}",
174            sel_args, encs.len(),
175        );
176
177        let types = method_type_encoding(&F::Ret::encode(), encs);
178        let success = runtime::class_addMethod(self.cls, sel, func.imp(),
179            types.as_ptr());
180        assert!(success != NO, "Failed to add method {:?}", sel);
181    }
182
183    /// Adds a class method with the given name and implementation to self.
184    /// Panics if the method wasn't sucessfully added
185    /// or if the selector and function take different numbers of arguments.
186    /// Unsafe because the caller must ensure that the types match those that
187    /// are expected when the method is invoked from Objective-C.
188    pub unsafe fn add_class_method<F>(&mut self, sel: Sel, func: F)
189            where F: MethodImplementation<Callee=Class> {
190        let encs = F::Args::encodings();
191        let encs = encs.as_ref();
192        let sel_args = count_args(sel);
193        assert!(sel_args == encs.len(),
194            "Selector accepts {} arguments, but function accepts {}",
195            sel_args, encs.len(),
196        );
197
198        let types = method_type_encoding(&F::Ret::encode(), encs);
199        let metaclass = (*self.cls).metaclass() as *const _ as *mut _;
200        let success = runtime::class_addMethod(metaclass, sel, func.imp(),
201            types.as_ptr());
202        assert!(success != NO, "Failed to add class method {:?}", sel);
203    }
204
205    /// Adds an ivar with type `T` and the provided name to self.
206    /// Panics if the ivar wasn't successfully added.
207    pub fn add_ivar<T>(&mut self, name: &str) where T: Encode {
208        let c_name = CString::new(name).unwrap();
209        let encoding = CString::new(T::encode().as_str()).unwrap();
210        let size = mem::size_of::<T>();
211        let align = log2_align_of::<T>();
212        let success = unsafe {
213            runtime::class_addIvar(self.cls, c_name.as_ptr(), size, align,
214                encoding.as_ptr())
215        };
216        assert!(success != NO, "Failed to add ivar {}", name);
217    }
218
219    /// Adds a protocol to self. Panics if the protocol wasn't successfully
220    /// added
221    pub fn add_protocol(&mut self, proto: &Protocol) {
222        let success = unsafe { runtime::class_addProtocol(self.cls, proto) };
223        assert!(success != NO, "Failed to add protocol {:?}", proto);
224    }
225
226    /// Registers self, consuming it and returning a reference to the
227    /// newly registered `Class`.
228    pub fn register(self) -> &'static Class {
229        unsafe {
230            let cls = self.cls;
231            runtime::objc_registerClassPair(cls);
232            // Forget self otherwise the class will be disposed in drop
233            mem::forget(self);
234            &*cls
235        }
236    }
237}
238
239impl Drop for ClassDecl {
240    fn drop(&mut self) {
241        unsafe {
242            runtime::objc_disposeClassPair(self.cls);
243        }
244    }
245}
246
247/// A type for declaring a new protocol and adding new methods to it
248/// before registering it.
249pub struct ProtocolDecl {
250    proto: *mut Protocol
251}
252
253impl ProtocolDecl {
254    /// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
255    /// protocol couldn't be allocated.
256    pub fn new(name: &str) -> Option<ProtocolDecl> {
257        let c_name = CString::new(name).unwrap();
258        let proto = unsafe {
259            runtime::objc_allocateProtocol(c_name.as_ptr())
260        };
261        if proto.is_null() {
262            None
263        } else {
264            Some(ProtocolDecl { proto: proto })
265        }
266    }
267
268    fn add_method_description_common<Args, Ret>(&mut self, sel: Sel, is_required: bool,
269            is_instance_method: bool)
270            where Args: EncodeArguments,
271                  Ret: Encode {
272        let encs = Args::encodings();
273        let encs = encs.as_ref();
274        let sel_args = count_args(sel);
275        assert!(sel_args == encs.len(),
276            "Selector accepts {} arguments, but function accepts {}",
277            sel_args, encs.len(),
278        );
279        let types = method_type_encoding(&Ret::encode(), encs);
280        unsafe {
281            runtime::protocol_addMethodDescription(
282                self.proto, sel, types.as_ptr(), is_required as BOOL, is_instance_method as BOOL);
283        }
284    }
285
286    /// Adds an instance method declaration with a given description to self.
287    pub fn add_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
288            where Args: EncodeArguments,
289                  Ret: Encode {
290        self.add_method_description_common::<Args, Ret>(sel, is_required, true)
291    }
292
293    /// Adds a class method declaration with a given description to self.
294    pub fn add_class_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
295            where Args: EncodeArguments,
296                  Ret: Encode {
297        self.add_method_description_common::<Args, Ret>(sel, is_required, false)
298    }
299
300    /// Adds a requirement on another protocol.
301    pub fn add_protocol(&mut self, proto: &Protocol) {
302        unsafe {
303            runtime::protocol_addProtocol(self.proto, proto);
304        }
305    }
306
307    /// Registers self, consuming it and returning a reference to the
308    /// newly registered `Protocol`.
309    pub fn register(self) -> &'static Protocol {
310        unsafe {
311            runtime::objc_registerProtocol(self.proto);
312            &*self.proto
313        }
314    }
315}
316
317#[cfg(test)]
318mod tests {
319    use test_utils;
320
321    #[test]
322    fn test_custom_class() {
323        // Registering the custom class is in test_utils
324        let obj = test_utils::custom_object();
325        unsafe {
326            let _: () = msg_send![obj, setFoo:13u32];
327            let result: u32 = msg_send![obj, foo];
328            assert!(result == 13);
329        }
330    }
331
332    #[test]
333    fn test_class_method() {
334        let cls = test_utils::custom_class();
335        unsafe {
336            let result: u32 = msg_send![cls, classFoo];
337            assert!(result == 7);
338        }
339    }
340}