don't use values() for Which
This commit is contained in:
parent
ff04e3a98c
commit
32d2a7bbda
2 changed files with 31 additions and 31 deletions
|
@ -610,9 +610,9 @@ private:
|
||||||
kj::str(
|
kj::str(
|
||||||
spaces(indent), " _builder.setShortField(", discrimOffset, ", (short)",
|
spaces(indent), " _builder.setShortField(", discrimOffset, ", (short)",
|
||||||
scope, "Which.", upperCase, ".ordinal());\n"),
|
scope, "Which.", upperCase, ".ordinal());\n"),
|
||||||
kj::strTree(spaces(indent), " public final boolean is", titleCase, "() {\n",
|
kj::strTree(spaces(indent), "public final boolean is", titleCase, "() {\n",
|
||||||
spaces(indent), " return which() == ", scope, "Which.", upperCase,";\n",
|
spaces(indent), " return which() == ", scope, "Which.", upperCase,";\n",
|
||||||
spaces(indent), " }\n"),
|
spaces(indent), "}\n"),
|
||||||
kj::strTree(spaces(indent), "public final boolean is", titleCase, "() {\n",
|
kj::strTree(spaces(indent), "public final boolean is", titleCase, "() {\n",
|
||||||
spaces(indent), " return which() == ", scope, "Which.", upperCase, ";\n",
|
spaces(indent), " return which() == ", scope, "Which.", upperCase, ";\n",
|
||||||
spaces(indent), "}\n"),
|
spaces(indent), "}\n"),
|
||||||
|
@ -1236,19 +1236,36 @@ private:
|
||||||
kj::StringTree inlineMethodDefs;
|
kj::StringTree inlineMethodDefs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
kj::StringTree makeWhich(StructSchema schema, kj::String member, int indent) {
|
||||||
|
if (schema.getProto().getStruct().getDiscriminantCount() == 0) {
|
||||||
|
return kj::strTree();
|
||||||
|
} else {
|
||||||
|
auto fields = schema.getUnionFields();
|
||||||
|
return kj::strTree(
|
||||||
|
spaces(indent), "public Which which() {\n",
|
||||||
|
spaces(indent+1), "switch(", member, ".getShortField(",
|
||||||
|
schema.getProto().getStruct().getDiscriminantOffset(), ")) {\n",
|
||||||
|
KJ_MAP(f, fields) {
|
||||||
|
return kj::strTree(spaces(indent+2), "case ", f.getProto().getDiscriminantValue(), " : return ",
|
||||||
|
"Which.",
|
||||||
|
toUpperCase(f.getProto().getName()), ";\n");
|
||||||
|
},
|
||||||
|
spaces(indent+2), "default: return Which._UNKNOWN;\n",
|
||||||
|
spaces(indent+1), "}\n",
|
||||||
|
spaces(indent), "}\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
kj::StringTree makeReaderDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType,
|
kj::StringTree makeReaderDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType,
|
||||||
bool isUnion, uint discriminantOffset, kj::Array<kj::StringTree>&& methodDecls,
|
StructSchema schema, kj::Array<kj::StringTree>&& methodDecls,
|
||||||
int indent) {
|
int indent) {
|
||||||
return kj::strTree(
|
return kj::strTree(
|
||||||
spaces(indent), "public static final class Reader {\n",
|
spaces(indent), "public static final class Reader {\n",
|
||||||
spaces(indent), " public Reader(org.capnproto.StructReader base){ this._reader = base; }\n",
|
spaces(indent), " public Reader(org.capnproto.StructReader base){ this._reader = base; }\n",
|
||||||
"\n",
|
"\n",
|
||||||
(isUnion ?
|
makeWhich(schema, kj::str("_reader"), indent+1),
|
||||||
kj::strTree(spaces(indent), " public Which which() {\n",
|
|
||||||
spaces(indent), " return org.capnproto.GeneratedClassSupport.clampOrdinal(Which.values(),",
|
|
||||||
"_reader.getShortField(", discriminantOffset, "));\n",
|
|
||||||
spaces(indent), " }\n")
|
|
||||||
: kj::strTree()),
|
|
||||||
kj::mv(methodDecls),
|
kj::mv(methodDecls),
|
||||||
spaces(indent), " public org.capnproto.StructReader _reader;\n",
|
spaces(indent), " public org.capnproto.StructReader _reader;\n",
|
||||||
spaces(indent), "}\n"
|
spaces(indent), "}\n"
|
||||||
|
@ -1256,20 +1273,13 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
kj::StringTree makeBuilderDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType,
|
kj::StringTree makeBuilderDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType,
|
||||||
schema::Node::Struct::Reader structNode,
|
StructSchema schema, kj::Array<kj::StringTree>&& methodDecls,
|
||||||
kj::Array<kj::StringTree>&& methodDecls,
|
|
||||||
int indent) {
|
int indent) {
|
||||||
bool isUnion = structNode.getDiscriminantCount() != 0;
|
|
||||||
return kj::strTree(
|
return kj::strTree(
|
||||||
spaces(indent), "public static final class Builder {\n",
|
spaces(indent), "public static final class Builder {\n",
|
||||||
spaces(indent), " public Builder(org.capnproto.StructBuilder base){ this._builder = base; }\n",
|
spaces(indent), " public Builder(org.capnproto.StructBuilder base){ this._builder = base; }\n",
|
||||||
spaces(indent), " public org.capnproto.StructBuilder _builder;\n",
|
spaces(indent), " public org.capnproto.StructBuilder _builder;\n",
|
||||||
(isUnion ?
|
makeWhich(schema, kj::str("_builder"), indent+1),
|
||||||
kj::strTree(spaces(indent), " public Which which() {\n",
|
|
||||||
spaces(indent), " return org.capnproto.GeneratedClassSupport.clampOrdinal(Which.values(),",
|
|
||||||
"_builder.getShortField(", structNode.getDiscriminantOffset(), "));\n",
|
|
||||||
spaces(indent), " }\n")
|
|
||||||
: kj::strTree()),
|
|
||||||
spaces(indent), " public final Reader asReader() {\n",
|
spaces(indent), " public final Reader asReader() {\n",
|
||||||
spaces(indent), " return new Reader(this._builder.asReader());\n",
|
spaces(indent), " return new Reader(this._builder.asReader());\n",
|
||||||
spaces(indent), " }\n",
|
spaces(indent), " }\n",
|
||||||
|
@ -1317,11 +1327,10 @@ private:
|
||||||
spaces(indent), " public static final Factory factory = new Factory();\n",
|
spaces(indent), " public static final Factory factory = new Factory();\n",
|
||||||
|
|
||||||
|
|
||||||
kj::strTree(makeReaderDef(fullName, name, structNode.getDiscriminantCount() != 0,
|
kj::strTree(makeReaderDef(fullName, name, schema,
|
||||||
structNode.getDiscriminantOffset(),
|
|
||||||
KJ_MAP(f, fieldTexts) { return kj::mv(f.readerMethodDecls); },
|
KJ_MAP(f, fieldTexts) { return kj::mv(f.readerMethodDecls); },
|
||||||
indent + 1),
|
indent + 1),
|
||||||
makeBuilderDef(fullName, name, structNode,
|
makeBuilderDef(fullName, name, schema,
|
||||||
KJ_MAP(f, fieldTexts) { return kj::mv(f.builderMethodDecls); },
|
KJ_MAP(f, fieldTexts) { return kj::mv(f.builderMethodDecls); },
|
||||||
indent + 1)),
|
indent + 1)),
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,6 @@
|
||||||
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");
|
||||||
|
|
Loading…
Reference in a new issue