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

« back to all changes in this revision

Viewing changes to external/maccore/src/Foundation/DictionaryContainer.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
// 
 
2
// DictionaryContainer.cs: Foundation implementation for NSDictionary based setting classes
 
3
//
 
4
// Authors: Marek Safar (marek.safar@gmail.com)
 
5
//     
 
6
// Copyright 2012 Xamarin Inc
 
7
//
 
8
// Permission is hereby granted, free of charge, to any person obtaining
 
9
// a copy of this software and associated documentation files (the
 
10
// "Software"), to deal in the Software without restriction, including
 
11
// without limitation the rights to use, copy, modify, merge, publish,
 
12
// distribute, sublicense, and/or sell copies of the Software, and to
 
13
// permit persons to whom the Software is furnished to do so, subject to
 
14
// the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be
 
17
// included in all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
20
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
21
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
22
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
23
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
24
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
25
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
26
//
 
27
//
 
28
 
 
29
using System;
 
30
using System.Runtime.InteropServices;
 
31
 
 
32
using MonoMac.CoreFoundation;
 
33
using MonoMac.ObjCRuntime;
 
34
 
 
35
namespace MonoMac.Foundation {
 
36
 
 
37
        public abstract class DictionaryContainer
 
38
        {
 
39
#if !COREBUILD
 
40
                internal /*protected*/ DictionaryContainer ()
 
41
                {
 
42
                        Dictionary = new NSMutableDictionary ();
 
43
                }
 
44
 
 
45
                internal /*protected*/ DictionaryContainer (NSDictionary dictionary)
 
46
                {
 
47
                        if (dictionary == null)
 
48
                                throw new ArgumentNullException ("dictionary");
 
49
                        Dictionary = dictionary;
 
50
                }
 
51
                
 
52
                public NSDictionary Dictionary { get; private set; }
 
53
 
 
54
                protected T[] GetArray<T> (NSString key) where T : NSObject
 
55
                {
 
56
                        if (key == null)
 
57
                                throw new ArgumentNullException ("key");
 
58
 
 
59
                        var value = CFDictionary.GetValue (Dictionary.Handle, key.Handle);
 
60
                        if (value == IntPtr.Zero)
 
61
                                return null;
 
62
 
 
63
                        return NSArray.ArrayFromHandle<T> (value);
 
64
                }
 
65
 
 
66
                protected T[] GetArray<T> (NSString key, Func<IntPtr, T> creator)
 
67
                {
 
68
                        if (key == null)
 
69
                                throw new ArgumentNullException ("key");
 
70
 
 
71
                        var value = CFDictionary.GetValue (Dictionary.Handle, key.Handle);
 
72
                        if (value == IntPtr.Zero)
 
73
                                return null;
 
74
 
 
75
                        return NSArray.ArrayFromHandleFunc<T> (value, creator);
 
76
                }
 
77
 
 
78
                protected int? GetInt32Value (NSString key)
 
79
                {
 
80
                        if (key == null)
 
81
                                throw new ArgumentNullException ("key");
 
82
 
 
83
                        NSObject value;
 
84
                        if (!Dictionary.TryGetValue (key, out value))
 
85
                                return null;
 
86
 
 
87
                        return ((NSNumber) value).Int32Value;
 
88
                }
 
89
 
 
90
                protected long? GetLongValue (NSString key)
 
91
                {
 
92
                        if (key == null)
 
93
                                throw new ArgumentNullException ("key");
 
94
 
 
95
                        NSObject value;
 
96
                        if (!Dictionary.TryGetValue (key, out value))
 
97
                                return null;
 
98
 
 
99
                        return ((NSNumber) value).Int64Value;
 
100
                }
 
101
 
 
102
                protected uint? GetUIntValue (NSString key)
 
103
                {
 
104
                        if (key == null)
 
105
                                throw new ArgumentNullException ("key");
 
106
 
 
107
                        NSObject value;
 
108
                        if (!Dictionary.TryGetValue (key, out value))
 
109
                                return null;
 
110
 
 
111
                        return ((NSNumber) value).UInt32Value;
 
112
                }
 
113
 
 
114
                protected float? GetFloatValue (NSString key)
 
115
                {
 
116
                        if (key == null)
 
117
                                throw new ArgumentNullException ("key");
 
118
 
 
119
                        NSObject value;
 
120
                        if (!Dictionary.TryGetValue (key, out value))
 
121
                                return null;
 
122
 
 
123
                        return ((NSNumber) value).FloatValue;
 
124
                }
 
125
 
 
126
                protected double? GetDoubleValue (NSString key)
 
127
                {
 
128
                        if (key == null)
 
129
                                throw new ArgumentNullException ("key");
 
130
 
 
131
                        NSObject value;
 
132
                        if (!Dictionary.TryGetValue (key, out value))
 
133
                                return null;
 
134
 
 
135
                        return ((NSNumber) value).DoubleValue;
 
136
                }
 
137
 
 
138
                protected bool? GetBoolValue (NSString key)
 
139
                {
 
140
                        if (key == null)
 
141
                                throw new ArgumentNullException ("key");
 
142
 
 
143
                        var value = CFDictionary.GetValue (Dictionary.Handle, key.Handle);
 
144
                        if (value == IntPtr.Zero)
 
145
                                return null;
 
146
 
 
147
                        return CFBoolean.GetValue (value);
 
148
                }
 
149
 
 
150
                protected NSDictionary GetNSDictionary (NSString key)
 
151
                {
 
152
                        if (key == null)
 
153
                                throw new ArgumentNullException ("key");
 
154
 
 
155
                        NSObject value;
 
156
                        Dictionary.TryGetValue (key, out value);
 
157
                        return value as NSDictionary;
 
158
                }
 
159
 
 
160
                protected string GetNSStringValue (NSString key)
 
161
                {
 
162
                        if (key == null)
 
163
                                throw new ArgumentNullException ("key");
 
164
 
 
165
                        NSObject value;
 
166
                        Dictionary.TryGetValue (key, out value);
 
167
                        return value as NSString;
 
168
                }
 
169
 
 
170
                protected string GetStringValue (NSString key)
 
171
                {
 
172
                        if (key == null)
 
173
                                throw new ArgumentNullException ("key");
 
174
 
 
175
                        NSObject value;
 
176
                        if (!Dictionary.TryGetValue (key, out value))
 
177
                                return null;
 
178
                        
 
179
                        return CFString.FetchString (value.Handle);
 
180
                }
 
181
 
 
182
                protected string GetStringValue (string key)
 
183
                {
 
184
                        if (key == null)
 
185
                                throw new ArgumentNullException ("key");
 
186
 
 
187
                        using (var str = new CFString (key)) {
 
188
                                return CFString.FetchString (CFDictionary.GetValue (Dictionary.Handle, str.handle));
 
189
                        }
 
190
                }               
 
191
 
 
192
                protected void SetArrayValue (NSString key, NSNumber[] values)
 
193
                {
 
194
                        if (key == null)
 
195
                                throw new ArgumentNullException ("key");
 
196
 
 
197
                        if (values == null) {
 
198
                                RemoveValue (key);
 
199
                                return;
 
200
                        }
 
201
 
 
202
                        Dictionary [key] = NSArray.FromNSObjects (values);
 
203
                }
 
204
 
 
205
                protected void SetArrayValue (NSString key, string[] values)
 
206
                {
 
207
                        if (key == null)
 
208
                                throw new ArgumentNullException ("key");
 
209
 
 
210
                        if (values == null) {
 
211
                                RemoveValue (key);
 
212
                                return;
 
213
                        }
 
214
 
 
215
                        Dictionary [key] = NSArray.FromStrings (values);
 
216
                }
 
217
 
 
218
                protected void SetArrayValue (NSString key, INativeObject[] values)
 
219
                {
 
220
                        if (key == null)
 
221
                                throw new ArgumentNullException ("key");
 
222
 
 
223
                        if (values == null) {
 
224
                                RemoveValue (key);
 
225
                                return;                 
 
226
                        }
 
227
 
 
228
                        CFMutableDictionary.SetValue (Dictionary.Handle, key.Handle, CFArray.FromNativeObjects (values).Handle);
 
229
                }
 
230
 
 
231
                #region Sets CFBoolean value
 
232
 
 
233
                protected void SetBooleanValue (NSString key, bool? value)
 
234
                {
 
235
                        if (key == null)
 
236
                                throw new ArgumentNullException ("key");
 
237
                        
 
238
                        if (value == null) {
 
239
                                RemoveValue (key);
 
240
                                return;
 
241
                        }
 
242
 
 
243
                        CFMutableDictionary.SetValue (Dictionary.Handle, key.Handle, value.Value ? CFBoolean.True.Handle : CFBoolean.False.Handle);                     
 
244
                }
 
245
 
 
246
                #endregion
 
247
 
 
248
                #region Sets NSNumber value
 
249
 
 
250
                protected void SetNumberValue (NSString key, int? value)
 
251
                {
 
252
                        if (key == null)
 
253
                                throw new ArgumentNullException ("key");
 
254
 
 
255
                        if (value == null) {
 
256
                                RemoveValue (key);
 
257
                                return;
 
258
                        }
 
259
 
 
260
                        Dictionary [key] = new NSNumber (value.Value);                          
 
261
                }
 
262
 
 
263
                protected void SetNumberValue (NSString key, uint? value)
 
264
                {
 
265
                        if (key == null)
 
266
                                throw new ArgumentNullException ("key");
 
267
 
 
268
                        if (value == null) {
 
269
                                RemoveValue (key);
 
270
                                return;
 
271
                        }
 
272
 
 
273
                        Dictionary [key] = new NSNumber (value.Value);                          
 
274
                }
 
275
 
 
276
                protected void SetNumberValue (NSString key, long? value)
 
277
                {
 
278
                        if (key == null)
 
279
                                throw new ArgumentNullException ("key");
 
280
 
 
281
                        Dictionary [key] = new NSNumber (value.Value);                          
 
282
                }
 
283
 
 
284
                protected void SetNumberValue (NSString key, float? value)
 
285
                {
 
286
                        if (key == null)
 
287
                                throw new ArgumentNullException ("key");
 
288
 
 
289
                        if (value == null) {
 
290
                                RemoveValue (key);
 
291
                                return;
 
292
                        }
 
293
 
 
294
                        Dictionary [key] = new NSNumber (value.Value);                          
 
295
                }
 
296
 
 
297
                protected void SetNumberValue (NSString key, double? value)
 
298
                {
 
299
                        if (key == null)
 
300
                                throw new ArgumentNullException ("key");
 
301
 
 
302
                        if (value == null) {
 
303
                                RemoveValue (key);
 
304
                                return;
 
305
                        }
 
306
 
 
307
                        Dictionary [key] = new NSNumber (value.Value);                          
 
308
                }
 
309
 
 
310
                #endregion
 
311
 
 
312
                #region Sets NSString value
 
313
 
 
314
                protected void SetStringValue (NSString key, string value)
 
315
                {
 
316
                        SetStringValue (key, value == null ? (NSString) null : new NSString (value));
 
317
                }
 
318
 
 
319
                protected void SetStringValue (NSString key, NSString value)
 
320
                {
 
321
                        if (key == null)
 
322
                                throw new ArgumentNullException ("key");
 
323
 
 
324
                        if (value == null) {
 
325
                                RemoveValue (key);
 
326
                        } else {
 
327
                                Dictionary [key] = value;
 
328
                        }                       
 
329
                }
 
330
 
 
331
                #endregion
 
332
 
 
333
                #region Sets Native value
 
334
 
 
335
                protected void SetNativeValue (NSString key, INativeObject value, bool removeNullValue = true)
 
336
                {
 
337
                        if (key == null)
 
338
                                throw new ArgumentNullException ("key");
 
339
 
 
340
                        if (value == null && removeNullValue) {
 
341
                                RemoveValue (key);
 
342
                        } else {
 
343
                                CFMutableDictionary.SetValue (Dictionary.Handle, key.Handle, value == null ? IntPtr.Zero : value.Handle);
 
344
                        }                       
 
345
                }
 
346
 
 
347
                #endregion
 
348
 
 
349
                protected void RemoveValue (NSString key)
 
350
                {
 
351
                        if (key == null)
 
352
                                throw new ArgumentNullException ("key");
 
353
 
 
354
                        ((NSMutableDictionary) Dictionary).Remove (key);
 
355
                }
 
356
#endif
 
357
        }
 
358
}