Browse Source

Added work-in-progress std::vector interop support.

pull/86/head
triton 12 years ago
parent
commit
b02e7aa86a
  1. 146
      src/Runtime/StdVector.cs

146
src/Runtime/StdVector.cs

@ -19,23 +19,76 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#define MSVC
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using CppSharp.Runtime; using CppSharp.Runtime;
namespace Std namespace Std
{ {
public struct Vector [StructLayout(LayoutKind.Sequential)]
public unsafe struct Vector
{ {
#if MSVC
public byte* _Myfirst;
public byte* _Mylast;
public byte* _Myend;
#endif
public long Size()
{
return _Mylast - _Myfirst;
}
public bool Capacity()
{
return (_Myend - _Myfirst) != 0;
}
public bool UnusedCapacity()
{
return (_Myend - _Mylast) != 0;
}
public bool HasUnusedCapacity()
{
return _Myend != _Mylast;
}
public void Add<T>(T item) where T : ICppMarshal
{
item.MarshalManagedToNative(new IntPtr(_Mylast));
_Mylast += item.NativeDataSize;
}
public void ReserveNew(long count, int dataSize)
{
// Allocate some storage if we do not have any yet.
if (_Myfirst == null)
{
_Myfirst = (byte*) Marshal.AllocHGlobal(dataSize);
_Mylast = _Myfirst;
_Myend = _Myfirst + dataSize;
}
// Ensure room for count new elements, grow exponentially.
}
} }
public struct Vector<T> : ICollection<T> where T : ICppMarshal [StructLayout(LayoutKind.Sequential)]
public struct Vector<T> : IList<T> where T : ICppMarshal
{ {
public Vector Internal; public Vector Internal;
public int Count { get; private set; } public int Count
{
get { return (int)Internal.Size(); }
}
public bool IsReadOnly { get; private set; } public bool IsReadOnly { get; private set; }
public Vector(Vector vector) : this() public Vector(Vector vector) : this()
@ -45,7 +98,8 @@ namespace Std
public IEnumerator<T> GetEnumerator() public IEnumerator<T> GetEnumerator()
{ {
throw new NotImplementedException(); var enumerator = new VectorEnumerator<T>(this);
return enumerator;
} }
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator()
@ -55,7 +109,10 @@ namespace Std
public void Add(T item) public void Add(T item)
{ {
throw new NotImplementedException(); if (!Internal.HasUnusedCapacity())
Internal.ReserveNew(1, item.NativeDataSize);
Internal.Add(item);
} }
public void Clear() public void Clear()
@ -77,5 +134,84 @@ namespace Std
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void Resize(int newSize)
{
throw new NotImplementedException();
}
public int IndexOf(T item)
{
throw new NotImplementedException();
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public T this[int index]
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
}
public static class ListExtensions
{
public static Std.Vector<T> ToStd<T>(this List<T> list)
where T : ICppMarshal
{
var vec = new Std.Vector<T>();
return vec;
}
}
public unsafe class VectorEnumerator<T> : IEnumerator<T>
where T : ICppMarshal
{
public Vector<T> Vector;
public byte* Position;
public int DataSize;
public VectorEnumerator(Vector<T> vector)
{
Vector = vector;
if (Vector.Count > 0)
DataSize = Vector[0].NativeDataSize;
}
public void Dispose()
{
}
public bool MoveNext()
{
Position = (byte*)Vector.Internal._Myfirst;
if (Position == Vector.Internal._Mylast)
return false;
Position += DataSize;
return false;
}
public void Reset()
{
throw new NotImplementedException();
}
public T Current { get; private set; }
object IEnumerator.Current
{
get { return Current; }
}
} }
} }

Loading…
Cancel
Save