/** * sugar.i * * Helper macros to implement .net collections and other assorted syntactic sugar */ %define IMPLEMENT_LIST(collection_type, item_type) //Necessary imports %typemap(csimports) collection_type %{ using System; using System.Reflection; using System.Collections; using System.Collections.Generic; //These warnings are false positives as a result of SWIG generated code #pragma warning disable 0108, 0114 %} //Collection Interfaces implemented by the implementing proxy class %typemap(csinterfaces_derived) collection_type "IList" //This is the IList implementation that is injected into the implementing proxy class %typemap(cscode) collection_type %{ /* int IList.IndexOf(item_type item) { return this.IndexOf(item); } void IList.Insert(int index, item_type item) { this.Insert(index, item); } void IList.RemoveAt(int index) { this.RemoveAt(index); } */ public item_type this[int index] { get { return this.GetItem(index); } set { this.SetItem(index, value); } } /* void ICollection.Add(item_type item) { this.Add(item); } void ICollection.Clear() { this.Clear(); } bool ICollection.Contains(item_type item) { return this.Contains(item); } */ public void CopyTo(item_type[] array, int arrayIndex) { throw new global::System.NotImplementedException(); } bool ICollection.Remove(item_type item) { int count = this.GetCount(); this.Remove(item); return this.GetCount() < count; } public int Count { get { return this.GetCount(); } } public bool IsReadOnly { get { return false; } } class CollectionEnumerator : IEnumerator { private IList _list; private int _position; private int _count; public CollectionEnumerator(IList list) { _list = list; _count = list.Count; _position = -1; } bool IEnumerator.MoveNext() { _position++; return _position < _count; } void IEnumerator.Reset() { _position = -1; } object IEnumerator.Current { get { return _list[_position]; } } item_type IEnumerator.Current { get { return _list[_position]; } } public void Dispose() { } } public IEnumerator GetEnumerator() { return new CollectionEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new CollectionEnumerator(this); } %} %enddef %define IMPLEMENT_READONLY_LIST(collection_type, item_type) //Necessary imports %typemap(csimports) collection_type %{ using System; using System.Reflection; using System.Collections; using System.Collections.Generic; %} //Collection Interfaces implemented by the implementing proxy class %typemap(csinterfaces_derived) collection_type "IReadOnlyList" //This is the IReadOnlyList implementation that is injected into the implementing proxy class %typemap(cscode) collection_type %{ item_type IReadOnlyList.this[int index] { get { return this.GetItem(index); } } int IReadOnlyCollection.Count { get { return this.GetCount(); } } class CollectionEnumerator : IEnumerator { private IReadOnlyList _list; private int _position; private int _count; public CollectionEnumerator(IReadOnlyList list) { _list = list; _count = list.Count; _position = -1; } bool IEnumerator.MoveNext() { _position++; return _position < _count; } void IEnumerator.Reset() { _position = -1; } object IEnumerator.Current { get { return _list[_position]; } } item_type IEnumerator.Current { get { return _list[_position]; } } public void Dispose() { } } IEnumerator IEnumerable.GetEnumerator() { return new CollectionEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new CollectionEnumerator(this); } %} %enddef