-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathmacros.rs
More file actions
117 lines (110 loc) · 3.07 KB
/
macros.rs
File metadata and controls
117 lines (110 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
Gets a reference to a `Class`.
Panics if no class with the given name can be found.
To check for a class that may not exist, use `Class::get`.
# Example
``` no_run
# #[macro_use] extern crate objc;
# fn main() {
let cls = class!(NSObject);
# }
```
*/
#[macro_export]
macro_rules! class {
($name:ident) => {{
static CLASS: $crate::__CachedClass = $crate::__CachedClass::new();
let name = concat!(stringify!($name), '\0');
#[allow(unused_unsafe)]
let cls = unsafe { CLASS.get(name) };
match cls {
Some(cls) => cls,
None => panic!("Class with name {} could not be found", stringify!($name)),
}
}};
}
/**
Registers a selector, returning a `Sel`.
# Example
```
# #[macro_use] extern crate objc;
# fn main() {
let sel = sel!(description);
let sel = sel!(setObject:forKey:);
# }
```
*/
#[macro_export]
macro_rules! sel {
($name:ident) => ({
static SEL: $crate::__CachedSel = $crate::__CachedSel::new();
let name = concat!(stringify!($name), '\0');
#[allow(unused_unsafe)]
unsafe { SEL.get(name) }
});
($($name:ident :)+) => ({
static SEL: $crate::__CachedSel = $crate::__CachedSel::new();
let name = concat!($(stringify!($name), ':'),+, '\0');
#[allow(unused_unsafe)]
unsafe { SEL.get(name) }
});
}
/**
Sends a message to an object.
The first argument can be any type that dereferences to a type that implements
`Message`, like a reference, pointer, or an `Id`.
The syntax is similar to the message syntax in Objective-C.
Variadic arguments are not currently supported.
# Example
``` no_run
# #[macro_use] extern crate objc;
# use objc::runtime::Object;
# fn main() {
# unsafe {
let obj: *mut Object;
# let obj: *mut Object = 0 as *mut Object;
let description: *const Object = msg_send![obj, description];
let _: () = msg_send![obj, setArg1:1 arg2:2];
# }
# }
```
*/
#[macro_export]
macro_rules! msg_send {
(super($obj:expr, $superclass:expr), $name:ident) => ({
let sel = $crate::sel!($name);
let result;
match $crate::__send_super_message(&*$obj, $superclass, sel, ()) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
}
result
});
(super($obj:expr, $superclass:expr), $($name:ident : $arg:expr)+) => ({
let sel = $crate::sel!($($name:)+);
let result;
match $crate::__send_super_message(&*$obj, $superclass, sel, ($($arg,)*)) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
}
result
});
($obj:expr, $name:ident) => ({
let sel = $crate::sel!($name);
let result;
match $crate::__send_message(&*$obj, sel, ()) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
}
result
});
($obj:expr, $($name:ident : $arg:expr)+) => ({
let sel = $crate::sel!($($name:)+);
let result;
match $crate::__send_message(&*$obj, sel, ($($arg,)*)) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
}
result
});
}