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
pub use core_foundation_sys::dictionary::*;
use core_foundation_sys::base::CFRelease;
use core_foundation_sys::base::{CFTypeRef, kCFAllocatorDefault};
use libc::c_void;
use std::mem;
use std::ptr;
use base::{CFType, CFIndexConvertible, TCFType};
pub struct CFDictionary(CFDictionaryRef);
impl Drop for CFDictionary {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl_TCFType!(CFDictionary, CFDictionaryRef, CFDictionaryGetTypeID);
impl_CFTypeDescription!(CFDictionary);
impl CFDictionary {
pub fn from_CFType_pairs<R1, R2, K, V>(pairs: &[(K, V)]) -> CFDictionary
where K: TCFType<R1>, V: TCFType<R2> {
let (keys, values): (Vec<CFTypeRef>,Vec<CFTypeRef>) =
pairs.iter()
.map(|&(ref key, ref value)| (key.as_CFTypeRef(), value.as_CFTypeRef()))
.unzip();
unsafe {
let dictionary_ref = CFDictionaryCreate(kCFAllocatorDefault,
mem::transmute(keys.as_ptr()),
mem::transmute(values.as_ptr()),
keys.len().to_CFIndex(),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
TCFType::wrap_under_create_rule(dictionary_ref)
}
}
#[inline]
pub fn len(&self) -> usize {
unsafe {
CFDictionaryGetCount(self.0) as usize
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn contains_key(&self, key: *const c_void) -> bool {
unsafe {
CFDictionaryContainsKey(self.0, key) != 0
}
}
#[inline]
pub fn contains_key2<X, K: TCFType<*const X>>(&self, key: &K) -> bool {
self.contains_key(key.as_concrete_TypeRef() as *const c_void)
}
#[inline]
pub fn find(&self, key: *const c_void) -> Option<*const c_void> {
unsafe {
let mut value: *const c_void = ptr::null();
if CFDictionaryGetValueIfPresent(self.0, key, &mut value) != 0 {
Some(value)
} else {
None
}
}
}
#[inline]
pub fn find2<X, K: TCFType<*const X>>(&self, key: &K) -> Option<*const c_void> {
self.find(key.as_concrete_TypeRef() as *const c_void)
}
#[inline]
pub fn get(&self, key: *const c_void) -> *const c_void {
self.find(key).expect(&format!("No entry found for key {:p}", key))
}
#[inline]
pub unsafe fn get_CFType(&self, key: *const c_void) -> CFType {
let value: CFTypeRef = mem::transmute(self.get(key));
TCFType::wrap_under_get_rule(value)
}
#[inline]
pub unsafe fn set_value(&self, key: *const c_void, value: *const c_void) {
CFDictionarySetValue(self.0, key, value)
}
pub fn get_keys_and_values(&self) -> (Vec<*const c_void>, Vec<*const c_void>) {
let length = self.len();
let mut keys = Vec::with_capacity(length);
let mut values = Vec::with_capacity(length);
unsafe {
CFDictionaryGetKeysAndValues(self.0, keys.as_mut_ptr(), values.as_mut_ptr());
keys.set_len(length);
values.set_len(length);
}
(keys, values)
}
}