~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/CoreFoundation/CFDictionary.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// 
2
2
// CFDictionary.cs: P/Invokes for CFDictionary, CFMutableDictionary
3
3
//
4
 
// Authors: Mono Team
 
4
// Authors:
 
5
//    Mono Team
 
6
//    Rolf Bjarne Kvinge (rolf@xamarin.com)
5
7
//     
6
8
// Copyright 2010 Novell, Inc
 
9
// Copyright 2012 Xamarin Inc
7
10
//
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 {
34
37
 
35
38
        [Since (3,2)]
36
 
        static class CFDictionary {
37
 
 
 
39
        class CFDictionary : INativeObject, IDisposable {
 
40
                public IntPtr Handle { get; private set; }
 
41
        
 
42
                public static IntPtr KeyCallbacks;
 
43
                public static IntPtr ValueCallbacks;
 
44
 
 
45
                public CFDictionary (IntPtr handle)
 
46
                        : this (handle, false)
 
47
                {
 
48
                }
 
49
 
 
50
                public CFDictionary (IntPtr handle, bool owns)
 
51
                {
 
52
                        if (!owns)
 
53
                                CFObject.CFRetain (handle);
 
54
                        this.Handle = handle;
 
55
                }
 
56
                
 
57
                ~CFDictionary ()
 
58
                {
 
59
                        Dispose (false);
 
60
                }
 
61
 
 
62
                public void Dispose ()
 
63
                {
 
64
                        Dispose (true);
 
65
                        GC.SuppressFinalize (this);
 
66
                }
 
67
 
 
68
                public virtual void Dispose (bool disposing)
 
69
                {
 
70
                        if (Handle != IntPtr.Zero){
 
71
                                CFObject.CFRelease (Handle);
 
72
                                Handle = IntPtr.Zero;
 
73
                        }
 
74
                }
 
75
 
 
76
                static CFDictionary ()
 
77
                {
 
78
                        var lib = Dlfcn.dlopen (Constants.CoreFoundationLibrary, 0);
 
79
                        try {
 
80
                                KeyCallbacks = Dlfcn.GetIndirect (lib, "kCFTypeDictionaryKeyCallBacks");
 
81
                                ValueCallbacks = Dlfcn.GetIndirect (lib, "kCFTypeDictionaryValueCallBacks");
 
82
                        } finally {
 
83
                                Dlfcn.dlclose (lib);
 
84
                        }
 
85
                }
 
86
                
 
87
                public static CFDictionary FromObjectAndKey (INativeObject obj, INativeObject key)
 
88
                {
 
89
                        return new CFDictionary (CFDictionaryCreate (IntPtr.Zero, new IntPtr[] { key.Handle }, new IntPtr [] { obj.Handle }, 1, KeyCallbacks, ValueCallbacks), true);
 
90
                }
 
91
                
 
92
                public static CFDictionary FromObjectsAndKeys (INativeObject[] objects, INativeObject[] keys)
 
93
                {
 
94
                        if (objects == null)
 
95
                                throw new ArgumentNullException ("objects");
 
96
 
 
97
                        if (keys == null)
 
98
                                throw new ArgumentNullException ("keys");
 
99
 
 
100
                        if (objects.Length != keys.Length)
 
101
                                throw new ArgumentException ("The length of both arrays must be the same");
 
102
 
 
103
                        IntPtr [] k = new IntPtr [keys.Length];
 
104
                        IntPtr [] v = new IntPtr [keys.Length];
 
105
                        
 
106
                        for (int i = 0; i < k.Length; i++) {
 
107
                                k [i] = keys [i].Handle;
 
108
                                v [i] = objects [i].Handle;
 
109
                        }
 
110
 
 
111
                        return new CFDictionary (CFDictionaryCreate (IntPtr.Zero, k, v, k.Length, KeyCallbacks, ValueCallbacks), true);
 
112
                }
 
113
        
 
114
                [DllImport (Constants.CoreFoundationLibrary)]
 
115
                extern static IntPtr CFDictionaryCreate (IntPtr allocator, IntPtr[] keys, IntPtr[] vals, int len, IntPtr keyCallbacks, IntPtr valCallbacks);
 
116
                
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);
43
122
                }
44
123
 
 
124
                [DllImport (Constants.CoreFoundationLibrary)]
 
125
                extern static int CFDictionaryGetCount (IntPtr theDict);
 
126
                public int Count {
 
127
                        get { return CFDictionaryGetCount (Handle); }
 
128
                }
 
129
 
 
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)
 
133
                {
 
134
                        int count = this.Count;
 
135
 
 
136
                        keys = new IntPtr [count];
 
137
                        values = new IntPtr [count];
 
138
                        CFDictionaryGetKeysAndValues (Handle, keys, values);
 
139
                }
 
140
 
45
141
                public static bool GetBooleanValue (IntPtr theDict, IntPtr key)
46
142
                {
47
143
                        var value = GetValue (theDict, key);
49
145
                                return false;
50
146
                        return CFBoolean.GetValue (value);
51
147
                }
 
148
                
 
149
                public string GetStringValue (string key)
 
150
                {
 
151
                        using (var str = new CFString (key)) {
 
152
                                return CFString.FetchString (CFDictionaryGetValue (Handle, str.handle));
 
153
                        }
 
154
                }
 
155
 
 
156
                public int GetInt32Value (string key)
 
157
                {
 
158
                        int value = 0;
 
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));
 
162
                                return value;
 
163
                        }
 
164
                }
 
165
 
 
166
                public long GetInt64Value (string key)
 
167
                {
 
168
                        long value = 0;
 
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));
 
172
                                return value;
 
173
                        }
 
174
                }
 
175
 
 
176
                public IntPtr GetIntPtrValue (string key)
 
177
                {
 
178
                        using (var str = new CFString (key)) {
 
179
                                return CFDictionaryGetValue (Handle, str.handle);
 
180
                        }
 
181
                }
 
182
 
 
183
                public CFDictionary GetDictionaryValue (string key)
 
184
                {
 
185
                        using (var str = new CFString (key)) {
 
186
                                var ptr = CFDictionaryGetValue (Handle, str.handle);
 
187
                                return ptr == IntPtr.Zero ? null : new CFDictionary (ptr);
 
188
                        }
 
189
                }
 
190
 
 
191
                public bool ContainsKey (string key)
 
192
                {
 
193
                        using (var str = new CFString (key)) {
 
194
                                return CFDictionaryContainsKey (Handle, str.handle);
 
195
                        }
 
196
                }
 
197
 
 
198
                [DllImport (Constants.CoreFoundationLibrary)]
 
199
                static extern bool CFNumberGetValue (IntPtr number, int theType, out int value);
 
200
 
 
201
                [DllImport (Constants.CoreFoundationLibrary)]
 
202
                static extern bool CFNumberGetValue (IntPtr number, int theType, out long value);
 
203
 
 
204
                [DllImport (Constants.CoreFoundationLibrary)]
 
205
                extern static bool CFDictionaryContainsKey (IntPtr theDict, IntPtr key);
52
206
        }
53
207
 
54
208
        static class CFMutableDictionary {