initPeople

This commit is contained in:
David Renshaw 2014-05-20 19:59:40 -04:00
parent 971ce0a798
commit 8b2655623f
4 changed files with 70 additions and 14 deletions

View file

@ -871,8 +871,44 @@ private:
"\n") "\n")
}; };
} else { } else if (kind == FieldKind::STRUCT) {
// Blob, struct, or list. These have only minor differences. KJ_FAIL_REQUIRE("unimplemented");
} else if (kind == FieldKind::BLOB) {
return FieldText {
kj::strTree(
kj::mv(unionDiscrim.readerIsDecl),
spaces(indent), " public boolean has", titleCase, "() {\n",
spaces(indent), " return !_reader.getPointerField(", offset, ").isNull();\n",
spaces(indent), " }\n",
spaces(indent), " public ", type, ".Reader",
" get", titleCase, "() {\n",
spaces(indent), " return _reader.getPointerField(",
offset, ").getText();\n", // XXX
spaces(indent), " }\n", "\n"),
kj::strTree(
kj::mv(unionDiscrim.builderIsDecl),
spaces(indent), " public final boolean has", titleCase, "() {\n",
spaces(indent), " return !_builder.getPointerField(", offset, ").isNull();\n",
spaces(indent), " }\n",
spaces(indent), " public final ", type, ".Builder get", titleCase, "() {\n",
spaces(indent), " throw new Error();\n",
spaces(indent), " }\n",
spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n",
spaces(indent), " throw new Error();\n",
spaces(indent), " }\n",
spaces(indent), " public final ", type, ".Builder init", titleCase, "(int size) {\n",
spaces(indent), " throw new Error();\n",
spaces(indent), " }\n"),
kj::strTree(),
kj::strTree()
};
} else if (kind == FieldKind::LIST) {
uint64_t typeId = field.getContainingStruct().getProto().getId(); uint64_t typeId = field.getContainingStruct().getProto().getId();
kj::String defaultParam = defaultOffset == 0 ? kj::str() : kj::str( kj::String defaultParam = defaultOffset == 0 ? kj::str() : kj::str(
@ -880,6 +916,7 @@ private:
defaultSize == 0 ? kj::strTree() : kj::strTree(", ", defaultSize)); defaultSize == 0 ? kj::strTree() : kj::strTree(", ", defaultSize));
kj::String elementReaderType; kj::String elementReaderType;
kj::String elementBuilderType;
bool isStructOrCapList = false; bool isStructOrCapList = false;
if (kind == FieldKind::LIST) { if (kind == FieldKind::LIST) {
bool primitiveElement = false; bool primitiveElement = false;
@ -922,6 +959,9 @@ private:
elementReaderType = kj::str( elementReaderType = kj::str(
typeName(typeBody.getList().getElementType()), typeName(typeBody.getList().getElementType()),
primitiveElement ? "" : interface ? "::Client" : ".Reader"); primitiveElement ? "" : interface ? "::Client" : ".Reader");
elementBuilderType = kj::str(
typeName(typeBody.getList().getElementType()),
primitiveElement ? "" : interface ? "::Client" : ".Builder");
} }
@ -932,11 +972,7 @@ private:
spaces(indent), " return !_reader.getPointerField(", offset, ").isNull();\n", spaces(indent), " return !_reader.getPointerField(", offset, ").isNull();\n",
spaces(indent), " }\n", spaces(indent), " }\n",
spaces(indent), " public ", type, ".Reader", spaces(indent), " public final ", type, ".Reader<", elementReaderType, ">",
(kind == FieldKind::LIST ?
kj::strTree("<", elementReaderType, ">") :
kj::strTree()
),
" get", titleCase, "() {\n", " get", titleCase, "() {\n",
(kind == FieldKind::LIST ? (kind == FieldKind::LIST ?
kj::strTree(spaces(indent), kj::strTree(spaces(indent),
@ -948,10 +984,7 @@ private:
// XXX what about lists of non-structs? // XXX what about lists of non-structs?
typeName(typeBody.getList().getElementType()),".STRUCT_SIZE.preferredListEncoding), ", typeName(typeBody.getList().getElementType()),".STRUCT_SIZE.preferredListEncoding), ",
elementReaderType, ".factory);\n") : elementReaderType, ".factory);\n") :
(kind == FieldKind::BLOB ? kj::strTree(spaces(indent), "Struct\n")), // XXX
kj::strTree(spaces(indent), " return _reader.getPointerField(",
offset,").getText();\n") :
kj::strTree(spaces(indent), "Struct\n"))), // XXX
spaces(indent), " }\n", spaces(indent), " }\n",
"\n"), "\n"),
@ -960,13 +993,15 @@ private:
spaces(indent), " public final boolean has", titleCase, "() {\n", spaces(indent), " public final boolean has", titleCase, "() {\n",
spaces(indent), " return !_builder.getPointerField(", offset, ").isNull();\n", spaces(indent), " return !_builder.getPointerField(", offset, ").isNull();\n",
spaces(indent), " }\n", spaces(indent), " }\n",
spaces(indent), " public final ", type, ".Builder get", titleCase, "() {\n", spaces(indent), " public final ", type, ".Builder<",elementBuilderType, ">",
" get", titleCase, "() {\n",
spaces(indent), " throw new Error();\n", spaces(indent), " throw new Error();\n",
spaces(indent), " }\n", spaces(indent), " }\n",
spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n", spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n",
spaces(indent), " throw new Error();\n", spaces(indent), " throw new Error();\n",
spaces(indent), " }\n", spaces(indent), " }\n",
spaces(indent), " public final ", type, ".Builder init", titleCase, "() {\n", spaces(indent), " public final ", type, ".Builder<", elementBuilderType,">",
" init", titleCase, "(int size) {\n",
spaces(indent), " throw new Error();\n", spaces(indent), " throw new Error();\n",
spaces(indent), " }\n"), spaces(indent), " }\n"),
@ -1033,6 +1068,8 @@ private:
"}\n" "}\n"
"\n") "\n")
}; };
} else {
KJ_UNREACHABLE;
} }
} }

View file

@ -13,7 +13,10 @@ public class AddressbookMain {
System.out.println("WARNING: writing is not yet fully implemented"); System.out.println("WARNING: writing is not yet fully implemented");
MessageBuilder message = new MessageBuilder(); MessageBuilder message = new MessageBuilder();
AddressBook.Builder addressbook = message.initRoot(AddressBook.Builder.factory); AddressBook.Builder addressbook = message.initRoot(AddressBook.Builder.factory);
//addressbook.initPeople(2); StructList.Builder<Person.Builder> people = addressbook.initPeople(2);
Person.Builder alice = people.get(0);
//alice.setId(123);
} }
public static void printAddressBook() throws java.io.IOException { public static void printAddressBook() throws java.io.IOException {

View file

@ -19,6 +19,18 @@ public final class ListBuilder {
this.structPointerCount = structPointerCount; this.structPointerCount = structPointerCount;
} }
public final StructBuilder getStructElement(int index) {
int indexBit = index * this.step;
int structData = this.ptr + indexBit / 8 ;
int structPointers = (structData + (this.structDataSize / 8)) / 8;
return new StructBuilder(this.segment,
structData,
structPointers,
this.structDataSize,
this.structPointerCount,
(byte)(indexBit % 8));
}
} }

View file

@ -28,6 +28,10 @@ public final class StructList {
this.factory = factory; this.factory = factory;
} }
public T get(int index) {
return this.factory.fromStructBuilder(this.builder.getStructElement(index));
}
} }
} }