Commit e3b432cb authored by Mormegil's avatar Mormegil Committed by Wouter van Oortmerssen

Enums use native enums in C#

Enums should not be (badly) emulated with classes in C# but should
use native C# enums instead. Java implementation made an explicit
choice not to use the (more complex) Java enums, but C# enums are
just light-weight syntactic coating over integral types.

Fixes issue #171.

Change-Id: I9f4d6ba5324400a1e52982e49b58603cb7d7cca7
parent 557c57eb
This diff is collapsed.
......@@ -72,7 +72,7 @@ namespace FlatBuffers.Test
Monster.AddHp(fbb, (short)80);
Monster.AddName(fbb, str);
Monster.AddInventory(fbb, inv);
Monster.AddTestType(fbb, (byte)1);
Monster.AddTestType(fbb, Any.Monster);
Monster.AddTest(fbb, mon2);
Monster.AddTest4(fbb, test4);
Monster.AddTestarrayofstring(fbb, testArrayOfString);
......@@ -111,7 +111,7 @@ namespace FlatBuffers.Test
Assert.AreEqual((short)5, t.A());
Assert.AreEqual((sbyte)6, t.B());
Assert.AreEqual((byte)Any.Monster, monster.TestType());
Assert.AreEqual(Any.Monster, monster.TestType());
var monster2 = new Monster();
Assert.IsTrue(monster.Test(monster2) != null);
......@@ -148,10 +148,10 @@ namespace FlatBuffers.Test
public void TestEnums()
{
Assert.AreEqual(Color.Name(Color.Red), "Red");
Assert.AreEqual(Color.Name(Color.Blue), "Blue");
Assert.AreEqual(Any.Name(Any.NONE), "NONE");
Assert.AreEqual(Any.Name(Any.Monster), "Monster");
Assert.AreEqual("Red", Color.Red.ToString());
Assert.AreEqual("Blue", Color.Blue.ToString());
Assert.AreEqual("NONE", Any.NONE.ToString());
Assert.AreEqual("Monster", Any.Monster.ToString());
}
}
}
......@@ -3,15 +3,10 @@
namespace MyGame.Example
{
public sealed class Any
public enum Any : byte
{
private Any() { }
public static readonly byte NONE = 0;
public static readonly byte Monster = 1;
private static readonly string[] names = { "NONE", "Monster", };
public static string Name(int e) { return names[e]; }
NONE = 0,
Monster = 1,
};
......
......@@ -3,16 +3,11 @@
namespace MyGame.Example
{
public sealed class Color
public enum Color : sbyte
{
private Color() { }
public static readonly sbyte Red = 1;
public static readonly sbyte Green = 2;
public static readonly sbyte Blue = 8;
private static readonly string[] names = { "Red", "Green", "", "", "", "", "", "Blue", };
public static string Name(int e) { return names[e - Red]; }
Red = 1,
Green = 2,
Blue = 8,
};
......
......@@ -18,8 +18,8 @@ public sealed class Monster : Table {
public string Name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
public byte Inventory(int j) { int o = __offset(14); return o != 0 ? bb.Get(__vector(o) + j * 1) : (byte)0; }
public int InventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; }
public sbyte Color() { int o = __offset(16); return o != 0 ? bb.GetSbyte(o + bb_pos) : (sbyte)8; }
public byte TestType() { int o = __offset(18); return o != 0 ? bb.Get(o + bb_pos) : (byte)0; }
public Color Color() { int o = __offset(16); return o != 0 ? (Color)bb.GetSbyte(o + bb_pos) : (Color)8; }
public Any TestType() { int o = __offset(18); return o != 0 ? (Any)bb.Get(o + bb_pos) : (Any)0; }
public Table Test(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o) : null; }
public Test Test4(int j) { return Test4(new Test(), j); }
public Test Test4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; }
......@@ -55,8 +55,8 @@ public sealed class Monster : Table {
public static void AddInventory(FlatBufferBuilder builder, int inventoryOffset) { builder.AddOffset(5, inventoryOffset, 0); }
public static int CreateInventoryVector(FlatBufferBuilder builder, byte[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddByte(data[i]); return builder.EndVector(); }
public static void StartInventoryVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); }
public static void AddColor(FlatBufferBuilder builder, sbyte color) { builder.AddSbyte(6, color, 8); }
public static void AddTestType(FlatBufferBuilder builder, byte testType) { builder.AddByte(7, testType, 0); }
public static void AddColor(FlatBufferBuilder builder, Color color) { builder.AddSbyte(6, (sbyte)(color), 8); }
public static void AddTestType(FlatBufferBuilder builder, Any testType) { builder.AddByte(7, (byte)(testType), 0); }
public static void AddTest(FlatBufferBuilder builder, int testOffset) { builder.AddOffset(8, testOffset, 0); }
public static void AddTest4(FlatBufferBuilder builder, int test4Offset) { builder.AddOffset(9, test4Offset, 0); }
public static void StartTest4Vector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 2); }
......
......@@ -12,11 +12,11 @@ public sealed class Vec3 : Struct {
public float Y() { return bb.GetFloat(bb_pos + 4); }
public float Z() { return bb.GetFloat(bb_pos + 8); }
public double Test1() { return bb.GetDouble(bb_pos + 16); }
public sbyte Test2() { return bb.GetSbyte(bb_pos + 24); }
public Color Test2() { return (Color)bb.GetSbyte(bb_pos + 24); }
public Test Test3() { return Test3(new Test()); }
public Test Test3(Test obj) { return obj.__init(bb_pos + 26, bb); }
public static int CreateVec3(FlatBufferBuilder builder, float X, float Y, float Z, double Test1, sbyte Test2, short Test_A, sbyte Test_B) {
public static int CreateVec3(FlatBufferBuilder builder, float X, float Y, float Z, double Test1, Color Test2, short Test_A, sbyte Test_B) {
builder.Prep(16, 32);
builder.Pad(2);
builder.Prep(2, 4);
......@@ -24,7 +24,7 @@ public sealed class Vec3 : Struct {
builder.PutSbyte(Test_B);
builder.PutShort(Test_A);
builder.Pad(1);
builder.PutSbyte(Test2);
builder.PutSbyte((sbyte)(Test2));
builder.PutDouble(Test1);
builder.Pad(4);
builder.PutFloat(Z);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment