_UNKNOWN variant for enums

This commit is contained in:
David Renshaw 2014-10-01 12:01:24 -04:00
parent 14f9c22907
commit fdb7c8ad9c
4 changed files with 21 additions and 8 deletions

View file

@ -53,7 +53,7 @@ public class CarSales
car.setMake(MAKES[rng.nextLessThan(MAKES.length)]); car.setMake(MAKES[rng.nextLessThan(MAKES.length)]);
car.setModel(MODELS[rng.nextLessThan(MODELS.length)]); car.setModel(MODELS[rng.nextLessThan(MODELS.length)]);
car.setColor(Color.values()[rng.nextLessThan(Color.values().length)]); car.setColor(Color.values()[rng.nextLessThan(Color.SILVER.ordinal() + 1)]);
car.setSeats((byte)(2 + rng.nextLessThan(6))); car.setSeats((byte)(2 + rng.nextLessThan(6)));
car.setDoors((byte)(2 + rng.nextLessThan(3))); car.setDoors((byte)(2 + rng.nextLessThan(3)));

View file

@ -10,7 +10,7 @@ public class Eval
EvaluationResult.Factory, EvaluationResult.Builder, EvaluationResult.Reader, Integer> { EvaluationResult.Factory, EvaluationResult.Builder, EvaluationResult.Reader, Integer> {
public static int makeExpression(Common.FastRand rng, Expression.Builder exp, int depth) { public static int makeExpression(Common.FastRand rng, Expression.Builder exp, int depth) {
exp.setOp(Operation.values()[rng.nextLessThan(Operation.values().length)]); exp.setOp(Operation.values()[rng.nextLessThan(Operation.MODULUS.ordinal() + 1)]);
int left = 0; int left = 0;
if (rng.nextLessThan(8) < depth) { if (rng.nextLessThan(8) < depth) {

View file

@ -820,7 +820,8 @@ private:
spaces(indent), " public final ", type, " get", titleCase, "() {\n", spaces(indent), " public final ", type, " get", titleCase, "() {\n",
spaces(indent), spaces(indent),
(typeBody.which() == schema::Type::ENUM ? (typeBody.which() == schema::Type::ENUM ?
kj::strTree(" return ", type, ".values()[_reader.getShortField(", offset, ")];\n") : kj::strTree(" return org.capnproto.GeneratedClassSupport.clampOrdinal(", type, ".values(),",
"_reader.getShortField(", offset, "));\n") :
(typeBody.which() == schema::Type::VOID ? (typeBody.which() == schema::Type::VOID ?
kj::strTree(" return org.capnproto.Void.VOID;\n") : kj::strTree(" return org.capnproto.Void.VOID;\n") :
kj::strTree(" return _reader.get",toTitleCase(type),"Field(", offset, defaultMaskParam, ");\n"))), kj::strTree(" return _reader.get",toTitleCase(type),"Field(", offset, defaultMaskParam, ");\n"))),
@ -832,7 +833,8 @@ private:
spaces(indent), " public final ", type, " get", titleCase, "() {\n", spaces(indent), " public final ", type, " get", titleCase, "() {\n",
spaces(indent), spaces(indent),
(typeBody.which() == schema::Type::ENUM ? (typeBody.which() == schema::Type::ENUM ?
kj::strTree(" return ", type, ".values()[_builder.getShortField(", offset, ")];\n") : kj::strTree(" return org.capnproto.GeneratedClassSupport.clampOrdinal(", type, ".values(),",
"_builder.getShortField(", offset, "));\n") :
(typeBody.which() == schema::Type::VOID ? (typeBody.which() == schema::Type::VOID ?
kj::strTree(" return org.capnproto.Void.VOID;\n") : kj::strTree(" return org.capnproto.Void.VOID;\n") :
kj::strTree(" return _builder.get",toTitleCase(type),"Field(", offset, defaultMaskParam, ");\n"))), kj::strTree(" return _builder.get",toTitleCase(type),"Field(", offset, defaultMaskParam, ");\n"))),
@ -1229,8 +1231,8 @@ private:
"\n", "\n",
(isUnion ? (isUnion ?
kj::strTree(spaces(indent), " public Which which() {\n", kj::strTree(spaces(indent), " public Which which() {\n",
spaces(indent), " return Which.values()[_reader.getShortField(", spaces(indent), " return org.capnproto.GeneratedClassSupport.clampOrdinal(Which.values(),",
discriminantOffset, ")];\n", "_reader.getShortField(", discriminantOffset, "));\n",
spaces(indent), " }\n") spaces(indent), " }\n")
: kj::strTree()), : kj::strTree()),
kj::mv(methodDecls), kj::mv(methodDecls),
@ -1250,8 +1252,8 @@ private:
spaces(indent), " public org.capnproto.StructBuilder _builder;\n", spaces(indent), " public org.capnproto.StructBuilder _builder;\n",
(isUnion ? (isUnion ?
kj::strTree(spaces(indent), " public Which which() {\n", kj::strTree(spaces(indent), " public Which which() {\n",
spaces(indent), " return Which.values()[_builder.getShortField(", spaces(indent), " return org.capnproto.GeneratedClassSupport.clampOrdinal(Which.values(),",
structNode.getDiscriminantOffset(), ")];\n", "_builder.getShortField(", structNode.getDiscriminantOffset(), "));\n",
spaces(indent), " }\n") spaces(indent), " }\n")
: kj::strTree()), : kj::strTree()),
spaces(indent), " public final Reader asReader() {\n", spaces(indent), " public final Reader asReader() {\n",
@ -1320,6 +1322,7 @@ private:
return kj::strTree(); return kj::strTree();
} }
}, },
spaces(indent), " _UNKNOWN,\n",
spaces(indent), " }\n"), spaces(indent), " }\n"),
KJ_MAP(n, nestedTypeDecls) { return kj::mv(n); }, KJ_MAP(n, nestedTypeDecls) { return kj::mv(n); },
spaces(indent), "}\n" spaces(indent), "}\n"
@ -1627,6 +1630,7 @@ private:
KJ_MAP(e, enumerants) { KJ_MAP(e, enumerants) {
return kj::strTree(spaces(indent), " ", toUpperCase(e.getProto().getName()), ",\n"); return kj::strTree(spaces(indent), " ", toUpperCase(e.getProto().getName()), ",\n");
}, },
spaces(indent), " _UNKNOWN,\n",
spaces(indent), "}\n" spaces(indent), "}\n"
"\n"), "\n"),

View file

@ -1,6 +1,15 @@
package org.capnproto; package org.capnproto;
public final class GeneratedClassSupport { public final class GeneratedClassSupport {
public static <T> T clampOrdinal(T values[], short ordinal) {
int index = ordinal;
if (ordinal < 0 || ordinal >= values.length) {
index = values.length - 1;
}
return values[index];
}
public static byte[] decodeRawBytes(String s) { public static byte[] decodeRawBytes(String s) {
try { try {
return s.getBytes("ISO_8859-1"); return s.getBytes("ISO_8859-1");