2005-06-02 16:16:28 +00:00
|
|
|
// ListBase.cs - List base class implementation
|
2002-09-01 04:46:38 +00:00
|
|
|
//
|
2005-06-02 16:16:28 +00:00
|
|
|
// Authors: Mike Kestner <mkestner@novell.com>
|
2002-09-01 04:46:38 +00:00
|
|
|
//
|
2004-06-25 18:42:19 +00:00
|
|
|
// Copyright (c) 2002 Mike Kestner
|
2005-06-02 16:16:28 +00:00
|
|
|
// Copyright (c) 2005 Novell, Inc.
|
2004-06-25 18:42:19 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2002-09-01 04:46:38 +00:00
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
2002-09-01 10:40:24 +00:00
|
|
|
public abstract class ListBase : IDisposable, ICollection, GLib.IWrapper, ICloneable {
|
2002-09-01 04:46:38 +00:00
|
|
|
|
|
|
|
private IntPtr list_ptr = IntPtr.Zero;
|
|
|
|
private int length = -1;
|
|
|
|
private bool managed = false;
|
2006-03-27 18:35:08 +00:00
|
|
|
internal bool elements_owned = false;
|
2003-02-24 06:39:30 +00:00
|
|
|
protected System.Type element_type = null;
|
2002-09-01 04:46:38 +00:00
|
|
|
|
2008-09-18 11:34:15 +00:00
|
|
|
abstract internal IntPtr NthData (uint index);
|
2002-09-01 04:46:38 +00:00
|
|
|
abstract internal IntPtr GetData (IntPtr current);
|
|
|
|
abstract internal IntPtr Next (IntPtr current);
|
|
|
|
abstract internal int Length (IntPtr list);
|
|
|
|
abstract internal void Free (IntPtr list);
|
2002-10-11 15:43:52 +00:00
|
|
|
abstract internal IntPtr Append (IntPtr current, IntPtr raw);
|
|
|
|
abstract internal IntPtr Prepend (IntPtr current, IntPtr raw);
|
2002-09-01 04:46:38 +00:00
|
|
|
|
2005-06-23 22:28:06 +00:00
|
|
|
internal ListBase (IntPtr list, System.Type element_type, bool owned, bool elements_owned)
|
2002-09-01 04:46:38 +00:00
|
|
|
{
|
|
|
|
list_ptr = list;
|
|
|
|
this.element_type = element_type;
|
2005-06-23 22:28:06 +00:00
|
|
|
managed = owned;
|
|
|
|
this.elements_owned = elements_owned;
|
2002-09-01 04:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~ListBase ()
|
|
|
|
{
|
2003-08-26 21:26:30 +00:00
|
|
|
Dispose (false);
|
2002-09-01 04:46:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-23 22:28:06 +00:00
|
|
|
[Obsolete ("Replaced by owned parameter on ctor.")]
|
2002-09-01 04:46:38 +00:00
|
|
|
public bool Managed {
|
|
|
|
set { managed = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntPtr Handle {
|
|
|
|
get {
|
|
|
|
return list_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-11 15:43:52 +00:00
|
|
|
public void Append (IntPtr raw)
|
|
|
|
{
|
|
|
|
list_ptr = Append (list_ptr, raw);
|
|
|
|
}
|
|
|
|
|
2003-08-19 21:07:24 +00:00
|
|
|
public void Append (string item)
|
|
|
|
{
|
2005-03-08 21:28:08 +00:00
|
|
|
this.Append (Marshaller.StringToPtrGStrdup (item));
|
2003-08-19 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 18:53:53 +00:00
|
|
|
public void Append (object item)
|
|
|
|
{
|
|
|
|
this.Append (AllocNativeElement (item));
|
|
|
|
}
|
|
|
|
|
2002-10-11 15:43:52 +00:00
|
|
|
public void Prepend (IntPtr raw)
|
|
|
|
{
|
|
|
|
list_ptr = Prepend (list_ptr, raw);
|
|
|
|
}
|
|
|
|
|
2002-09-01 04:46:38 +00:00
|
|
|
// ICollection
|
|
|
|
public int Count {
|
|
|
|
get {
|
|
|
|
if (length == -1)
|
|
|
|
length = Length (list_ptr);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-27 04:58:42 +00:00
|
|
|
public object this [int index] {
|
|
|
|
get {
|
|
|
|
IntPtr data = NthData ((uint) index);
|
|
|
|
object ret = null;
|
|
|
|
ret = DataMarshal (data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-01 04:46:38 +00:00
|
|
|
// 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)
|
2008-09-18 11:34:15 +00:00
|
|
|
orig [i++] = o;
|
2002-09-01 04:46:38 +00:00
|
|
|
|
|
|
|
orig.CopyTo (array, index);
|
|
|
|
}
|
|
|
|
|
2005-06-02 16:16:28 +00:00
|
|
|
public class FilenameString {
|
|
|
|
private FilenameString () {}
|
|
|
|
}
|
|
|
|
|
2007-08-14 18:53:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-10-27 04:58:42 +00:00
|
|
|
internal object DataMarshal (IntPtr data)
|
|
|
|
{
|
|
|
|
object ret = null;
|
|
|
|
if (element_type != null) {
|
|
|
|
if (element_type == typeof (string))
|
2005-03-08 21:28:08 +00:00
|
|
|
ret = Marshaller.Utf8PtrToString (data);
|
2005-06-02 16:16:28 +00:00
|
|
|
else if (element_type == typeof (FilenameString))
|
|
|
|
ret = Marshaller.FilenamePtrToString (data);
|
2005-06-09 14:27:48 +00:00
|
|
|
else if (element_type == typeof (IntPtr))
|
|
|
|
ret = data;
|
2005-06-08 17:29:48 +00:00
|
|
|
else if (element_type.IsSubclassOf (typeof (GLib.Object)))
|
|
|
|
ret = GLib.Object.GetObject (data, false);
|
2006-03-27 18:35:08 +00:00
|
|
|
else if (element_type.IsSubclassOf (typeof (GLib.Opaque)))
|
|
|
|
ret = GLib.Opaque.GetOpaque (data, element_type, elements_owned);
|
2003-10-27 04:58:42 +00:00
|
|
|
else if (element_type == typeof (int))
|
|
|
|
ret = (int) data;
|
|
|
|
else if (element_type.IsValueType)
|
|
|
|
ret = Marshal.PtrToStructure (data, element_type);
|
2008-10-27 15:08:02 +00:00
|
|
|
else if (element_type.IsInterface) {
|
|
|
|
Type adapter_type = element_type.Assembly.GetType (element_type.FullName + "Adapter");
|
|
|
|
System.Reflection.MethodInfo method = adapter_type.GetMethod ("GetObject", new Type[] {typeof(IntPtr), typeof(bool)});
|
|
|
|
ret = method.Invoke (null, new object[] {data, false});
|
|
|
|
} else
|
2003-10-27 04:58:42 +00:00
|
|
|
ret = Activator.CreateInstance (element_type, new object[] {data});
|
|
|
|
|
|
|
|
} else if (Object.IsObject (data))
|
|
|
|
ret = GLib.Object.GetObject (data, false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-11-04 19:29:01 +00:00
|
|
|
[DllImport ("libglib-2.0-0.dll")]
|
|
|
|
static extern void g_free (IntPtr item);
|
|
|
|
|
|
|
|
[DllImport ("libglib-2.0-0.dll")]
|
|
|
|
static extern void g_object_unref (IntPtr item);
|
|
|
|
|
|
|
|
public void Empty ()
|
|
|
|
{
|
2005-06-23 22:28:06 +00:00
|
|
|
if (elements_owned)
|
|
|
|
for (uint i = 0; i < Count; i++)
|
|
|
|
if (typeof (GLib.Object).IsAssignableFrom (element_type))
|
|
|
|
g_object_unref (NthData (i));
|
2006-03-27 18:35:08 +00:00
|
|
|
else if (typeof (GLib.Opaque).IsAssignableFrom (element_type))
|
|
|
|
GLib.Opaque.GetOpaque (NthData (i), element_type, true).Dispose ();
|
|
|
|
else
|
2005-06-23 22:28:06 +00:00
|
|
|
g_free (NthData (i));
|
|
|
|
|
|
|
|
if (managed)
|
|
|
|
FreeList ();
|
2004-11-04 19:29:01 +00:00
|
|
|
}
|
2003-10-27 04:58:42 +00:00
|
|
|
|
2002-09-01 04:46:38 +00:00
|
|
|
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;
|
2003-10-27 04:58:42 +00:00
|
|
|
ret = list.DataMarshal (data);
|
2002-09-01 04:46:38 +00:00
|
|
|
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 ()
|
2003-08-26 21:26:30 +00:00
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
2002-09-01 04:46:38 +00:00
|
|
|
{
|
2005-06-23 22:28:06 +00:00
|
|
|
Empty ();
|
2003-08-26 21:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeList ()
|
|
|
|
{
|
2002-09-01 04:46:38 +00:00
|
|
|
if (list_ptr != IntPtr.Zero)
|
|
|
|
Free (list_ptr);
|
|
|
|
list_ptr = IntPtr.Zero;
|
|
|
|
length = -1;
|
|
|
|
}
|
2003-08-26 21:26:30 +00:00
|
|
|
|
2002-09-01 04:46:38 +00:00
|
|
|
// ICloneable
|
|
|
|
abstract public object Clone ();
|
|
|
|
}
|
|
|
|
}
|