/* * Copyright (C) 2007-2012 Xagasoft, All rights reserved. * * This file is part of the libgats library and is released under the * terms of the license contained in the file LICENSE. */ using System; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; namespace Com.Xagasoft.Gats { /// /// Encapsulates a single list of GatsObjects. /// /// /// A list of arbitrary size containing ordered GatsObjects. All standard /// .NET IList, ICollection, and IEnumerable interfaces are implemented, /// so a GatsList should work just like a standard List, but with a few /// extras. /// public class GatsList : GatsObject, IList, ICollection, IEnumerable // IReadOnlyList, IReadOnlyCollection, // These two are in .net 4.5, not 4.0, mono can't use them. { private List Value = new List(); public GatsList() { } public override string ToString() { StringBuilder bld = new StringBuilder(); bld.Append("["); for( int j = 0; j < Value.Count-1; j++ ) { bld.Append( Value[j] ); bld.Append(", "); } bld.Append( Value[Value.Count-1] ); bld.Append("]"); return bld.ToString(); } public override void Read( Stream s, char type ) { for(;;) { GatsObject obj = GatsObject.Read( s ); if( obj == null ) break; Value.Add( obj ); } } public override void Write( Stream s ) { s.WriteByte( (int)'l' ); foreach( GatsObject obj in Value ) { obj.Write( s ); } s.WriteByte( (int)'e' ); } // // Helper functions // /// /// Helper function to add a new GatsString to the list. /// /// The byte array to be added as a GatsString public void Add( byte[] val ) { Add( new GatsString( val ) ); } /// /// Helper function to add a new GatsString to the list. /// /// /// The string is encoded UTF-8 by .NETs internal facilities. /// /// The string to be added as a GatsString public void Add( string val ) { Add( new GatsString( val ) ); } /// /// Helper function to add a new GatsInteger to the list. /// /// /// Implicit upcasting should allow all integer types (byte, short, /// int, long), and automatic unboxing should allow all object variants /// to be passed into this method without problem. /// /// The long to be added as a GatsInteger public void Add( long val ) { Add( new GatsInteger( val ) ); } /// /// Helper function to add a new GatsFloat to the list. /// /// /// Implicit upcasting should allow floats and doubles to both be /// accepted by this method. Please note that decimal types are not /// strictly compatible, please see GatsFloat for more details. /// /// The double to be added as a GatsFloat public void Add( double val ) { Add( new GatsFloat( val ) ); } /// /// Helper function to add a new GatsBoolean to the list. /// /// /// The boolean value to be added as a GatsBoolean /// public void Add( bool val ) { Add( new GatsBoolean( val ) ); } /// /// Helper to append a GatsNull to the list. /// public void AddNull() { Add( new GatsNull() ); } /// /// Helper to append a GatsDictionary to the list. /// /// /// A new GatsDictionary is constructed, appended to the list, and /// returned. /// public GatsDictionary AddDict() { GatsDictionary dict = new GatsDictionary(); Add( dict ); return dict; } /// /// Helper to append a GatsList to the list. /// /// /// A new GatsList is constructed, appended to the list, and returned. /// public GatsList AddList() { GatsList list = new GatsList(); Add( list ); return list; } // // List interface overrides under here. // public int IndexOf( GatsObject obj ) { return Value.IndexOf( obj ); } public void Insert( int idx, GatsObject obj ) { Value.Insert( idx, obj ); } public void RemoveAt( int idx ) { Value.RemoveAt( idx ); } public GatsObject this[int idx] { get { return this.Value[idx]; } set { this.Value[idx] = value; } } public int Count { get { return this.Value.Count; } } public bool IsReadOnly { get { return false; } // this.Value.IsReadOnly; } } public void Add( GatsObject obj ) { Value.Add( obj ); } public void Clear() { Value.Clear(); } public bool Contains( GatsObject obj ) { return Value.Contains( obj ); } public void CopyTo( GatsObject[] result, int count ) { Value.CopyTo( result, count ); } public bool Remove( GatsObject obj ) { return Value.Remove( obj ); } IEnumerator IEnumerable.GetEnumerator() { return Value.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return Value.GetEnumerator(); } public bool IsFixedSize { get { return false; } // this.Value.IsFixedSize; } } public bool IsSynchronized { get { return false; } // this.Value.IsSynchronized; } } public object SyncRoot { get { return null; } // this.Value.SyncRoot; } } } }