Implementent MessageBuilder.getRoot() and MessageBuilder.setRoot().

This commit is contained in:
David Renshaw 2015-06-11 22:40:30 -04:00
parent 89e3dff2da
commit 49a5c6ba64
2 changed files with 41 additions and 7 deletions

View file

@ -563,6 +563,32 @@ class EncodingSuite extends FunSuite {
} }
} }
test("StructSetters") {
val builder = new MessageBuilder()
val root = builder.initRoot(TestAllTypes.factory)
TestUtil.initTestMessage(root)
{
val builder2 = new MessageBuilder()
builder2.setRoot(TestAllTypes.factory, root.asReader())
TestUtil.checkTestMessage(builder2.getRoot(TestAllTypes.factory))
}
{
val builder2 = new MessageBuilder()
val root2 = builder2.getRoot(TestAllTypes.factory)
root2.setStructField(root.asReader())
TestUtil.checkTestMessage(root2.getStructField())
}
{
val builder2 = new MessageBuilder()
val root2 = builder2.getRoot(TestAnyPointer.factory)
root2.getAnyPointerField().setAs(TestAllTypes.factory, root.asReader())
TestUtil.checkTestMessage(root2.getAnyPointerField.getAs(TestAllTypes.factory))
}
}
// to debug, do this: // to debug, do this:
//Serialize.write((new java.io.FileOutputStream("/Users/dwrensha/Desktop/test.dat")).getChannel(), //Serialize.write((new java.io.FileOutputStream("/Users/dwrensha/Desktop/test.dat")).getChannel(),

View file

@ -40,11 +40,7 @@ public final class MessageBuilder {
allocationStrategy); allocationStrategy);
} }
public <T> T getRoot(StructBuilder.Factory<T> factory) { private AnyPointer.Builder getRootInternal() {
throw new Error("unimplemented");
}
public <T> T initRoot(FromPointerBuilder<T> factory) {
SegmentBuilder rootSegment = this.arena.segments.get(0); SegmentBuilder rootSegment = this.arena.segments.get(0);
if (rootSegment.currentSize() == 0) { if (rootSegment.currentSize() == 0) {
int location = rootSegment.allocate(1); int location = rootSegment.allocate(1);
@ -54,12 +50,24 @@ public final class MessageBuilder {
if (location != 0) { if (location != 0) {
throw new Error("First allocated word of new segment was not at offset 0"); throw new Error("First allocated word of new segment was not at offset 0");
} }
return factory.initFromPointerBuilder(rootSegment, location, 0); return new AnyPointer.Builder(rootSegment, location);
} else { } else {
return factory.initFromPointerBuilder(rootSegment, 0, 0); return new AnyPointer.Builder(rootSegment, 0);
} }
} }
public <T> T getRoot(FromPointerBuilder<T> factory) {
return this.getRootInternal().getAs(factory);
}
public <T, U> void setRoot(SetPointerBuilder<T, U> factory, U reader) {
this.getRootInternal().setAs(factory, reader);
}
public <T> T initRoot(FromPointerBuilder<T> factory) {
return this.getRootInternal().initAs(factory);
}
public final java.nio.ByteBuffer[] getSegmentsForOutput() { public final java.nio.ByteBuffer[] getSegmentsForOutput() {
return this.arena.getSegmentsForOutput(); return this.arena.getSegmentsForOutput();
} }