1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// ListBase.cs - List base class implementation
//
// Authors: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2002 Mike Kestner
// Copyright (c) 2005 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
namespace GLib {
using System;
using System.Collections;
using System.Runtime.InteropServices;
public abstract class ListBase : IDisposable, ICollection, GLib.IWrapper, ICloneable {
private IntPtr list_ptr = IntPtr.Zero;
private int length = -1;
private bool managed = false;
internal bool elements_owned = false;
protected System.Type element_type = null;
abstract internal IntPtr NthData (uint index);
abstract internal int Length (IntPtr list);
abstract internal void Free (IntPtr list);
abstract internal IntPtr Append (IntPtr current, IntPtr raw);
abstract internal IntPtr Prepend (IntPtr current, IntPtr raw);
internal ListBase (IntPtr list, System.Type element_type, bool owned, bool elements_owned)
{
list_ptr = list;
this.element_type = element_type;
managed = owned;
this.elements_owned = elements_owned;
}
~ListBase ()
{
Dispose (false);
}
[Obsolete ("Replaced by owned parameter on ctor.")]
public bool Managed {
set { managed = value; }
}
public IntPtr Handle {
get {
return list_ptr;
}
}
public void Append (IntPtr raw)
{
list_ptr = Append (list_ptr, raw);
}
public void Append (string item)
{
this.Append (Marshaller.StringToPtrGStrdup (item));
}
public void Append (object item)
{
this.Append (AllocNativeElement (item));
}
public void Prepend (IntPtr raw)
{
list_ptr = Prepend (list_ptr, raw);
}
// ICollection
public int Count {
get {
if (length == -1)
length = Length (list_ptr);
return length;
}
}
public object this [int index] {
get {
IntPtr data = NthData ((uint) index);
object ret = null;
ret = DataMarshal (data);
return ret;
}
}
// Synchronization could be tricky here. Hmm.
public bool IsSynchronized {
get { return false; }
}
public object SyncRoot {
get { return null; }
}
public void CopyTo (Array array, int index)
{
object[] orig = new object[Count];
int i = 0;
foreach (object o in this)
orig [i++] = o;
orig.CopyTo (array, index);
}
public class FilenameString {
private FilenameString () {}
}
IntPtr AllocNativeElement (object element)
{
if (element_type == null) {
if (element is IWrapper)
return (element as IWrapper).Handle;
else
return (IntPtr) GCHandle.Alloc (element);
} else {
if (element_type == typeof (string))
return Marshaller.StringToPtrGStrdup (element as string);
else if (element_type == typeof (FilenameString))
return Marshaller.StringToFilenamePtr (element as string);
else if (element_type == typeof (IntPtr))
return (IntPtr) GCHandle.Alloc (element);
else if (typeof (IWrapper).IsAssignableFrom (element_type))
return (element as IWrapper).Handle;
else if (element_type == typeof (int))
return new IntPtr ((int) element);
else if (element_type.IsValueType)
return Marshaller.StructureToPtrAlloc (element);
}
return IntPtr.Zero;
}
internal object DataMarshal (IntPtr data)
{
object ret = null;
if (element_type != null) {
if (element_type == typeof (string))
ret = Marshaller.Utf8PtrToString (data);
else if (element_type == typeof (FilenameString))
ret = Marshaller.FilenamePtrToString (data);
else if (element_type == typeof (IntPtr))
ret = data;
else if (element_type.IsSubclassOf (typeof (GLib.Object)))
ret = GLib.Object.GetObject (data, false);
else if (element_type.IsSubclassOf (typeof (GLib.Opaque)))
ret = GLib.Opaque.GetOpaque (data, element_type, elements_owned);
else if (element_type == typeof (int))
ret = (int) data;
else if (element_type.IsValueType)
ret = Marshal.PtrToStructure (data, element_type);
else if (element_type.IsInterface) {
Type adapter_type = element_type.Assembly.GetType (InterfaceToAdapterTypeName (element_type));
System.Reflection.MethodInfo method = adapter_type.GetMethod ("GetObject", new Type[] {typeof(IntPtr), typeof(bool)});
ret = method.Invoke (null, new object[] {data, false});
} else
ret = Activator.CreateInstance (element_type, new object[] {data});
} else if (Object.IsObject (data))
ret = GLib.Object.GetObject (data, false);
return ret;
}
static string InterfaceToAdapterTypeName (Type type)
{
string fullname = type.Namespace;
if (!String.IsNullOrEmpty (fullname)) {
fullname += ".";
}
fullname += type.Name.Substring (1); // IActivatable -> Activatable
return fullname + "Adapter";
}
[DllImport (Global.GObjectNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void g_object_unref (IntPtr item);
public void Empty ()
{
if (elements_owned) {
for (uint i = 0; i < Count; i++) {
if (typeof (GLib.Opaque).IsAssignableFrom (element_type)) {
GLib.Opaque.GetOpaque (NthData (i), element_type, true).Dispose ();
} else if (typeof (GLib.IWrapper).IsAssignableFrom (element_type)) {
g_object_unref (NthData (i));
} else {
Marshaller.Free (NthData (i));
}
}
}
if (managed)
FreeList ();
}
IntPtr GetData (IntPtr current)
{
// data field is at offset 0 for GList and GSList
return Marshal.ReadIntPtr (current);
}
IntPtr Next (IntPtr current)
{
// next field follows gpointer data field for GList and GSList
return Marshal.ReadIntPtr (current, IntPtr.Size);
}
private class ListEnumerator : IEnumerator
{
private IntPtr current = IntPtr.Zero;
private ListBase list;
public ListEnumerator (ListBase list)
{
this.list = list;
}
public object Current {
get {
IntPtr data = list.GetData (current);
object ret = null;
ret = list.DataMarshal (data);
return ret;
}
}
public bool MoveNext ()
{
if (current == IntPtr.Zero)
current = list.list_ptr;
else
current = list.Next (current);
return (current != IntPtr.Zero);
}
public void Reset ()
{
current = IntPtr.Zero;
}
}
// IEnumerable
public IEnumerator GetEnumerator ()
{
return new ListEnumerator (this);
}
// IDisposable
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
Empty ();
}
void FreeList ()
{
if (list_ptr != IntPtr.Zero)
Free (list_ptr);
list_ptr = IntPtr.Zero;
length = -1;
}
// ICloneable
abstract public object Clone ();
}
}
|