Data constants

This commit is contained in:
David Renshaw 2014-10-02 15:27:07 -04:00
parent df389a939b
commit c44e73267b
6 changed files with 58 additions and 14 deletions

View file

@ -1420,12 +1420,12 @@ private:
}
case schema::Value::DATA: {
kj::String constType = kj::strTree(
"::capnp::_::ConstData<", schema.as<Data>().size(), ">").flatten();
return ConstText {
true,
kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_",
kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n")
kj::strTree("public static final org.capnproto.Data.Reader ", upperCase,
" = new org.capnproto.Data.Reader(Schemas.b_",
kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(),
", ", constProto.getValue().getData().size(), ");\n")
};
}

View file

@ -85,6 +85,7 @@ class EncodingSuite extends FunSuite {
(-123e45) should equal (TestConstants.FLOAT64_CONST);
(TestConstants.TEXT_CONST.toString()) should equal ("foo");
(TestConstants.DATA_CONST.asArray()) should equal (TestUtil.data("bar"));
(TestConstants.ENUM_CONST) should equal (TestEnum.CORGE);

View file

@ -2,9 +2,16 @@ package org.capnproto;
import org.capnproto.test.Test._;
object TestUtil {
def data(str : String) : Array[Byte] = {
try {
return str.getBytes("ISO_8859-1");
} catch {
case e: Exception => throw new Error("could not decode");
}
}
def initTestMessage(builder : TestAllTypes.Builder) {
builder.setVoidField(org.capnproto.Void.VOID);
builder.setBoolField(true);
@ -18,7 +25,7 @@ object TestUtil {
builder.setUInt64Field(0x1234567890123456L);
builder.setFloat32Field(1234.5f);
builder.setFloat64Field(-123e45);
builder.setTextField(new Text.Reader("foo"));
builder.setTextField("foo");
// builder.setDataField(data("bar"));
{

View file

@ -204,6 +204,7 @@ struct TestConstants {
const float32Const :Float32 = 1234.5;
const float64Const :Float64 = -123e45;
const textConst :Text = "foo";
const dataConst :Data = "bar";
# ...
const enumConst :TestEnum = corge;
}

View file

@ -5,9 +5,46 @@ import java.nio.ByteBuffer;
public final class Data {
public static final class Reader {
public final ByteBuffer buffer;
public final int offset; // in bytes
public final int size; // in bytes
public Reader(ByteBuffer buffer, int offset, int size) {
this.buffer = buffer;
this.offset = offset * 8;
this.size = size;
}
public final int size() {
return this.size;
}
public ByteBuffer asByteBuffer() {
// not thread safe
this.buffer.position(this.offset);
ByteBuffer result = this.buffer.slice();
result.limit(this.size);
return result;
}
public byte[] asArray() {
// not thread safe
byte result[] = new byte[this.size];
this.buffer.position(this.offset);
this.buffer.get(result, 0, this.size);
return result;
}
}
public static final class Builder {
}
public final ByteBuffer buffer;
public final int offset; // in bytes
public final int size; // in bytes
public Builder(ByteBuffer buffer, int offset, int size) {
this.buffer = buffer;
this.offset = offset;
this.size = size;
}
}
}

View file

@ -40,7 +40,7 @@ public final class Text {
try {
return new String(bytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
return "unsupported encoding"; // XXX
throw new Error("UTF-8 is unsupported");
}
}
@ -48,10 +48,8 @@ public final class Text {
public static final class Builder {
public final ByteBuffer buffer;
public final int offset;
// size not including the NUL terminator
public final int size;
public final int offset; // in bytes
public final int size; // in bytes
public Builder(ByteBuffer buffer, int offset, int size) {
this.buffer = buffer;
@ -69,7 +67,7 @@ public final class Text {
try {
return new String(bytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
return "unsupported encoding"; // XXX
throw new Error("UTF-8 is unsupported");
}
}