From b5c00547873ca8d0da17be1d26a0a58d382dea67 Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Fri, 19 Apr 2002 16:17:47 +0000 Subject: [PATCH] 2002-04-19 Mike Kestner * glib/SList.cs : A more sane approach. * glib/Value.cs : Marshal strings directly with pinvoke svn path=/trunk/gtk-sharp/; revision=3913 --- ChangeLog | 5 + glib/SList.cs | 293 +++----------------------------------------------- glib/Value.cs | 12 +-- 3 files changed, 23 insertions(+), 287 deletions(-) diff --git a/ChangeLog b/ChangeLog index 21ff453aa..4efa03d1e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-04-19 Mike Kestner + + * glib/SList.cs : A more sane approach. + * glib/Value.cs : Marshal strings directly with pinvoke + 2002-04-18 Joe Shaw * */makefile: Allow a different MCS to be passed in on the make diff --git a/glib/SList.cs b/glib/SList.cs index b7a6dc6cd..43cac08ed 100644 --- a/glib/SList.cs +++ b/glib/SList.cs @@ -18,297 +18,30 @@ namespace GLib { /// Wrapper class for GSList. /// - public class SList : IList { - - // Private class and instance members - IntPtr _list; - int rev_cnt; - - /// - /// Object Constructor - /// - /// - /// - /// Dummy constructor needed for derived classes. - /// - - public SList () - { - _list = IntPtr.Zero; - rev_cnt = 0; - } + public class SList : ArrayList { /// /// Handle Property /// /// /// - /// The raw GSList reference. There should be no need to - /// use this other than marshaling situations. + /// A raw GSList reference for marshaling situations. /// + [DllImport("gobject-2.0")] + static extern IntPtr g_slist_append(IntPtr l, IntPtr d); + public IntPtr Handle { get { - return _list; - } - } - - // ------------------------------ - // ICollection Interface Implementation - // ------------------------------ - - /// - /// Count Property - /// - /// - /// - /// The number of elements in the SList. - /// - - [DllImport("glib-2.0", CallingConvention=CallingConvention.Cdecl)] - static extern int g_slist_length(IntPtr raw); - - public int Count { - get { - return g_slist_length(_list); - } - } - - /// - /// IsSynchronized Property - /// - /// - /// - /// Returns false. GSLists are not threadsafe. - /// - - public bool IsSynchronized { - get { - return false; - } - } - - /// - /// SyncRoot Property - /// - /// - /// - /// Throws not implemented exception. GSLists are not - /// thread safe. - /// - - public object SyncRoot { - get { - throw new NotImplementedException(); - } - } - - /// - /// CopyTo Method - /// - /// - /// - /// Copies the list to an Array. - /// - - public void CopyTo(Array target, int index) - { - foreach (IntPtr item in this) { - target.SetValue(item, index++); - } - } - - // ------------------------------ - // IEnumerable Interface Implementation - // ------------------------------ - - public IEnumerator GetEnumerator() - { - return new SListEnumerator(this); - } - - // ------------------------------ - // IList Interface Implementation - // ------------------------------ - - /// - /// IsFixedSize Property - /// - /// - /// - /// Returns false. Items can be added and removed from - /// an SList. - /// - - public bool IsFixedSize { - get { - return false; - } - } - - /// - /// IsReadOnly Property - /// - /// - /// - /// Returns false. Items of an SList can be modified. - /// - - public bool IsReadOnly { - get { - return false; - } - } - - /// - /// Item Property - /// - /// - /// - /// Indexer to access members of the SList. - /// - - [DllImport("glib-2.0", CallingConvention=CallingConvention.Cdecl)] - static extern IntPtr g_slist_nth_data(IntPtr raw, int index); - - public object this[int index] { - get { - return g_slist_nth_data(_list, index); - } - set { - // FIXME: Set a data element. - rev_cnt++; - } - } - - // FIXME: Just a stub - public int Add(object o) - { - rev_cnt++; - return 0; - } - - // FIXME: Just a stub - public void Clear() - { - rev_cnt++; - } - - // FIXME: Just a stub - public bool Contains(object o) - { - return false; - } - - // FIXME: Just a stub - public int IndexOf(object o) - { - return 0; - } - - // FIXME: Just a stub - public void Insert(int index, object o) - { - rev_cnt++; - } - - // FIXME: Just a stub - public void Remove(object o) - { - rev_cnt++; - } - - // FIXME: Just a stub - public void RemoveAt(int index) - { - rev_cnt++; - } - - // -------------- - // object methods - // -------------- - - /// - /// Equals Method - /// - /// - /// - /// Checks equivalence of two SLists. - /// - - public override bool Equals (object o) - { - if (!(o is SList)) - return false; - - return (Handle == ((SList) o).Handle); - } - - /// - /// GetHashCode Method - /// - /// - /// - /// Calculates a hashing value. - /// - - public override int GetHashCode () - { - return Handle.GetHashCode (); - } - - // Internal enumerator class - - public class SListEnumerator : IEnumerator { - - IntPtr _cursor; - int i_rev; - SList _list; - bool virgin; - - public SListEnumerator (SList list) - { - _list = list; - i_rev = list.rev_cnt; - virgin = true; - } - - public object Current { - get { - if (virgin || (i_rev != _list.rev_cnt)) { - throw new InvalidOperationException(); - } - - if (_cursor == IntPtr.Zero) { - return null; - } - - return Marshal.ReadIntPtr(_cursor); + IntPtr l = IntPtr.Zero; + foreach (object o in this) { + IntPtr data = IntPtr.Zero; + if (o is GLib.Object) + l = g_slist_append (l, ((GLib.Object)o).Handle); + else + throw new Exception(); } - } - - public bool MoveNext() - { - if (i_rev != _list.rev_cnt) { - throw new InvalidOperationException(); - } - - if (virgin) { - _cursor = _list.Handle; - } else if (_cursor != IntPtr.Zero) { - _cursor = Marshal.ReadIntPtr(_cursor, IntPtr.Size); - } - - return (_cursor != IntPtr.Zero); - } - - public void Reset() - { - if (i_rev != _list.rev_cnt) { - throw new InvalidOperationException(); - } - - virgin = true; + return l; } } diff --git a/glib/Value.cs b/glib/Value.cs index ccc44710a..a387b0b90 100755 --- a/glib/Value.cs +++ b/glib/Value.cs @@ -113,12 +113,11 @@ namespace GLib { [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_string (IntPtr val, - IntPtr data); - public Value (String val) : this () + string data); + public Value (string val) : this () { g_value_init (_val, TypeFundamentals.TypeString); - g_value_set_string (_val, - Marshal.StringToHGlobalAnsi (val)); + g_value_set_string (_val, val); } /// @@ -193,14 +192,13 @@ namespace GLib { [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] - static extern IntPtr g_value_get_string (IntPtr val); + static extern string g_value_get_string (IntPtr val); public static explicit operator String (Value val) { // FIXME: Insert an appropriate exception here if // _val.type indicates an error. - return Marshal.PtrToStringAnsi ( - g_value_get_string (val._val)); + return g_value_get_string (val._val); } ///