2
// CFArray.cs: P/Invokes for CFArray
6
// Rolf Bjarne Kvinge (rolf@xamarin.com)
9
// Copyright 2010 Novell, Inc
10
// Copyright 2012 Xamarin Inc
12
// Permission is hereby granted, free of charge, to any person obtaining
13
// a copy of this software and associated documentation files (the
14
// "Software"), to deal in the Software without restriction, including
15
// without limitation the rights to use, copy, modify, merge, publish,
16
// distribute, sublicense, and/or sell copies of the Software, and to
17
// permit persons to whom the Software is furnished to do so, subject to
18
// the following conditions:
20
// The above copyright notice and this permission notice shall be
21
// included in all copies or substantial portions of the Software.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
using System.Runtime.InteropServices;
34
using MonoMac.Foundation;
35
using MonoMac.ObjCRuntime;
37
using CFIndex = System.Int32;
39
namespace MonoMac.CoreFoundation {
41
class CFArray : INativeObject, IDisposable {
43
internal IntPtr handle;
45
internal CFArray (IntPtr handle)
46
: this (handle, false)
50
[Preserve (Conditional = true)]
51
internal CFArray (IntPtr handle, bool owns)
53
if (handle == IntPtr.Zero)
54
throw new ArgumentNullException ("handle");
58
CFObject.CFRetain (handle);
61
public IntPtr Handle {
65
[DllImport (Constants.CoreFoundationLibrary, EntryPoint="CFArrayGetTypeID")]
66
public extern static int GetTypeID ();
73
public void Dispose ()
76
GC.SuppressFinalize (this);
79
protected virtual void Dispose (bool disposing)
81
if (handle != IntPtr.Zero){
82
CFObject.CFRelease (handle);
87
// pointer to a const struct (REALLY APPLE?)
88
static readonly IntPtr kCFTypeArrayCallbacks_ptr;
92
var handle = Dlfcn.dlopen (Constants.CoreFoundationLibrary, 0);
93
if (handle == IntPtr.Zero)
96
kCFTypeArrayCallbacks_ptr = Dlfcn.GetIndirect (handle, "kCFTypeArrayCallBacks");
99
Dlfcn.dlclose (handle);
103
public static CFArray FromIntPtrs (params IntPtr[] values)
105
return new CFArray (Create (values), true);
108
public static CFArray FromNativeObjects (params INativeObject[] values)
110
return new CFArray (Create (values), true);
114
get {return CFArrayGetCount (handle);}
117
[DllImport (Constants.CoreFoundationLibrary)]
118
extern static IntPtr CFArrayCreate (IntPtr allocator, IntPtr values, CFIndex numValues, IntPtr callbacks);
120
[DllImport (Constants.CoreFoundationLibrary)]
121
extern static IntPtr CFArrayGetValueAtIndex (IntPtr theArray, IntPtr index);
123
public IntPtr GetValue (int index)
125
return CFArrayGetValueAtIndex (handle, new IntPtr (index));
128
public static unsafe IntPtr Create (params IntPtr[] values)
131
throw new ArgumentNullException ("values");
132
fixed (IntPtr* pv = values) {
133
return CFArrayCreate (IntPtr.Zero,
136
kCFTypeArrayCallbacks_ptr);
140
public static IntPtr Create (params INativeObject[] values)
143
throw new ArgumentNullException ("values");
144
IntPtr[] _values = new IntPtr [values.Length];
145
for (int i = 0; i < _values.Length; ++i)
146
_values [i] = values [i].Handle;
147
return Create (_values);
150
[DllImport (Constants.CoreFoundationLibrary)]
151
extern static CFIndex CFArrayGetCount (IntPtr theArray);
152
public static int GetCount (IntPtr array)
154
return CFArrayGetCount (array);