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
use super::{NSEdgeInsets, NSPoint, NSRange, NSRect, NSSize};
use crate::core::Arc;
use crate::objc::{ClassType, NSObject, NSUInteger, ObjCObject};
use std::{
ffi::CStr,
mem,
os::raw::{c_char, c_void},
};
objc_subclass! {
pub class NSValue: NSObject<'static>;
}
impl NSValue {
#[inline]
#[doc(alias = "valueWithBytes")]
pub unsafe fn from_bytes(value: *const c_void, objc_type: *const c_char) -> Arc<Self> {
_msg_send_any![
Self::class(),
valueWithBytes: value
withObjCType: objc_type
]
}
pub(crate) fn objc_type_single(&self) -> c_char {
let objc_type = self.objc_type();
let single = unsafe { *objc_type };
if single == 0 || unsafe { *objc_type.add(1) != 0 } {
0
} else {
single
}
}
#[inline]
#[doc(alias = "objCType")]
pub fn objc_type(&self) -> *const c_char {
unsafe { _msg_send_any![self, objCType] }
}
#[inline]
#[doc(alias = "objCType")]
pub fn objc_type_cstr(&self) -> &CStr {
unsafe { CStr::from_ptr(self.objc_type()) }
}
#[inline]
#[doc(alias = "getValue")]
#[doc(alias = "getValue:size:")]
pub unsafe fn write_value(&self, value: *mut c_void, size: NSUInteger) {
_msg_send_any![
self,
getValue: value
size: size
]
}
#[inline]
#[doc(alias = "getValue")]
#[doc(alias = "getValue:size:")]
pub fn write_value_slice(&self, value: &mut [u8]) {
unsafe { self.write_value(value.as_mut_ptr().cast(), value.len()) };
}
#[inline]
#[doc(alias = "getValue")]
#[doc(alias = "getValue:size:")]
pub unsafe fn transmute_value<T>(&self) -> T {
let mut value = mem::MaybeUninit::<T>::uninit();
self.write_value(value.as_mut_ptr().cast(), mem::size_of::<T>());
value.assume_init()
}
}
impl NSValue {
#[inline]
#[doc(alias = "valueWithPointer")]
#[doc(alias = "valueWithPointer:")]
pub fn from_ptr(ptr: *const c_void) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithPointer: ptr] }
}
#[inline]
pub fn ptr_value(&self) -> *const c_void {
unsafe { _msg_send_any![self, pointerValue] }
}
#[inline]
#[doc(alias = "valueWithNonretainedObject")]
#[doc(alias = "valueWithNonretainedObject:")]
pub fn from_nonretained_object(obj: *mut ObjCObject) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithNonretainedObject: obj] }
}
#[inline]
pub fn nonretained_object_value(&self) -> *mut ObjCObject<'_> {
unsafe { _msg_send_strict![self, nonretainedObjectValue] }
}
}
impl NSValue {
#[inline]
#[doc(alias = "valueWithRange")]
#[doc(alias = "valueWithRange:")]
pub fn from_range(value: NSRange) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithRange: value] }
}
#[inline]
pub fn range_value(&self) -> NSRange {
unsafe { _msg_send_any![self, pointValue] }
}
}
impl NSValue {
#[inline]
#[doc(alias = "valueWithPoint")]
#[doc(alias = "valueWithPoint:")]
pub fn from_point(value: NSPoint) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithPoint: value] }
}
#[inline]
#[doc(alias = "pointValue")]
pub fn point_value(&self) -> NSPoint {
unsafe { _msg_send_any![self, pointValue] }
}
#[inline]
#[doc(alias = "valueWithSize")]
#[doc(alias = "valueWithSize:")]
pub fn from_size(value: NSSize) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithSize: value] }
}
#[inline]
#[doc(alias = "sizeValue")]
pub fn size_value(&self) -> NSSize {
unsafe { _msg_send_any![self, sizeValue] }
}
#[inline]
#[doc(alias = "valueWithRect")]
#[doc(alias = "valueWithRect:")]
pub fn from_rect(value: NSRect) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithRect: value] }
}
#[inline]
#[doc(alias = "rectValue")]
pub fn rect_value(&self) -> NSRect {
unsafe { _msg_send_any![self, rectValue] }
}
#[inline]
#[doc(alias = "valueWithEdgeInsets")]
#[doc(alias = "valueWithEdgeInsets:")]
pub fn from_edge_insets(value: NSEdgeInsets) -> Arc<Self> {
unsafe { _msg_send_any![Self::class(), valueWithEdgeInsets: value] }
}
#[inline]
#[doc(alias = "edgeInsetsValue")]
pub fn edge_insets_value(&self) -> NSEdgeInsets {
unsafe { _msg_send_any![self, edgeInsetsValue] }
}
}