summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorRevenantX <doomrevx@gmail.com>2015-07-29 00:33:45 +0300
committerRevenantX <doomrevx@gmail.com>2015-07-29 00:33:45 +0300
commit588564d74f2968808557d96b6f71e5dc7cd62622 (patch)
tree49c76768b4049d24e7518c48ef0add03c7d7dc99 /net
parent6e192fa4087aaafd17a050ee5da3d4a043352055 (diff)
downloadflatbuffers-588564d74f2968808557d96b6f71e5dc7cd62622.tar.gz
flatbuffers-588564d74f2968808557d96b6f71e5dc7cd62622.tar.bz2
flatbuffers-588564d74f2968808557d96b6f71e5dc7cd62622.zip
Base type safety in C#. Clear FlatBufferBuilder in C#.
Diffstat (limited to 'net')
-rwxr-xr-xnet/FlatBuffers/ByteBuffer.cs5
-rw-r--r--net/FlatBuffers/FlatBufferBuilder.cs21
-rw-r--r--net/FlatBuffers/FlatBuffers.csproj1
-rw-r--r--net/FlatBuffers/Offset.cs48
4 files changed, 70 insertions, 5 deletions
diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs
index aadd0ea5..c986269b 100755
--- a/net/FlatBuffers/ByteBuffer.cs
+++ b/net/FlatBuffers/ByteBuffer.cs
@@ -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.
diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs
index 453b6e5c..f184f52c 100644
--- a/net/FlatBuffers/FlatBufferBuilder.cs
+++ b/net/FlatBuffers/FlatBufferBuilder.cs
@@ -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.
diff --git a/net/FlatBuffers/FlatBuffers.csproj b/net/FlatBuffers/FlatBuffers.csproj
index 9307c33f..3ae938a1 100644
--- a/net/FlatBuffers/FlatBuffers.csproj
+++ b/net/FlatBuffers/FlatBuffers.csproj
@@ -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" />
diff --git a/net/FlatBuffers/Offset.cs b/net/FlatBuffers/Offset.cs
new file mode 100644
index 00000000..a1524df0
--- /dev/null
+++ b/net/FlatBuffers/Offset.cs
@@ -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;
+ }
+ }
+}