From 8b2655623f333dacea52db9303e1b2239c6d0a40 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Tue, 20 May 2014 19:59:40 -0400 Subject: [PATCH] initPeople --- compiler/src/main/cpp/capnpc-java.c++ | 63 +++++++++++++++---- .../capnproto/examples/AddressbookMain.java | 5 +- .../main/java/org/capnproto/ListBuilder.java | 12 ++++ .../main/java/org/capnproto/StructList.java | 4 ++ 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index 49af5e8..6de8cee 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -871,8 +871,44 @@ private: "\n") }; - } else { - // Blob, struct, or list. These have only minor differences. + } else if (kind == FieldKind::STRUCT) { + 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(); kj::String defaultParam = defaultOffset == 0 ? kj::str() : kj::str( @@ -880,6 +916,7 @@ private: defaultSize == 0 ? kj::strTree() : kj::strTree(", ", defaultSize)); kj::String elementReaderType; + kj::String elementBuilderType; bool isStructOrCapList = false; if (kind == FieldKind::LIST) { bool primitiveElement = false; @@ -922,6 +959,9 @@ private: elementReaderType = kj::str( typeName(typeBody.getList().getElementType()), 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), " }\n", - spaces(indent), " public ", type, ".Reader", - (kind == FieldKind::LIST ? - kj::strTree("<", elementReaderType, ">") : - kj::strTree() - ), + spaces(indent), " public final ", type, ".Reader<", elementReaderType, ">", " get", titleCase, "() {\n", (kind == FieldKind::LIST ? kj::strTree(spaces(indent), @@ -948,10 +984,7 @@ private: // XXX what about lists of non-structs? typeName(typeBody.getList().getElementType()),".STRUCT_SIZE.preferredListEncoding), ", elementReaderType, ".factory);\n") : - (kind == FieldKind::BLOB ? - kj::strTree(spaces(indent), " return _reader.getPointerField(", - offset,").getText();\n") : - kj::strTree(spaces(indent), "Struct\n"))), // XXX + kj::strTree(spaces(indent), "Struct\n")), // XXX spaces(indent), " }\n", "\n"), @@ -960,13 +993,15 @@ private: 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), " public final ", type, ".Builder<",elementBuilderType, ">", + " 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, "() {\n", + spaces(indent), " public final ", type, ".Builder<", elementBuilderType,">", + " init", titleCase, "(int size) {\n", spaces(indent), " throw new Error();\n", spaces(indent), " }\n"), @@ -1033,6 +1068,8 @@ private: "}\n" "\n") }; + } else { + KJ_UNREACHABLE; } } diff --git a/examples/src/main/java/org/capnproto/examples/AddressbookMain.java b/examples/src/main/java/org/capnproto/examples/AddressbookMain.java index 928dce1..6ac37c3 100644 --- a/examples/src/main/java/org/capnproto/examples/AddressbookMain.java +++ b/examples/src/main/java/org/capnproto/examples/AddressbookMain.java @@ -13,7 +13,10 @@ public class AddressbookMain { System.out.println("WARNING: writing is not yet fully implemented"); MessageBuilder message = new MessageBuilder(); AddressBook.Builder addressbook = message.initRoot(AddressBook.Builder.factory); - //addressbook.initPeople(2); + StructList.Builder people = addressbook.initPeople(2); + + Person.Builder alice = people.get(0); + //alice.setId(123); } public static void printAddressBook() throws java.io.IOException { diff --git a/runtime/src/main/java/org/capnproto/ListBuilder.java b/runtime/src/main/java/org/capnproto/ListBuilder.java index 4891fca..fda22be 100644 --- a/runtime/src/main/java/org/capnproto/ListBuilder.java +++ b/runtime/src/main/java/org/capnproto/ListBuilder.java @@ -19,6 +19,18 @@ public final class ListBuilder { 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)); + } } diff --git a/runtime/src/main/java/org/capnproto/StructList.java b/runtime/src/main/java/org/capnproto/StructList.java index 6750a07..49df15e 100644 --- a/runtime/src/main/java/org/capnproto/StructList.java +++ b/runtime/src/main/java/org/capnproto/StructList.java @@ -28,6 +28,10 @@ public final class StructList { this.factory = factory; } + public T get(int index) { + return this.factory.fromStructBuilder(this.builder.getStructElement(index)); + } + } }