From 737717e5a51ba9ce63885127960001ae6b0ebfb6 Mon Sep 17 00:00:00 2001 From: Daniel Kornhauser Eisenberg Date: Mon, 27 Oct 2003 04:58:42 +0000 Subject: [PATCH] 2003-10-27 Daniel Kornhauser Eisenberg * glib/ListBase.cs : Added Indexer to allow [] based adressing * glib/List.cs : Added DllImport for Indexer * glib/SList.cs : Added DllImport for Indexer svn path=/trunk/gtk-sharp/; revision=19420 --- glib/List.cs | 9 +++++++++ glib/ListBase.cs | 45 +++++++++++++++++++++++++++++++-------------- glib/SList.cs | 9 +++++++++ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/glib/List.cs b/glib/List.cs index 154dffcfe..7067fb69b 100644 --- a/glib/List.cs +++ b/glib/List.cs @@ -76,6 +76,15 @@ namespace GLib { return g_list_prepend (list, raw); } + [DllImport("libglib-2.0-0.dll")] + static extern IntPtr g_list_nth_data (IntPtr l, uint n); + + internal override IntPtr NthData (uint n) + { + return g_list_nth_data (Handle, n); + } + + public List (IntPtr raw) : base (raw) { } diff --git a/glib/ListBase.cs b/glib/ListBase.cs index a5bf67b9b..1d1077747 100644 --- a/glib/ListBase.cs +++ b/glib/ListBase.cs @@ -25,6 +25,7 @@ namespace GLib { private bool managed = false; protected System.Type element_type = null; + abstract internal IntPtr NthData (uint index); abstract internal IntPtr GetData (IntPtr current); abstract internal IntPtr Next (IntPtr current); abstract internal int Length (IntPtr list); @@ -106,6 +107,15 @@ namespace GLib { } } + 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; } @@ -125,6 +135,26 @@ namespace GLib { orig.CopyTo (array, index); } + internal object DataMarshal (IntPtr data) + { + object ret = null; + if (element_type != null) { + if (element_type == typeof (string)) + ret = Marshal.PtrToStringAnsi (data); + else if (element_type == typeof (int)) + ret = (int) data; + else if (element_type.IsValueType) + ret = Marshal.PtrToStructure (data, element_type); + else + ret = Activator.CreateInstance (element_type, new object[] {data}); + + } else if (Object.IsObject (data)) + ret = GLib.Object.GetObject (data, false); + + return ret; + } + + private class ListEnumerator : IEnumerator { private IntPtr current = IntPtr.Zero; @@ -139,20 +169,7 @@ namespace GLib { get { IntPtr data = list.GetData (current); object ret = null; - if (list.element_type != null) - { - if (list.element_type == typeof (string)) - ret = Marshal.PtrToStringAnsi (data); - else if (list.element_type == typeof (int)) - ret = (int) data; - else if (list.element_type.IsValueType) - ret = Marshal.PtrToStructure (data, list.element_type); - else - ret = Activator.CreateInstance (list.element_type, new object[] {data}); - } - else if (Object.IsObject (data)) - ret = GLib.Object.GetObject (data, false); - + ret = list.DataMarshal (data); return ret; } } diff --git a/glib/SList.cs b/glib/SList.cs index 460a1adf8..d82f345b4 100644 --- a/glib/SList.cs +++ b/glib/SList.cs @@ -76,6 +76,15 @@ namespace GLib { return g_slist_prepend (list, raw); } + + [DllImport("libglib-2.0-0.dll")] + static extern IntPtr g_slist_nth_data (IntPtr l, uint n); + + internal override IntPtr NthData (uint n) + { + return g_slist_nth_data (Handle, n); + } + public SList (IntPtr raw) : base (raw) { }