2
// DictionaryContainer.cs: Foundation implementation for NSDictionary based setting classes
4
// Authors: Marek Safar (marek.safar@gmail.com)
6
// Copyright 2012 Xamarin Inc
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:
16
// The above copyright notice and this permission notice shall be
17
// included in all copies or substantial portions of the Software.
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.
30
using System.Runtime.InteropServices;
32
using MonoMac.CoreFoundation;
33
using MonoMac.ObjCRuntime;
35
namespace MonoMac.Foundation {
37
public abstract class DictionaryContainer
40
internal /*protected*/ DictionaryContainer ()
42
Dictionary = new NSMutableDictionary ();
45
internal /*protected*/ DictionaryContainer (NSDictionary dictionary)
47
if (dictionary == null)
48
throw new ArgumentNullException ("dictionary");
49
Dictionary = dictionary;
52
public NSDictionary Dictionary { get; private set; }
54
protected T[] GetArray<T> (NSString key) where T : NSObject
57
throw new ArgumentNullException ("key");
59
var value = CFDictionary.GetValue (Dictionary.Handle, key.Handle);
60
if (value == IntPtr.Zero)
63
return NSArray.ArrayFromHandle<T> (value);
66
protected T[] GetArray<T> (NSString key, Func<IntPtr, T> creator)
69
throw new ArgumentNullException ("key");
71
var value = CFDictionary.GetValue (Dictionary.Handle, key.Handle);
72
if (value == IntPtr.Zero)
75
return NSArray.ArrayFromHandleFunc<T> (value, creator);
78
protected int? GetInt32Value (NSString key)
81
throw new ArgumentNullException ("key");
84
if (!Dictionary.TryGetValue (key, out value))
87
return ((NSNumber) value).Int32Value;
90
protected long? GetLongValue (NSString key)
93
throw new ArgumentNullException ("key");
96
if (!Dictionary.TryGetValue (key, out value))
99
return ((NSNumber) value).Int64Value;
102
protected uint? GetUIntValue (NSString key)
105
throw new ArgumentNullException ("key");
108
if (!Dictionary.TryGetValue (key, out value))
111
return ((NSNumber) value).UInt32Value;
114
protected float? GetFloatValue (NSString key)
117
throw new ArgumentNullException ("key");
120
if (!Dictionary.TryGetValue (key, out value))
123
return ((NSNumber) value).FloatValue;
126
protected double? GetDoubleValue (NSString key)
129
throw new ArgumentNullException ("key");
132
if (!Dictionary.TryGetValue (key, out value))
135
return ((NSNumber) value).DoubleValue;
138
protected bool? GetBoolValue (NSString key)
141
throw new ArgumentNullException ("key");
143
var value = CFDictionary.GetValue (Dictionary.Handle, key.Handle);
144
if (value == IntPtr.Zero)
147
return CFBoolean.GetValue (value);
150
protected NSDictionary GetNSDictionary (NSString key)
153
throw new ArgumentNullException ("key");
156
Dictionary.TryGetValue (key, out value);
157
return value as NSDictionary;
160
protected string GetNSStringValue (NSString key)
163
throw new ArgumentNullException ("key");
166
Dictionary.TryGetValue (key, out value);
167
return value as NSString;
170
protected string GetStringValue (NSString key)
173
throw new ArgumentNullException ("key");
176
if (!Dictionary.TryGetValue (key, out value))
179
return CFString.FetchString (value.Handle);
182
protected string GetStringValue (string key)
185
throw new ArgumentNullException ("key");
187
using (var str = new CFString (key)) {
188
return CFString.FetchString (CFDictionary.GetValue (Dictionary.Handle, str.handle));
192
protected void SetArrayValue (NSString key, NSNumber[] values)
195
throw new ArgumentNullException ("key");
197
if (values == null) {
202
Dictionary [key] = NSArray.FromNSObjects (values);
205
protected void SetArrayValue (NSString key, string[] values)
208
throw new ArgumentNullException ("key");
210
if (values == null) {
215
Dictionary [key] = NSArray.FromStrings (values);
218
protected void SetArrayValue (NSString key, INativeObject[] values)
221
throw new ArgumentNullException ("key");
223
if (values == null) {
228
CFMutableDictionary.SetValue (Dictionary.Handle, key.Handle, CFArray.FromNativeObjects (values).Handle);
231
#region Sets CFBoolean value
233
protected void SetBooleanValue (NSString key, bool? value)
236
throw new ArgumentNullException ("key");
243
CFMutableDictionary.SetValue (Dictionary.Handle, key.Handle, value.Value ? CFBoolean.True.Handle : CFBoolean.False.Handle);
248
#region Sets NSNumber value
250
protected void SetNumberValue (NSString key, int? value)
253
throw new ArgumentNullException ("key");
260
Dictionary [key] = new NSNumber (value.Value);
263
protected void SetNumberValue (NSString key, uint? value)
266
throw new ArgumentNullException ("key");
273
Dictionary [key] = new NSNumber (value.Value);
276
protected void SetNumberValue (NSString key, long? value)
279
throw new ArgumentNullException ("key");
281
Dictionary [key] = new NSNumber (value.Value);
284
protected void SetNumberValue (NSString key, float? value)
287
throw new ArgumentNullException ("key");
294
Dictionary [key] = new NSNumber (value.Value);
297
protected void SetNumberValue (NSString key, double? value)
300
throw new ArgumentNullException ("key");
307
Dictionary [key] = new NSNumber (value.Value);
312
#region Sets NSString value
314
protected void SetStringValue (NSString key, string value)
316
SetStringValue (key, value == null ? (NSString) null : new NSString (value));
319
protected void SetStringValue (NSString key, NSString value)
322
throw new ArgumentNullException ("key");
327
Dictionary [key] = value;
333
#region Sets Native value
335
protected void SetNativeValue (NSString key, INativeObject value, bool removeNullValue = true)
338
throw new ArgumentNullException ("key");
340
if (value == null && removeNullValue) {
343
CFMutableDictionary.SetValue (Dictionary.Handle, key.Handle, value == null ? IntPtr.Zero : value.Handle);
349
protected void RemoveValue (NSString key)
352
throw new ArgumentNullException ("key");
354
((NSMutableDictionary) Dictionary).Remove (key);