"String",
"boolean ",
" {\n",
- " public static final ",
+ " final ",
" extends ",
"package ",
";",
"string",
"bool ",
"\n{\n",
- " public static ",
+ " readonly ",
" : ",
"namespace ",
"\n{",
++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, " ");
+ code += " public static";
code += lang.const_decl;
code += GenTypeBasic(lang, enum_def.underlying_type);
code += " " + ev.name + " = ";
code += NumToString(ev.value) + ";\n";
}
+
+ // Generate a generate string table for enum values.
+ // Problem is, if values are very sparse that could generate really big
+ // tables. Ideally in that case we generate a map lookup instead, but for
+ // the moment we simply don't output a table at all.
+ auto range = enum_def.vals.vec.back()->value -
+ enum_def.vals.vec.front()->value + 1;
+ // Average distance between values above which we consider a table
+ // "too sparse". Change at will.
+ static const int kMaxSparseness = 5;
+ if (range / static_cast<int64_t>(enum_def.vals.vec.size()) < kMaxSparseness) {
+ code += "\n private static";
+ code += lang.const_decl;
+ code += lang.string_type;
+ code += "[] names = { ";
+ auto val = enum_def.vals.vec.front()->value;
+ for (auto it = enum_def.vals.vec.begin();
+ it != enum_def.vals.vec.end();
+ ++it) {
+ while (val++ != (*it)->value) code += "\"\", ";
+ code += "\"" + (*it)->name + "\", ";
+ }
+ code += "};\n\n";
+ code += " public static ";
+ code += lang.string_type;
+ code += " " + MakeCamel("name", lang.first_camel_upper);
+ code += "(int e) { return names[e";
+ if (enum_def.vals.vec.front()->value)
+ code += " - " + enum_def.vals.vec.front()->name;
+ code += "]; }\n";
+ }
+
+ // Close the class
code += "};\n\n";
}
{
CanCreateNewFlatBufferFromScratch();
CanReadCppGeneratedWireFile();
+ TestEnums();
}
public void CanCreateNewFlatBufferFromScratch()
var bb = new ByteBuffer(data);
TestBuffer(bb);
}
+
+ 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");
+ }
}
}
// (see Table.__string).
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer());
+ TestEnums();
+
System.out.println("FlatBuffers test: completed successfully");
}
+ static void TestEnums() {
+ TestEq(Color.name(Color.Red), "Red");
+ TestEq(Color.name(Color.Blue), "Blue");
+ TestEq(Any.name(Any.NONE), "NONE");
+ TestEq(Any.name(Any.Monster), "Monster");
+ }
+
static void TestBuffer(ByteBuffer bb) {
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
public class Any
{
- public static byte NONE = 0;
- public static byte Monster = 1;
+ 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]; }
};
public class Any {
public static final byte NONE = 0;
public static final byte Monster = 1;
+
+ private static final String[] names = { "NONE", "Monster", };
+
+ public static String name(int e) { return names[e]; }
};
public class Color
{
- public static sbyte Red = 1;
- public static sbyte Green = 2;
- public static sbyte Blue = 8;
+ 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]; }
};
public static final byte Red = 1;
public static final byte Green = 2;
public static final byte Blue = 8;
+
+ private static final String[] names = { "Red", "Green", "", "", "", "", "", "Blue", };
+
+ public static String name(int e) { return names[e - Red]; }
};