2
2
// CFDictionary.cs: P/Invokes for CFDictionary, CFMutableDictionary
6
// Rolf Bjarne Kvinge (rolf@xamarin.com)
6
8
// Copyright 2010 Novell, Inc
9
// Copyright 2012 Xamarin Inc
8
11
// Permission is hereby granted, free of charge, to any person obtaining
9
12
// a copy of this software and associated documentation files (the
33
36
namespace MonoMac.CoreFoundation {
36
static class CFDictionary {
39
class CFDictionary : INativeObject, IDisposable {
40
public IntPtr Handle { get; private set; }
42
public static IntPtr KeyCallbacks;
43
public static IntPtr ValueCallbacks;
45
public CFDictionary (IntPtr handle)
46
: this (handle, false)
50
public CFDictionary (IntPtr handle, bool owns)
53
CFObject.CFRetain (handle);
62
public void Dispose ()
65
GC.SuppressFinalize (this);
68
public virtual void Dispose (bool disposing)
70
if (Handle != IntPtr.Zero){
71
CFObject.CFRelease (Handle);
76
static CFDictionary ()
78
var lib = Dlfcn.dlopen (Constants.CoreFoundationLibrary, 0);
80
KeyCallbacks = Dlfcn.GetIndirect (lib, "kCFTypeDictionaryKeyCallBacks");
81
ValueCallbacks = Dlfcn.GetIndirect (lib, "kCFTypeDictionaryValueCallBacks");
87
public static CFDictionary FromObjectAndKey (INativeObject obj, INativeObject key)
89
return new CFDictionary (CFDictionaryCreate (IntPtr.Zero, new IntPtr[] { key.Handle }, new IntPtr [] { obj.Handle }, 1, KeyCallbacks, ValueCallbacks), true);
92
public static CFDictionary FromObjectsAndKeys (INativeObject[] objects, INativeObject[] keys)
95
throw new ArgumentNullException ("objects");
98
throw new ArgumentNullException ("keys");
100
if (objects.Length != keys.Length)
101
throw new ArgumentException ("The length of both arrays must be the same");
103
IntPtr [] k = new IntPtr [keys.Length];
104
IntPtr [] v = new IntPtr [keys.Length];
106
for (int i = 0; i < k.Length; i++) {
107
k [i] = keys [i].Handle;
108
v [i] = objects [i].Handle;
111
return new CFDictionary (CFDictionaryCreate (IntPtr.Zero, k, v, k.Length, KeyCallbacks, ValueCallbacks), true);
114
[DllImport (Constants.CoreFoundationLibrary)]
115
extern static IntPtr CFDictionaryCreate (IntPtr allocator, IntPtr[] keys, IntPtr[] vals, int len, IntPtr keyCallbacks, IntPtr valCallbacks);
38
117
[DllImport (Constants.CoreFoundationLibrary)]
39
118
extern static IntPtr CFDictionaryGetValue (IntPtr theDict, IntPtr key);
40
119
public static IntPtr GetValue (IntPtr theDict, IntPtr key)
42
121
return CFDictionaryGetValue (theDict, key);
124
[DllImport (Constants.CoreFoundationLibrary)]
125
extern static int CFDictionaryGetCount (IntPtr theDict);
127
get { return CFDictionaryGetCount (Handle); }
130
[DllImport (Constants.CoreFoundationLibrary)]
131
extern static void CFDictionaryGetKeysAndValues (IntPtr theDict, IntPtr[] keys, IntPtr[] values);
132
public void GetKeysAndValues (out IntPtr [] keys, out IntPtr [] values)
134
int count = this.Count;
136
keys = new IntPtr [count];
137
values = new IntPtr [count];
138
CFDictionaryGetKeysAndValues (Handle, keys, values);
45
141
public static bool GetBooleanValue (IntPtr theDict, IntPtr key)
47
143
var value = GetValue (theDict, key);
50
146
return CFBoolean.GetValue (value);
149
public string GetStringValue (string key)
151
using (var str = new CFString (key)) {
152
return CFString.FetchString (CFDictionaryGetValue (Handle, str.handle));
156
public int GetInt32Value (string key)
159
using (var str = new CFString (key)) {
160
if (!CFNumberGetValue (CFDictionaryGetValue (Handle, str.Handle), /* kCFNumberSInt32Type */ 3, out value))
161
throw new System.Collections.Generic.KeyNotFoundException (string.Format ("Key {0} not found", key));
166
public long GetInt64Value (string key)
169
using (var str = new CFString (key)) {
170
if (!CFNumberGetValue (CFDictionaryGetValue (Handle, str.Handle), /* kCFNumberSInt64Type */ 4, out value))
171
throw new System.Collections.Generic.KeyNotFoundException (string.Format ("Key {0} not found", key));
176
public IntPtr GetIntPtrValue (string key)
178
using (var str = new CFString (key)) {
179
return CFDictionaryGetValue (Handle, str.handle);
183
public CFDictionary GetDictionaryValue (string key)
185
using (var str = new CFString (key)) {
186
var ptr = CFDictionaryGetValue (Handle, str.handle);
187
return ptr == IntPtr.Zero ? null : new CFDictionary (ptr);
191
public bool ContainsKey (string key)
193
using (var str = new CFString (key)) {
194
return CFDictionaryContainsKey (Handle, str.handle);
198
[DllImport (Constants.CoreFoundationLibrary)]
199
static extern bool CFNumberGetValue (IntPtr number, int theType, out int value);
201
[DllImport (Constants.CoreFoundationLibrary)]
202
static extern bool CFNumberGetValue (IntPtr number, int theType, out long value);
204
[DllImport (Constants.CoreFoundationLibrary)]
205
extern static bool CFDictionaryContainsKey (IntPtr theDict, IntPtr key);
54
208
static class CFMutableDictionary {