init root
This commit is contained in:
parent
2fa9e7bde9
commit
f0352680a0
8 changed files with 97 additions and 3 deletions
|
@ -9,9 +9,21 @@ public final class AnyPointer {
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getAsStruct(FromStructReader<T> factory) {
|
public final <T> T getAsStruct(FromStructReader<T> factory) {
|
||||||
return factory.fromStructReader(this.reader.getStruct());
|
return factory.fromStructReader(this.reader.getStruct());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class Builder {
|
||||||
|
public final PointerBuilder builder;
|
||||||
|
|
||||||
|
public Builder(PointerBuilder builder) {
|
||||||
|
this.builder = builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final <T> T initAsStruct(FromStructBuilder<T> factory) {
|
||||||
|
return factory.fromStructBuilder(this.builder.initStruct(factory.structSize()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
5
runtime/src/main/java/org/capnproto/Arena.java
Normal file
5
runtime/src/main/java/org/capnproto/Arena.java
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
public interface Arena {
|
||||||
|
public SegmentReader tryGetSegment(int id);
|
||||||
|
}
|
36
runtime/src/main/java/org/capnproto/BuilderArena.java
Normal file
36
runtime/src/main/java/org/capnproto/BuilderArena.java
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
public final class BuilderArena implements Arena {
|
||||||
|
public final Vector<SegmentBuilder> segments;
|
||||||
|
|
||||||
|
public BuilderArena() {
|
||||||
|
this.segments = new Vector<SegmentBuilder>();
|
||||||
|
SegmentBuilder segment0 = new SegmentBuilder(ByteBuffer.allocate(1024 * 8));
|
||||||
|
segment0.buffer.mark();
|
||||||
|
this.segments.add(segment0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SegmentReader tryGetSegment(int id) {
|
||||||
|
throw new Error("unimplemented");
|
||||||
|
}
|
||||||
|
public SegmentBuilder getSegment(int id) {
|
||||||
|
throw new Error("unimplemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AllocateResult {
|
||||||
|
public final SegmentBuilder segment;
|
||||||
|
public final int offset;
|
||||||
|
public AllocateResult(SegmentBuilder segment, int offset) {
|
||||||
|
this.segment = segment;
|
||||||
|
this.offset = offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public AllocateResult allocate(int amount) {
|
||||||
|
throw new Error("unimplemented");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,25 @@
|
||||||
package org.capnproto;
|
package org.capnproto;
|
||||||
|
|
||||||
public final class MessageBuilder {
|
public final class MessageBuilder {
|
||||||
|
|
||||||
|
private final BuilderArena arena;
|
||||||
|
|
||||||
|
public MessageBuilder() {
|
||||||
|
this.arena = new BuilderArena();
|
||||||
|
}
|
||||||
|
|
||||||
public <T> T getRoot(FromStructBuilder<T> factory) {
|
public <T> T getRoot(FromStructBuilder<T> factory) {
|
||||||
throw new Error("unimplemented");
|
throw new Error("unimplemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T initRoot(FromStructBuilder<T> factory) {
|
public <T> T initRoot(FromStructBuilder<T> factory) {
|
||||||
throw new Error("unimplemented");
|
SegmentBuilder rootSegment = this.arena.segments.get(0);
|
||||||
|
int location = rootSegment.allocate(1);
|
||||||
|
if (location == SegmentBuilder.FAILED_ALLOCATION) {
|
||||||
|
throw new Error("could not allocate root pointer");
|
||||||
|
}
|
||||||
|
|
||||||
|
AnyPointer.Builder ptr = new AnyPointer.Builder(PointerBuilder.getRoot(rootSegment, location));
|
||||||
|
return ptr.initAsStruct(factory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,18 @@ public final class PointerBuilder {
|
||||||
this.pointer = pointer;
|
this.pointer = pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static PointerBuilder getRoot(SegmentBuilder segment, int location) {
|
||||||
|
return new PointerBuilder(segment, location);
|
||||||
|
}
|
||||||
|
|
||||||
public final boolean isNull() {
|
public final boolean isNull() {
|
||||||
return this.segment.buffer.getLong(this.pointer) == 0;
|
return this.segment.buffer.getLong(this.pointer) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final StructBuilder initStruct(StructSize size) {
|
||||||
|
return WireHelpers.initStructPointer(this.pointer, this.segment, size);
|
||||||
|
}
|
||||||
|
|
||||||
public final ListBuilder initStructList(int elementCount, StructSize elementSize) {
|
public final ListBuilder initStructList(int elementCount, StructSize elementSize) {
|
||||||
return WireHelpers.initStructListPointer(this.pointer, this.segment, elementCount, elementSize);
|
return WireHelpers.initStructListPointer(this.pointer, this.segment, elementCount, elementSize);
|
||||||
}
|
}
|
||||||
|
|
7
runtime/src/main/java/org/capnproto/ReaderArena.java
Normal file
7
runtime/src/main/java/org/capnproto/ReaderArena.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
public final class ReaderArena implements Arena {
|
||||||
|
public SegmentReader tryGetSegment(int id) {
|
||||||
|
throw new Error("unimplemented");
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,9 @@ public final class SegmentBuilder extends SegmentReader {
|
||||||
return this.pos;
|
return this.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Allocate `amount` words.
|
||||||
|
*/
|
||||||
public final int allocate(int amount) {
|
public final int allocate(int amount) {
|
||||||
if (amount > this.capacity() - this.currentSize()) {
|
if (amount > this.capacity() - this.currentSize()) {
|
||||||
return FAILED_ALLOCATION; // no space left;
|
return FAILED_ALLOCATION; // no space left;
|
||||||
|
|
|
@ -25,6 +25,15 @@ final class WireHelpers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static StructBuilder initStructPointer(int refOffset,
|
||||||
|
SegmentBuilder segment,
|
||||||
|
StructSize size) {
|
||||||
|
int ptrOffset = allocate(refOffset, segment, size.total(), WirePointer.STRUCT);
|
||||||
|
StructPointer.setFromStructSize(segment.buffer, refOffset, size);
|
||||||
|
return new StructBuilder(segment, ptrOffset * 8, ptrOffset + size.data,
|
||||||
|
size.data * 64, size.pointers, (byte)0);
|
||||||
|
}
|
||||||
|
|
||||||
public static ListBuilder initListPointer(int refOffset,
|
public static ListBuilder initListPointer(int refOffset,
|
||||||
SegmentBuilder segment,
|
SegmentBuilder segment,
|
||||||
int elementCount,
|
int elementCount,
|
||||||
|
@ -56,7 +65,7 @@ final class WireHelpers {
|
||||||
|
|
||||||
ptrOffset += 1;
|
ptrOffset += 1;
|
||||||
|
|
||||||
return new ListBuilder(segment, ptrOffset, elementCount, wordsPerElement * 64,
|
return new ListBuilder(segment, ptrOffset * 8, elementCount, wordsPerElement * 64,
|
||||||
elementSize.data * 64, elementSize.pointers);
|
elementSize.data * 64, elementSize.pointers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue