diff --git a/core-foundation-sys/src/array.rs b/core-foundation-sys/src/array.rs index 2574eca..8b0ef29 100644 --- a/core-foundation-sys/src/array.rs +++ b/core-foundation-sys/src/array.rs @@ -9,19 +9,13 @@ use libc::c_void; -use base::{CFRange, CFIndex, CFAllocatorRef, CFTypeID}; +use base::{CFRange, CFIndex, CFAllocatorRef, CFTypeID, Boolean}; +use string::CFStringRef; -/// FIXME(pcwalton): This is wrong. -pub type CFArrayRetainCallBack = *const u8; - -/// FIXME(pcwalton): This is wrong. -pub type CFArrayReleaseCallBack = *const u8; - -/// FIXME(pcwalton): This is wrong. -pub type CFArrayCopyDescriptionCallBack = *const u8; - -/// FIXME(pcwalton): This is wrong. -pub type CFArrayEqualCallBack = *const u8; +pub type CFArrayRetainCallBack = extern "C" fn(allocator: CFAllocatorRef, value: *const c_void) -> *const c_void; +pub type CFArrayReleaseCallBack = extern "C" fn(allocator: CFAllocatorRef, value: *const c_void); +pub type CFArrayCopyDescriptionCallBack = extern "C" fn(value: *const c_void) -> CFStringRef; +pub type CFArrayEqualCallBack = extern "C" fn(value1: *const c_void, value2: *const c_void) -> Boolean; #[repr(C)] #[derive(Clone, Copy)] diff --git a/core-foundation-sys/src/base.rs b/core-foundation-sys/src/base.rs index 42e2946..03f81ba 100644 --- a/core-foundation-sys/src/base.rs +++ b/core-foundation-sys/src/base.rs @@ -8,6 +8,7 @@ // except according to those terms. use libc::{c_uint, c_long, c_ulong, c_void, c_int}; +use string::CFStringRef; pub type Boolean = u8; pub type CFIndex = c_long; @@ -38,13 +39,35 @@ impl CFRange { } } +pub type CFAllocatorRetainCallBack = extern "C" fn(info: *mut c_void) -> *mut c_void; +pub type CFAllocatorReleaseCallBack = extern "C" fn(info: *mut c_void); +pub type CFAllocatorCopyDescriptionCallBack = extern "C" fn(info: *mut c_void) -> CFStringRef; +pub type CFAllocatorAllocateCallBack = extern "C" fn(allocSize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void; +pub type CFAllocatorReallocateCallBack = extern "C" fn(ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void; +pub type CFAllocatorDeallocateCallBack = extern "C" fn(ptr: *mut c_void, info: *mut c_void); +pub type CFAllocatorPreferredSizeCallBack = extern "C" fn(size: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> CFIndex; + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct CFAllocatorContext { + pub version: CFIndex, + pub info: *mut c_void, + pub retain: CFAllocatorRetainCallBack, + pub release: CFAllocatorReleaseCallBack, + pub copyDescription: CFAllocatorCopyDescriptionCallBack, + pub allocate: CFAllocatorAllocateCallBack, + pub reallocate: CFAllocatorReallocateCallBack, + pub deallocate: CFAllocatorDeallocateCallBack, + pub preferredSize: CFAllocatorPreferredSizeCallBack +} + extern { /* * CFBase.h */ /* CFAllocator Reference */ - // N.B. Many CFAllocator functions and constants are omitted here. + pub static kCFAllocatorDefault: CFAllocatorRef; pub static kCFAllocatorSystemDefault: CFAllocatorRef; pub static kCFAllocatorMalloc: CFAllocatorRef; @@ -52,6 +75,16 @@ extern { pub static kCFAllocatorNull: CFAllocatorRef; pub static kCFAllocatorUseContext: CFAllocatorRef; + pub fn CFAllocatorCreate(allocator: CFAllocatorRef, context: *mut CFAllocatorContext) -> CFAllocatorRef; + pub fn CFAllocatorAllocate(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> *mut c_void; + pub fn CFAllocatorDeallocate(allocator: CFAllocatorRef, ptr: *mut c_void); + pub fn CFAllocatorGetPreferredSizeForSize(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> CFIndex; + pub fn CFAllocatorReallocate(allocator: CFAllocatorRef, ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags) -> *mut c_void; + pub fn CFAllocatorGetDefault() -> CFAllocatorRef; + pub fn CFAllocatorSetDefault(allocator: CFAllocatorRef); + pub fn CFAllocatorGetContext(allocator: CFAllocatorRef, context: *mut CFAllocatorContext); + pub fn CFAllocatorGetTypeID() -> CFTypeID; + /* CFNull Reference */ pub static kCFNull: CFNullRef; diff --git a/core-foundation-sys/src/dictionary.rs b/core-foundation-sys/src/dictionary.rs index bf51bb1..6626cff 100644 --- a/core-foundation-sys/src/dictionary.rs +++ b/core-foundation-sys/src/dictionary.rs @@ -9,18 +9,17 @@ use libc::{c_void}; -use base::{CFAllocatorRef, CFIndex, CFTypeID, Boolean}; +use base::{CFAllocatorRef, CFHashCode, CFIndex, CFTypeID, Boolean}; +use string::CFStringRef; -pub type CFDictionaryApplierFunction = extern "C" fn (key: *const c_void, - value: *const c_void, - context: *mut c_void); -pub type CFDictionaryCopyDescriptionCallBack = *const u8; -pub type CFDictionaryEqualCallBack = *const u8; -pub type CFDictionaryHashCallBack = *const u8; -pub type CFDictionaryReleaseCallBack = *const u8; -pub type CFDictionaryRetainCallBack = *const u8; +pub type CFDictionaryApplierFunction = extern "C" fn(key: *const c_void, value: *const c_void, context: *mut c_void); + +pub type CFDictionaryRetainCallBack = extern "C" fn(allocator: CFAllocatorRef, value: *const c_void) -> *const c_void; +pub type CFDictionaryReleaseCallBack = extern "C" fn(allocator: CFAllocatorRef, value: *const c_void); +pub type CFDictionaryCopyDescriptionCallBack = extern "C" fn(value: *const c_void) -> CFStringRef; +pub type CFDictionaryEqualCallBack = extern "C" fn(value1: *const c_void, value2: *const c_void) -> Boolean; +pub type CFDictionaryHashCallBack = extern "C" fn(value: *const c_void) -> CFHashCode; -#[allow(dead_code)] #[repr(C)] #[derive(Clone, Copy)] pub struct CFDictionaryKeyCallBacks { @@ -32,7 +31,6 @@ pub struct CFDictionaryKeyCallBacks { pub hash: CFDictionaryHashCallBack } -#[allow(dead_code)] #[repr(C)] #[derive(Clone, Copy)] pub struct CFDictionaryValueCallBacks { diff --git a/core-foundation-sys/src/number.rs b/core-foundation-sys/src/number.rs index 3a5c09f..cca4e7a 100644 --- a/core-foundation-sys/src/number.rs +++ b/core-foundation-sys/src/number.rs @@ -23,7 +23,7 @@ pub type CFNumberType = u32; // static kCFNumberSInt16Type: CFNumberType = 2; pub static kCFNumberSInt32Type: CFNumberType = 3; pub static kCFNumberSInt64Type: CFNumberType = 4; -// static kCFNumberFloat32Type: CFNumberType = 5; +pub static kCFNumberFloat32Type: CFNumberType = 5; pub static kCFNumberFloat64Type: CFNumberType = 6; // static kCFNumberCharType: CFNumberType = 7; // static kCFNumberShortType: CFNumberType = 8; diff --git a/core-foundation-sys/src/string.rs b/core-foundation-sys/src/string.rs index 6095bca..bdb1be7 100644 --- a/core-foundation-sys/src/string.rs +++ b/core-foundation-sys/src/string.rs @@ -209,8 +209,7 @@ extern { bytes: *const u8, numBytes: CFIndex, encoding: CFStringEncoding, - isExternalRepresentation: Boolean, - contentsDeallocator: CFAllocatorRef) + isExternalRepresentation: Boolean) -> CFStringRef; pub fn CFStringCreateWithBytesNoCopy(alloc: CFAllocatorRef, bytes: *const u8, diff --git a/core-foundation/src/base.rs b/core-foundation/src/base.rs index 3f4bcea..5e55b8b 100644 --- a/core-foundation/src/base.rs +++ b/core-foundation/src/base.rs @@ -46,6 +46,29 @@ impl Drop for CFType { } } +/// An allocator for Core Foundation objects. +pub struct CFAllocator(CFAllocatorRef); + +impl Drop for CFAllocator { + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl_TCFType!(CFAllocator, CFAllocatorRef, CFAllocatorGetTypeID); + +impl CFAllocator { + #[inline] + pub fn new(mut context: CFAllocatorContext) -> CFAllocator { + unsafe { + let allocator_ref = CFAllocatorCreate(kCFAllocatorDefault, &mut context); + TCFType::wrap_under_create_rule(allocator_ref) + } + } +} + /// All Core Foundation types implement this trait. The type parameter `TypeRef` specifies the /// associated Core Foundation type: e.g. for `CFType` this is `CFTypeRef`; for `CFArray` this is /// `CFArrayRef`. diff --git a/core-foundation/src/number.rs b/core-foundation/src/number.rs index 771c75e..b16d90b 100644 --- a/core-foundation/src/number.rs +++ b/core-foundation/src/number.rs @@ -28,7 +28,6 @@ impl Drop for CFNumber { impl_TCFType!(CFNumber, CFNumberRef, CFNumberGetTypeID); -// TODO(pcwalton): Floating point. impl CFNumber { #[inline] pub fn from_i32(value: i32) -> CFNumber { @@ -49,6 +48,15 @@ impl CFNumber { } } + #[inline] + pub fn to_f32(&self) -> Option { + unsafe { + let mut value: f32 = 0.0; + let ok = CFNumberGetValue(self.0, kCFNumberFloat32Type, mem::transmute(&mut value)); + if ok { Some(value) } else { None } + } + } + #[inline] pub fn to_f64(&self) -> Option { unsafe { @@ -68,6 +76,16 @@ impl CFNumber { } } + #[inline] + pub fn from_f32(value: f32) -> CFNumber { + unsafe { + let number_ref = CFNumberCreate(kCFAllocatorDefault, + kCFNumberFloat32Type, + mem::transmute(&value)); + TCFType::wrap_under_create_rule(number_ref) + } + } + #[inline] pub fn from_f64(value: f64) -> CFNumber { unsafe { diff --git a/core-foundation/src/string.rs b/core-foundation/src/string.rs index 051ca9a..aa7f313 100644 --- a/core-foundation/src/string.rs +++ b/core-foundation/src/string.rs @@ -113,8 +113,7 @@ impl CFString { string.as_ptr(), string.len().to_CFIndex(), kCFStringEncodingUTF8, - false as Boolean, - kCFAllocatorNull); + false as Boolean); CFString::wrap_under_create_rule(string_ref) } }