Base type safety in C#. Clear FlatBufferBuilder in C#.

This commit is contained in:
RevenantX
2015-07-29 00:33:45 +03:00
parent 6e192fa408
commit 588564d74f
12 changed files with 188 additions and 53 deletions

View File

@@ -41,6 +41,11 @@ namespace FlatBuffers
_pos = 0;
}
public void Reset()
{
_pos = 0;
}
public int Position { get { return _pos; } }
// Pre-allocated helper arrays for convertion.

View File

@@ -1,4 +1,4 @@
/*
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -49,6 +49,17 @@ namespace FlatBuffers
_bb = new ByteBuffer(new byte[initialSize]);
}
public void Clear()
{
_space = _bb.Length;
_bb.Reset();
_minAlign = 1;
_vtable = null;
_objectStart = 0;
_vtables = new int[16];
_numVtables = 0;
_vectorNumElems = 0;
}
public int Offset { get { return _bb.Length - _space; } }
@@ -196,10 +207,10 @@ namespace FlatBuffers
Prep(alignment, elemSize * count); // Just in case alignment > int.
}
public int EndVector()
public VectorOffset EndVector()
{
PutInt(_vectorNumElems);
return Offset;
return new VectorOffset(Offset);
}
public void Nested(int obj)
@@ -250,7 +261,7 @@ namespace FlatBuffers
public void AddDouble(int o, double x, double d) { if (x != d) { AddDouble(x); Slot(o); } }
public void AddOffset(int o, int x, int d) { if (x != d) { AddOffset(x); Slot(o); } }
public int CreateString(string s)
public StringOffset CreateString(string s)
{
NotNested();
byte[] utf8 = Encoding.UTF8.GetBytes(s);
@@ -258,7 +269,7 @@ namespace FlatBuffers
StartVector(1, utf8.Length, 1);
Buffer.BlockCopy(utf8, 0, _bb.Data, _space -= utf8.Length,
utf8.Length);
return EndVector();
return new StringOffset(EndVector().Value);
}
// Structs are stored inline, so nothing additional is being added.

View File

@@ -37,6 +37,7 @@
<Compile Include="ByteBuffer.cs" />
<Compile Include="FlatBufferBuilder.cs" />
<Compile Include="FlatBufferConstants.cs" />
<Compile Include="Offset.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Struct.cs" />
<Compile Include="Table.cs" />

48
net/FlatBuffers/Offset.cs Normal file
View File

@@ -0,0 +1,48 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace FlatBuffers
{
/// <summary>
/// Offset class for typesafe assignments.
/// </summary>
public struct Offset<T> where T : class
{
public int Value;
public Offset(int value)
{
Value = value;
}
}
public struct StringOffset
{
public int Value;
public StringOffset(int value)
{
Value = value;
}
}
public struct VectorOffset
{
public int Value;
public VectorOffset(int value)
{
Value = value;
}
}
}