rust_macios/foundation/
ns_coder.rs

1use libc::{c_char, c_double, c_float, c_void};
2use objc::{msg_send, sel, sel_impl};
3
4use crate::core_graphics::{CGPoint, CGRect, CGSize};
5use crate::{object, 
6    objective_c_runtime::{
7        id,
8        macros::{interface_impl},
9        traits::{PNSObject, ToId},
10    },
11    utils::to_bool,
12};
13
14use super::{
15    Int, Int32, Int64, NSDecodingFailurePolicy, NSPoint, NSRect, NSSize, NSString, UInt, UInt8,
16};
17
18object! {
19    /// An abstract class that serves as the basis for objects that enable archiving and distribution of other objects.
20    unsafe pub struct NSCoder;
21}
22
23#[interface_impl(NSObject)]
24impl NSCoder {
25    /* Inspecting a Coder
26     */
27
28    /// Returns a Boolean value that indicates whether an encoded value is available for a string.
29    #[property]
30    pub fn allows_keyed_coding(&self) -> bool {
31        unsafe { to_bool(msg_send![self.m_self(), allowsKeyedCoding]) }
32    }
33
34    /// Returns a Boolean value that indicates whether an encoded value is available for a string.
35    #[method]
36    pub fn contains_value_for_key(&self, key: NSString) -> bool {
37        unsafe { to_bool(msg_send![self.m_self(), containsValueForKey: key]) }
38    }
39
40    /// The action the coder should take when decoding fails.
41    #[property]
42    pub fn decoding_failure_policy(&self) -> NSDecodingFailurePolicy {
43        unsafe { msg_send![self.m_self(), decodingFailurePolicy] }
44    }
45
46    /* Encoding General Data
47     */
48
49    /// Encodes an array of the given Objective-C type, provided the number of items and a pointer.
50    #[method]
51    pub fn encode_array_of_objc_type_count_at(
52        &mut self,
53        type_: *const c_char,
54        count: usize,
55        array: *const c_void,
56    ) {
57        unsafe { msg_send![self.m_self(), encodeArrayOfObjCType: type_ count: count at: array] }
58    }
59
60    /// Encodes a Boolean value and associates it with the string key.
61    #[method]
62    pub fn encode_bool_for_key(&mut self, value: bool, key: NSString) {
63        unsafe { msg_send![self.m_self(), encodeBool: value forKey: key] }
64    }
65
66    /// An encoding method for subclasses to override such that it creates a copy, rather than a proxy, when decoded.
67    #[method]
68    pub fn encode_bycopy_object<T>(&mut self, object: T)
69    where
70        T: PNSObject + ToId,
71    {
72        unsafe { msg_send![self.m_self(), encodeBycopy: object.to_id()] }
73    }
74
75    /// An encoding method for subclasses to override such that it creates a proxy, rather than a copy, when decoded.
76    #[method]
77    pub fn encode_byref_object<T>(&mut self, object: T)
78    where
79        T: PNSObject,
80    {
81        unsafe { msg_send![self.m_self(), encodeByref: object] }
82    }
83
84    /// Encodes a buffer of data of an unspecified type.
85    #[method]
86    pub fn encode_bytes_length(&mut self, bytes: *const c_void, length: UInt) {
87        unsafe { msg_send![self.m_self(), encodeBytes: bytes length: length] }
88    }
89
90    /// Encodes a buffer of data, given its length and a pointer, and associates it with a string key.
91    #[method]
92    pub fn encode_bytes_length_for_key(
93        &mut self,
94        bytes: *const UInt8,
95        length: UInt,
96        key: NSString,
97    ) {
98        unsafe { msg_send![self.m_self(), encodeBytes: bytes length: length forKey: key] }
99    }
100
101    /// An encoding method for subclasses to override to conditionally encode an object, preserving common references to it.
102    #[method]
103    pub fn encode_conditional_object<T>(&mut self, object: T)
104    where
105        T: PNSObject,
106    {
107        unsafe { msg_send![self.m_self(), encodeConditionalObject: object] }
108    }
109
110    /// An encoding method for subclasses to override to conditionally encode an object, preserving common references to it, only if it has been unconditionally encoded.
111    #[method]
112    pub fn encode_conditional_object_for_key<T>(&mut self, object: T, key: NSString)
113    where
114        T: PNSObject,
115    {
116        unsafe { msg_send![self.m_self(), encodeConditionalObject: object forKey: key] }
117    }
118
119    /// Encodes a given data object.
120    #[method]
121    pub fn encode_data_object<T>(&mut self, object: T)
122    where
123        T: PNSObject,
124    {
125        unsafe { msg_send![self.m_self(), encodeDataObject: object] }
126    }
127
128    /// Encodes a double-precision floating point value and associates it with the string key.
129    #[method]
130    pub fn encode_double_for_key(&mut self, value: c_double, key: NSString) {
131        unsafe { msg_send![self.m_self(), encodeDouble: value forKey: key] }
132    }
133
134    /// Encodes a floating point value and associates it with the string key.
135    #[method]
136    pub fn encode_float_for_key(&mut self, value: c_float, key: NSString) {
137        unsafe { msg_send![self.m_self(), encodeFloat: value forKey: key] }
138    }
139
140    /// Encodes a C integer value and associates it with the string key.
141    #[method]
142    pub fn encode_int_for_key(&mut self, value: Int, key: NSString) {
143        unsafe { msg_send![self.m_self(), encodeInt: value forKey: key] }
144    }
145
146    /// Encodes an integer value and associates it with the string key.
147    #[method]
148    pub fn encode_integer_for_key(&mut self, value: Int, key: NSString) {
149        unsafe { msg_send![self.m_self(), encodeInteger: value forKey: key] }
150    }
151
152    /// Encodes a 32-bit integer value and associates it with the string key.
153    #[method]
154    pub fn encode_int32_for_key(&mut self, value: Int32, key: NSString) {
155        unsafe { msg_send![self.m_self(), encodeInt32: value forKey: key] }
156    }
157
158    /// Encodes a 64-bit integer value and associates it with the string key.
159    #[method]
160    pub fn encode_int64_for_key(&mut self, value: Int64, key: NSString) {
161        unsafe { msg_send![self.m_self(), encodeInt64: value forKey: key] }
162    }
163
164    /// Encodes an object.
165    #[method]
166    pub fn encode_object<T>(&mut self, object: T)
167    where
168        T: PNSObject,
169    {
170        unsafe { msg_send![self.m_self(), encodeObject: object] }
171    }
172
173    /// Encodes an object and associates it with the string key.
174    #[method]
175    pub fn encode_object_for_key<T>(&mut self, object: T, key: NSString)
176    where
177        T: PNSObject,
178    {
179        unsafe { msg_send![self.m_self(), encodeObject: object forKey: key] }
180    }
181
182    /// Encodes a point.
183    #[method]
184    pub fn encode_point(&mut self, point: NSPoint) {
185        unsafe { msg_send![self.m_self(), encodePoint: point] }
186    }
187
188    /// Encodes a point and associates it with the string key.
189    #[method]
190    pub fn encode_point_for_key(&mut self, point: NSPoint, key: NSString) {
191        unsafe { msg_send![self.m_self(), encodePoint: point forKey: key] }
192    }
193
194    /// Encodes a property list.
195    #[method]
196    pub fn encode_property_list(&mut self, property_list: id) {
197        unsafe { msg_send![self.m_self(), encodePropertyList: property_list] }
198    }
199
200    /// Encodes a rectangle structure.
201    #[method]
202    pub fn encode_rect(&mut self, rect: NSRect) {
203        unsafe { msg_send![self.m_self(), encodeRect: rect] }
204    }
205
206    /// Encodes a rectangle structure and associates it with the string key.
207    #[method]
208    pub fn encode_rect_for_key(&mut self, rect: NSRect, key: NSString) {
209        unsafe { msg_send![self.m_self(), encodeRect: rect forKey: key] }
210    }
211
212    /// An encoding method for subclasses to override to encode an interconnected group of objects, starting with the provided root object.
213    #[method]
214    pub fn encode_root_object(&mut self, root_object: id) {
215        unsafe { msg_send![self.m_self(), encodeRootObject: root_object] }
216    }
217
218    /// Encodes a size structure.
219    #[method]
220    pub fn encode_size(&mut self, size: NSSize) {
221        unsafe { msg_send![self.m_self(), encodeSize: size] }
222    }
223
224    /// Encodes a size structure and associates it with the given string key.
225    #[method]
226    pub fn encode_size_for_key(&mut self, size: NSSize, key: NSString) {
227        unsafe { msg_send![self.m_self(), encodeSize: size forKey: key] }
228    }
229
230    /// Encodes a value of the given type at the given address.
231    #[method]
232    pub fn encode_value_of_objc_type_at(&mut self, _type: *const c_char, addr: *const c_void) {
233        unsafe { msg_send![self.m_self(), encodeValueOfObjCType: _type at: addr] }
234    }
235
236    /// Encodes a point and associates it with the specified key in the receiver’s archive.
237    #[method]
238    pub fn encode_cgpoint_for_key(&mut self, point: CGPoint, key: NSString) {
239        unsafe { msg_send![self.m_self(), encodeCGPoint: point forKey: key] }
240    }
241
242    /// Encodes a rectangle and associates it with the specified key in the receiver’s archive.
243    #[method]
244    pub fn encode_cgrect_for_key(&mut self, rect: CGRect, key: NSString) {
245        unsafe { msg_send![self.m_self(), encodeCGRect: rect forKey: key] }
246    }
247
248    /// Encodes size information and associates it with the specified key in the coder’s archive.
249    #[method]
250    pub fn encode_cgzize_for_key(&mut self, size: CGSize, key: NSString) {
251        unsafe { msg_send![self.m_self(), encodeCGSize: size forKey: key] }
252    }
253}