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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::{
    core::Arc,
    core_foundation::{sys, CFAllocator, CFComparisonResult, CFType},
};
use std::{
    cmp::Ordering,
    mem::{self, MaybeUninit},
    ptr,
};

mod type_;

pub use type_::*;

use super::{CFIndex, CFTypeID};

subclass! {
    /// A number object.
    ///
    /// Documentation:
    /// [Swift](https://developer.apple.com/documentation/corefoundation/cfnumber?language=swift) |
    /// [Objective-C](https://developer.apple.com/documentation/corefoundation/cfnumber?language=objc)
    #[derive(PartialEq, Hash)]
    pub class CFNumber: CFType<'static>;
}

#[cfg(feature = "foundation")]
cf_bridge!(CFNumber, crate::foundation::NSNumber);

impl Eq for CFNumber {}

impl PartialOrd for CFNumber {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for CFNumber {
    #[inline]
    #[doc(alias = "CFNumberCompare")]
    fn cmp(&self, other: &Self) -> Ordering {
        self.compare(other).into()
    }
}

impl From<i8> for Arc<CFNumber> {
    #[inline]
    fn from(value: i8) -> Self {
        unsafe { CFNumber::create(None, CFNumberType::I8, &value) }
    }
}

impl From<i16> for Arc<CFNumber> {
    #[inline]
    fn from(value: i16) -> Self {
        unsafe { CFNumber::create(None, CFNumberType::I16, &value) }
    }
}

impl From<i32> for Arc<CFNumber> {
    #[inline]
    fn from(value: i32) -> Self {
        unsafe { CFNumber::create(None, CFNumberType::I32, &value) }
    }
}

impl From<i64> for Arc<CFNumber> {
    #[inline]
    fn from(value: i64) -> Self {
        unsafe { CFNumber::create(None, CFNumberType::I64, &value) }
    }
}

impl From<isize> for Arc<CFNumber> {
    #[inline]
    fn from(value: isize) -> Self {
        if mem::size_of::<isize>() == 4 {
            (value as i32).into()
        } else {
            (value as i64).into()
        }
    }
}

impl From<f32> for Arc<CFNumber> {
    #[inline]
    fn from(value: f32) -> Self {
        unsafe { CFNumber::create(None, CFNumberType::F32, &value) }
    }
}

impl From<f64> for Arc<CFNumber> {
    #[inline]
    fn from(value: f64) -> Self {
        unsafe { CFNumber::create(None, CFNumberType::F64, &value) }
    }
}

impl CFNumber {
    /// Returns a reference to a
    /// [NaN (Not a Number)](https://en.wikipedia.org/wiki/NaN) value.
    ///
    /// This internally references
    /// [`kCFNumberNaN`](https://developer.apple.com/documentation/corefoundation/kCFNumberNaN).
    #[inline]
    #[doc(alias = "kCFNumberNaN")]
    pub fn nan() -> &'static Self {
        extern "C" {
            static kCFNumberNaN: &'static CFNumber;
        }
        unsafe { kCFNumberNaN }
    }

    /// Returns a reference to the infinity (∞) value.
    ///
    /// This internally references
    /// [`kCFNumberPositiveInfinity`](https://developer.apple.com/documentation/corefoundation/kcfnumberpositiveinfinity).
    #[inline]
    #[doc(alias = "kCFNumberPositiveInfinity")]
    pub fn infinity() -> &'static Self {
        extern "C" {
            static kCFNumberPositiveInfinity: &'static CFNumber;
        }
        unsafe { kCFNumberPositiveInfinity }
    }

    /// Returns a reference to the negative infinity (−∞) value.
    ///
    /// This internally references
    /// [`kCFNumberNegativeInfinity`](https://developer.apple.com/documentation/corefoundation/kcfnumbernegativeinfinity).
    #[inline]
    #[doc(alias = "kCFNumberNegativeInfinity")]
    pub fn neg_infinity() -> &'static Self {
        extern "C" {
            static kCFNumberNegativeInfinity: &'static CFNumber;
        }
        unsafe { kCFNumberNegativeInfinity }
    }
}

impl CFNumber {
    /// Returns the type identifier for `CFNumber`.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1541730-cfnumbergettypeid?language=objc).
    #[inline]
    #[doc(alias = "CFNumberGetTypeID")]
    pub fn type_id() -> CFTypeID {
        unsafe { sys::CFNumberGetTypeID() }
    }

    /// Creates a new `CFNumber` object using a specified value's `Into`
    /// implementation.
    #[inline]
    pub fn new<T>(value: T) -> Arc<Self>
    where
        T: Into<Arc<Self>>,
    {
        value.into()
    }

    /// Creates a new `CFNumber` object using a specified value.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1542182-cfnumbercreate?language=objc).
    ///
    /// # Safety
    ///
    /// `value` must represent a valid instance for the given `CFNumberType`.
    #[inline]
    #[doc(alias = "CFNumberCreate")]
    pub unsafe fn create<T>(
        allocator: Option<&CFAllocator>,
        number_type: CFNumberType,
        value: &T,
    ) -> Arc<Self> {
        Arc::from_raw(sys::CFNumberCreate(
            match allocator {
                Some(allocator) => allocator,
                None => ptr::null(),
            },
            number_type,
            (value as *const T).cast(),
        ))
    }

    /// Compares `self` to `other` and returns the result.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1542018-cfnumbercompare?language=objc).
    #[inline]
    #[doc(alias = "CFNumberCompare")]
    pub fn compare(&self, other: &Self) -> CFComparisonResult {
        unsafe { sys::CFNumberCompare(self, other, ptr::null_mut()) }
    }

    /// Returns the number of bytes used by this object to store its value.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1542080-cfnumbergetbytesize?language=objc).
    #[inline]
    #[doc(alias = "CFNumberGetByteSize")]
    pub fn byte_size(&self) -> CFIndex {
        unsafe { sys::CFNumberGetByteSize(self) }
    }

    /// Returns the type used by this object to store its value.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1543388-cfnumbergettype?language=objc).
    #[inline]
    #[doc(alias = "CFNumberGetType")]
    pub fn get_type(&self) -> CFNumberType {
        unsafe { sys::CFNumberGetType(self) }
    }

    /// Returns `true` if this object contains a value stored as one of the
    /// defined floating point types.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1543131-cfnumberisfloattype?language=objc).
    #[inline]
    #[doc(alias = "CFNumberIsFloatType")]
    pub fn is_float_type(&self) -> bool {
        unsafe { sys::CFNumberIsFloatType(self) != 0 }
    }
}

/// Getting number values.
///
/// All of these functions call
/// [`CFNumberGetValue`](https://developer.apple.com/documentation/corefoundation/1543114-cfnumbergetvalue?language=objc).
impl CFNumber {
    /// Returns the value of this object cast to a specified type.
    ///
    /// See [documentation](https://developer.apple.com/documentation/corefoundation/1543114-cfnumbergetvalue?language=objc).
    ///
    /// # Safety
    ///
    /// The generic type `T` must have a valid representation for the requested
    /// [`CFNumberType`].
    #[inline]
    #[doc(alias = "CFNumberGetValue")]
    pub unsafe fn get_value<T>(&self, number_type: CFNumberType) -> Option<T> {
        let mut value = MaybeUninit::<T>::uninit();

        if sys::CFNumberGetValue(self, number_type, value.as_mut_ptr().cast()) != 0 {
            Some(value.assume_init())
        } else {
            None
        }
    }

    /// Returns the value of this object cast to `i8`.
    #[inline]
    pub fn i8_value(&self) -> Option<i8> {
        unsafe { self.get_value(CFNumberType::I8) }
    }

    /// Returns the value of this object cast to `i16`.
    #[inline]
    pub fn i16_value(&self) -> Option<i16> {
        unsafe { self.get_value(CFNumberType::I16) }
    }

    /// Returns the value of this object cast to `i32`.
    #[inline]
    pub fn i32_value(&self) -> Option<i32> {
        unsafe { self.get_value(CFNumberType::I32) }
    }

    /// Returns the value of this object cast to `i64`.
    #[inline]
    pub fn i64_value(&self) -> Option<i64> {
        unsafe { self.get_value(CFNumberType::I64) }
    }

    /// Returns the value of this object cast to `isize`.
    #[inline]
    pub fn isize_value(&self) -> Option<isize> {
        if mem::size_of::<isize>() == 4 {
            Some(self.i32_value()? as isize)
        } else {
            Some(self.i64_value()? as isize)
        }
    }

    /// Returns the value of this object cast to `f32`.
    #[inline]
    pub fn f32_value(&self) -> Option<f32> {
        unsafe { self.get_value(CFNumberType::F32) }
    }

    /// Returns the value of this object cast to `f64`.
    #[inline]
    pub fn f64_value(&self) -> Option<f64> {
        unsafe { self.get_value(CFNumberType::F64) }
    }
}