Skip to content

Commit

Permalink
default text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Oct 3, 2014
1 parent b269458 commit dc771a0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
8 changes: 6 additions & 2 deletions compiler/src/main/cpp/capnpc-java.c++
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,10 @@ private:

} else if (kind == FieldKind::BLOB && typeBody.which() == schema::Type::TEXT ) {

uint64_t typeId = field.getContainingStruct().getProto().getId();
kj::String defaultParams = defaultOffset == 0 ? kj::str() : kj::str(
"Schemas.b_", kj::hex(typeId), ", ", defaultOffset * 8, ", ", defaultSize);

kj::String blobKind = kj::str("Text");

return FieldText {
Expand All @@ -955,7 +959,7 @@ private:
spaces(indent), " public ", type, ".Reader",
" get", titleCase, "() {\n",
spaces(indent), " return _reader.getPointerField(",
offset, ").get", blobKind, " ();\n", // XXX
offset, ").getText(", defaultParams, ");\n",
spaces(indent), " }\n", "\n"),

kj::strTree(
Expand All @@ -966,7 +970,7 @@ private:
spaces(indent), " }\n",
spaces(indent), " public final ", type, ".Builder get", titleCase, "() {\n",
spaces(indent), " return _builder.getPointerField(",
offset, ").get", blobKind, " ();\n", // XXX
offset, ").get", blobKind, "(", defaultParams, ");\n",
spaces(indent), " }\n",
spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n",
unionDiscrim.set,
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/test/scala/org/capnproto/TestUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ object TestUtil {
assert(builder.getUInt64Field() == 0xab54a98ceb1f0ad2L);
assert(builder.getFloat32Field() == 1234.5f);
assert(builder.getFloat64Field() == -123e45);
(builder.getTextField().toString()) should equal ("foo");
}

def checkDefaultMessage(reader : TestDefaults.Reader) {
Expand All @@ -252,6 +253,7 @@ object TestUtil {
assert(reader.getUInt64Field() == 0xab54a98ceb1f0ad2L);
assert(reader.getFloat32Field() == 1234.5f);
assert(reader.getFloat64Field() == -123e45);
(reader.getTextField().toString()) should equal ("foo");
}


Expand Down
1 change: 1 addition & 0 deletions compiler/src/test/schema/test.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct TestDefaults {
uInt64Field @9 : UInt64 = 12345678901234567890;
float32Field @10 : Float32 = 1234.5;
float64Field @11 : Float64 = -123e45;
textField @12 : Text = "foo";
}

struct TestAnyPointer {
Expand Down
10 changes: 9 additions & 1 deletion runtime/src/main/java/org/capnproto/PointerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public final ListBuilder getStructList(StructSize elementSize) {

public final Text.Builder getText() {
return WireHelpers.getWritableTextPointer(
this.pointer, this.segment);
this.pointer, this.segment, null, 0, 0);
}

public Text.Builder getText(java.nio.ByteBuffer defaultBuffer, int defaultOffset, int defaultSize) {
return WireHelpers.getWritableTextPointer(this.pointer,
this.segment,
defaultBuffer,
defaultOffset,
defaultSize);
}

public final Data.Builder getData() {
Expand Down
7 changes: 5 additions & 2 deletions runtime/src/main/java/org/capnproto/PointerReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ public ListReader getList(byte expectedElementSize) {
}

public Text.Reader getText() {
return WireHelpers.readTextPointer(this.segment,
this.pointer);
return WireHelpers.readTextPointer(this.segment, this.pointer, null, 0, 0);
}

public Text.Reader getText(java.nio.ByteBuffer defaultBuffer, int defaultOffset, int defaultSize) {
return WireHelpers.readTextPointer(this.segment, this.pointer, defaultBuffer, defaultOffset, defaultSize);
}

public Data.Reader getData() {
Expand Down
32 changes: 26 additions & 6 deletions runtime/src/main/java/org/capnproto/WireHelpers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.capnproto;

import java.nio.ByteBuffer;

final class WireHelpers {

public static int roundBytesUpToWords(int bytes) {
Expand Down Expand Up @@ -310,12 +312,23 @@ public static Text.Builder setTextPointer(int refOffset,
}

public static Text.Builder getWritableTextPointer(int refOffset,
SegmentBuilder segment) {
SegmentBuilder segment,
ByteBuffer defaultBuffer,
int defaultOffset,
int defaultSize) {
long ref = WirePointer.get(segment.buffer, refOffset);

if (WirePointer.isNull(ref)) {
// TODO default values
return new Text.Builder(null, 0, 0);
if (defaultBuffer == null) {
return new Text.Builder(null, 0, 0);
} else {
Text.Builder builder = initTextPointer(refOffset, segment, defaultSize);
// TODO is there a way to do this with bulk methods?
for (int i = 0; i < builder.size; ++i) {
builder.buffer.put(builder.offset + i, defaultBuffer.get(defaultOffset + i));
}
return builder;
}
}

int refTarget = WirePointer.target(refOffset, ref);
Expand Down Expand Up @@ -454,12 +467,19 @@ public static ListReader readListPointer(SegmentReader segment,
}

public static Text.Reader readTextPointer(SegmentReader segment,
int refOffset) {
int refOffset,
ByteBuffer defaultBuffer,
int defaultOffset,
int defaultSize) {
long ref = WirePointer.get(segment.buffer, refOffset);

if (WirePointer.isNull(ref)) {
// XXX should use the default value
return new Text.Reader(java.nio.ByteBuffer.wrap(new byte[0]), 0, 0);
if (defaultBuffer == null) {
// XXX -- what about null terminator?
return new Text.Reader(ByteBuffer.wrap(new byte[0]), 0, 0);
} else {
return new Text.Reader(defaultBuffer, defaultOffset, defaultSize);
}
}

int refTarget = WirePointer.target(refOffset, ref);
Expand Down

0 comments on commit dc771a0

Please sign in to comment.