From f956b098580d43eb0f756c79ad801973c1dcb1b3 Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Tue, 14 Aug 2007 18:53:53 +0000 Subject: [PATCH] 2007-08-14 Mike Kestner * glib/ListBase.cs : add AllocNativeElement method and an Append (object) method that uses it. * glib/List.cs : add object[] ctor using new append method. * glib/SList.cs : add object[] ctor using new append method. These are needed to return G(S)List* values as virtual method return values. svn path=/trunk/gtk-sharp/; revision=84112 --- ChangeLog | 9 +++++++++ glib/List.cs | 5 +++++ glib/ListBase.cs | 29 +++++++++++++++++++++++++++++ glib/SList.cs | 6 ++++++ 4 files changed, 49 insertions(+) diff --git a/ChangeLog b/ChangeLog index a7a7e74a6..4178999e1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2007-08-14 Mike Kestner + + * glib/ListBase.cs : add AllocNativeElement method and an + Append (object) method that uses it. + * glib/List.cs : add object[] ctor using new append method. + * glib/SList.cs : add object[] ctor using new append method. + These are needed to return G(S)List* values as virtual method + return values. + 2007-08-13 Mike Kestner * generator/MethodBody.cs : finally kill the s/out ref/ref/ hack. diff --git a/glib/List.cs b/glib/List.cs index 41d1c0dbf..44cac7212 100644 --- a/glib/List.cs +++ b/glib/List.cs @@ -99,5 +99,10 @@ namespace GLib { public List (IntPtr raw, System.Type element_type, bool owned, bool elements_owned) : base (raw, element_type, owned, elements_owned) {} + public List (object[] elements, System.Type element_type, bool owned, bool elements_owned) : this (IntPtr.Zero, element_type, owned, elements_owned) + { + foreach (object o in elements) + Append (o); + } } } diff --git a/glib/ListBase.cs b/glib/ListBase.cs index 51836aee9..141466565 100644 --- a/glib/ListBase.cs +++ b/glib/ListBase.cs @@ -76,6 +76,11 @@ namespace GLib { 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); @@ -122,6 +127,30 @@ namespace GLib { 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; diff --git a/glib/SList.cs b/glib/SList.cs index 1eab04691..92fce7fdc 100644 --- a/glib/SList.cs +++ b/glib/SList.cs @@ -99,5 +99,11 @@ namespace GLib { public SList (IntPtr raw, System.Type element_type) : this (raw, element_type, false, false) {} public SList (IntPtr raw, System.Type element_type, bool owned, bool elements_owned) : base (raw, element_type, false, false) {} + + public SList (object[] members, System.Type element_type, bool owned, bool elements_owned) : this (IntPtr.Zero, element_type, owned, elements_owned) + { + foreach (object o in members) + Append (o); + } } }