C
colin
Hi,
I finaly managed to come up with this code after struggling for ages,..
its a way to access my list<T> as if it were a list of list<T2>
it looked a bit unweildly but it seems to work ...
I dont know if there are any better ways of doing this ?
it wouldnt let me simply override things in the main list class.
its just has an interface for a converter to convert the elemnt from one
type to another,
then a generic class wich takes the list class and passes all the enumerator
work directly to it
just intercepting the Current() method to do the conversion.
public interface ElementConverter<To,From>
{
To ConvertElement(From p);
}
public class ListEnum<T,To, From> : IEnumerable<To>, IEnumerator<To>
where T : IList<From>, ElementConverter<To,From>
{
#region IEnumerable Members
public T list;
public IEnumerator<From> enumerator;
public ListEnum(T l)
{
list = l;
}
IEnumerator<To> IEnumerable<To>.GetEnumerator()
{
enumerator = list.GetEnumerator();
return this;
}
#endregion
#region IEnumerator Members
public object Current
{
get { return list.ConvertElement((From)enumerator.Current); }
}
public bool MoveNext()
{
return enumerator.MoveNext();
}
public void Reset()
{
enumerator.Reset();
}
#endregion
#region IDisposable Members
public void Dispose()
{
enumerator.Dispose();
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
throw new Exception("The method or operation is not
implemented.");
}
#endregion
#region IEnumerator<To> Members
To IEnumerator<To>.Current
{
get { return list.ConvertElement(enumerator.Current); }
}
#endregion
}
public class Point3d
{
Public Vector3 Position;
//...lots of other stuff
}
public class PointList : List<Point3d>,
ElementConverter<Vector3,Point3d>
{
internal IEnumerable<Vector3> VectorList()
{
return (IEnumerable<Vector3>)new ListEnum<PointList, Vector3,
Point3d>(this);
}
#region ElementConverter<Vector3,Point3d> Members
public Vector3 ConvertElement(Point3d p)
{
return p.Position;
}
#endregion
}
basically I store my list of 3d points with all sorts of other information,
wich I need to keep in the same data structure,
but certain functions require a list of vector3 such as creating a
BoundingSphere.
this saves having to create a list of vector3 or try and keep a copy wich
remains up to date.
any comments would be of interest
Colin =^.^=
I finaly managed to come up with this code after struggling for ages,..
its a way to access my list<T> as if it were a list of list<T2>
it looked a bit unweildly but it seems to work ...
I dont know if there are any better ways of doing this ?
it wouldnt let me simply override things in the main list class.
its just has an interface for a converter to convert the elemnt from one
type to another,
then a generic class wich takes the list class and passes all the enumerator
work directly to it
just intercepting the Current() method to do the conversion.
public interface ElementConverter<To,From>
{
To ConvertElement(From p);
}
public class ListEnum<T,To, From> : IEnumerable<To>, IEnumerator<To>
where T : IList<From>, ElementConverter<To,From>
{
#region IEnumerable Members
public T list;
public IEnumerator<From> enumerator;
public ListEnum(T l)
{
list = l;
}
IEnumerator<To> IEnumerable<To>.GetEnumerator()
{
enumerator = list.GetEnumerator();
return this;
}
#endregion
#region IEnumerator Members
public object Current
{
get { return list.ConvertElement((From)enumerator.Current); }
}
public bool MoveNext()
{
return enumerator.MoveNext();
}
public void Reset()
{
enumerator.Reset();
}
#endregion
#region IDisposable Members
public void Dispose()
{
enumerator.Dispose();
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
throw new Exception("The method or operation is not
implemented.");
}
#endregion
#region IEnumerator<To> Members
To IEnumerator<To>.Current
{
get { return list.ConvertElement(enumerator.Current); }
}
#endregion
}
public class Point3d
{
Public Vector3 Position;
//...lots of other stuff
}
public class PointList : List<Point3d>,
ElementConverter<Vector3,Point3d>
{
internal IEnumerable<Vector3> VectorList()
{
return (IEnumerable<Vector3>)new ListEnum<PointList, Vector3,
Point3d>(this);
}
#region ElementConverter<Vector3,Point3d> Members
public Vector3 ConvertElement(Point3d p)
{
return p.Position;
}
#endregion
}
basically I store my list of 3d points with all sorts of other information,
wich I need to keep in the same data structure,
but certain functions require a list of vector3 such as creating a
BoundingSphere.
this saves having to create a list of vector3 or try and keep a copy wich
remains up to date.
any comments would be of interest
Colin =^.^=