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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
macro_rules! fbox_c_str {
($cb_name:expr) => {
cstr!(concat!("_IUPRUST_FBOX_", $cb_name))
}
}
macro_rules! set_fbox_callback {
($ih:expr, $cb_name:expr, $clistener:expr, $rcb:expr, Callback<$($rargs:ty),*>) => {{
use $crate::iup_sys;
unsafe fn box_into_raw<T : ?Sized>(b: Box<T>) -> *mut T {
transmute(b)
}
clear_fbox_callback!($ih, $cb_name, Callback<$($rargs),*>);
let ih = $ih;
let fb: Box<Box<$crate::callback::Callback<$($rargs),*>>> = Box::new(Box::new($rcb));
iup_sys::IupSetAttribute(ih, fbox_c_str!($cb_name), box_into_raw(fb) as *const _);
if ih.is_null() {
iup_sys::IupSetFunction(cstr!($cb_name), transmute($clistener));
} else {
iup_sys::IupSetCallback(ih, cstr!($cb_name), transmute($clistener));
}
}}
}
macro_rules! clear_fbox_callback {
($ih:expr, $cb_name:expr, Callback<$($rargs:ty),*>) => {{
use $crate::iup_sys;
use std::mem::transmute;
use std::ptr;
let ih = $ih;
let capsule_box = iup_sys::IupGetAttribute(ih, fbox_c_str!($cb_name))
as *mut Box<$crate::callback::Callback<$($rargs),*>>;
if capsule_box.is_null() {
None
} else {
let inner_box: Box<Box<$crate::callback::Callback<$($rargs),*>>> = transmute(capsule_box);
iup_sys::IupSetAttribute(ih, fbox_c_str!($cb_name), ptr::null());
if ih.is_null() {
iup_sys::IupSetFunction(cstr!($cb_name), transmute(ptr::null::<u8>()));
} else {
iup_sys::IupSetCallback(ih, cstr!($cb_name), transmute(ptr::null::<u8>()));
}
Some(*inner_box)
}
}}
}
macro_rules! get_fbox_callback {
($ih:expr, $cb_name:expr, Callback<$($rargs:ty),*>) => {{
let fbox_ptr = unsafe {
iup_sys::IupGetAttribute($ih, fbox_c_str!($cb_name))
as *mut Box<$crate::callback::Callback<($($rargs),*)>>
};
assert!(fbox_ptr.is_null() == false);
let fbox: &mut Box<_> = unsafe { &mut (*(fbox_ptr)) };
fbox
}}
}
macro_rules! impl_callback {
(
$(#[$trait_attr:meta])*
pub trait $trait_name:ident where Self: Element {
let name = $cb_name:expr;
extern fn listener(ih: *mut iup_sys::Ihandle $(, $ls_arg:ident: $ls_arg_ty:ty)*) -> CallbackReturn;
fn $set_method:ident<F: Callback(Self $(, $fn_arg_ty:ty)*)>(&mut self, cb: F) -> Self;
fn $remove_method:ident(&mut self) -> Option<Box<_>>;
}
) => {
impl_callback! {
$(#[$trait_attr])*
pub trait $trait_name where Self: Element {
let name = $cb_name;
extern fn listener(ih: *mut iup_sys::Ihandle $(, $ls_arg: $ls_arg_ty)*) -> CallbackReturn;
fn $set_method<F: Callback(Self $(, $fn_arg_ty)*)>(&mut self, cb: F) -> Self;
fn $remove_method(&mut self) -> Option<Box<_>>;
fn resolve_args(elem: Self, $($ls_arg: $ls_arg_ty),*) -> (Self, $($fn_arg_ty),*) {
(elem, $($ls_arg.into_rust()),*)
}
}
}
};
(
$(#[$trait_attr:meta])*
pub trait $trait_name:ident where Self: Element {
let name = $cb_name:expr;
extern fn listener(ih: *mut iup_sys::Ihandle $(, $ls_arg:ident: $ls_arg_ty:ty)*) -> CallbackReturn;
fn $set_method:ident<F: Callback(Self $(, $fn_arg_ty:ty)*)>(&mut self, cb: F) -> Self;
fn $remove_method:ident(&mut self) -> Option<Box<_>>;
fn resolve_args($aa_argself:ident: Self, $($aa_arg:ident: $aa_arg_ty:ty),*)
-> (Self, $($aa_ret_ty:ty),*)
$resolve_args:expr
}
) => {
$(#[$trait_attr])*
pub trait $trait_name where Self: $crate::Element + 'static {
fn $set_method<F>(&mut self, cb: F) -> Self
where F: $crate::callback::Callback<(Self, $($fn_arg_ty),*)> {
use std::mem::transmute;
use libc::c_int;
use $crate::iup_sys;
#[allow(unused_imports)]
use $crate::callback::IntoRust;
fn resolve_args<Self0: $trait_name>($aa_argself: Self0, $($aa_arg: $aa_arg_ty),*)
-> (Self0, $($aa_ret_ty),*) {
$resolve_args
}
extern fn listener<Self0: $trait_name + 'static>(ih: *mut iup_sys::Ihandle, $($ls_arg: $ls_arg_ty),*) -> c_int {
let fbox: &mut Box<_> = get_fbox_callback!(ih, $cb_name, Callback<(Self0, $($fn_arg_ty),*)>);
let element = unsafe { <Self0 as $crate::Element>::from_raw_unchecked(ih) };
fbox.on_callback(resolve_args::<Self0>(element, $($ls_arg),*))
}
unsafe {
set_fbox_callback!(self.raw(), $cb_name, listener::<Self>, cb,
Callback<(Self, $($fn_arg_ty),*)>);
}
self.clone()
}
fn $remove_method(&mut self)
-> Option<Box<$crate::callback::Callback<(Self, $($fn_arg_ty),*)>>> {
unsafe {
let old_cb = clear_fbox_callback!(self.raw(), $cb_name,
Callback<(Self, $($fn_arg_ty),*)>);
old_cb
}
}
}
};
(
let name = $cb_name:expr;
extern fn listener($($ls_arg:ident: $ls_arg_ty:ty),*) -> CallbackReturn;
$(#[$set_func_attr:meta])*
pub fn $set_func:ident<F: Callback($($fn_arg_ty:ty),*)>(cb: F);
$(#[$rem_func_attr:meta])*
pub fn $remove_func:ident() -> Option<Box<_>>;
) => {
$(#[$set_func_attr])*
pub fn $set_func<F>(cb: F)
where F: $crate::callback::Callback<($($fn_arg_ty),*)> {
use std::mem::transmute;
use std::ptr;
use libc::c_int;
use $crate::iup_sys;
#[allow(unused_imports)]
use $crate::callback::IntoRust;
extern fn listener($($ls_arg: $ls_arg_ty),*) -> c_int {
let fbox: &mut Box<_> = get_fbox_callback!(ptr::null_mut(), $cb_name, Callback<($($fn_arg_ty),*)>);
fbox.on_callback(($($ls_arg.into_rust()),*))
}
unsafe {
set_fbox_callback!(ptr::null_mut(), $cb_name, listener, cb,
Callback<($($fn_arg_ty),*)>);
}
}
$(#[$rem_func_attr])*
pub fn $remove_func()
-> Option<Box<$crate::callback::Callback<($($fn_arg_ty),*)>>> {
unsafe {
let old_cb = clear_fbox_callback!(ptr::null_mut(), $cb_name,
Callback<($($fn_arg_ty),*)>);
old_cb
}
}
};
}
macro_rules! drop_callback {
($ih:ident, $cb_name:expr) => {{
use std::mem::transmute;
use std::any::Any;
let capsule_box = iup_sys::IupGetAttribute($ih, fbox_c_str!($cb_name))
as *mut Box<Any>;
if !capsule_box.is_null() {
let inner_box: Box<Box<Any>> = transmute(capsule_box);
drop(inner_box);
}
}}
}