Skip to content

Commit

Permalink
thread safety; const indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Oct 3, 2014
1 parent 7e49ede commit b269458
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
36 changes: 18 additions & 18 deletions compiler/src/main/cpp/capnpc-java.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ private:
kj::StringTree decl;
};

ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema) {
ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema, int indent) {
auto proto = schema.getProto();
auto constProto = proto.getConst();
auto type = constProto.getType();
Expand All @@ -1339,14 +1339,15 @@ private:
case schema::Value::ENUM:
return ConstText {
false,
kj::strTree("public static final ", typeName_, ' ', upperCase, " = ",
literalValue(constProto.getType(), constProto.getValue()), ";\n")
kj::strTree(spaces(indent), "public static final ", typeName_, ' ', upperCase, " = ",
literalValue(constProto.getType(), constProto.getValue()), ";\n")
};

case schema::Value::TEXT: {
return ConstText {
true,
kj::strTree("public static final org.capnproto.Text.Reader ", upperCase,
kj::strTree(spaces(indent),
"public static final org.capnproto.Text.Reader ", upperCase,
" = new org.capnproto.Text.Reader(Schemas.b_",
kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(),
", ", constProto.getValue().getText().size(), ");\n")
Expand All @@ -1356,7 +1357,8 @@ private:
case schema::Value::DATA: {
return ConstText {
true,
kj::strTree("public static final org.capnproto.Data.Reader ", upperCase,
kj::strTree(spaces(indent),
"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")
Expand Down Expand Up @@ -1591,20 +1593,18 @@ private:

return NodeTextNoSchema {
kj::strTree(
spaces(indent), "public enum ", name, " {\n",
KJ_MAP(e, enumerants) {
return kj::strTree(spaces(indent), " ", toUpperCase(e.getProto().getName()), ",\n");
},
spaces(indent), " _UNKNOWN,\n",
spaces(indent), "}\n"
"\n"),
spaces(indent), "public enum ", name, " {\n",
KJ_MAP(e, enumerants) {
return kj::strTree(spaces(indent), " ", toUpperCase(e.getProto().getName()), ",\n");
},
spaces(indent), " _UNKNOWN,\n",
spaces(indent), "}\n"
"\n"),

kj::strTree(),
kj::strTree(),

kj::strTree(),
kj::strTree(),

kj::strTree(),
kj::strTree(),
kj::strTree(),
};
}
Expand All @@ -1615,10 +1615,10 @@ private:
}

case schema::Node::CONST: {
auto constText = makeConstText(scope, name, schema.asConst());
auto constText = makeConstText(scope, name, schema.asConst(), indent);

return NodeTextNoSchema {
kj::strTree(" ", kj::mv(constText.decl)),
kj::strTree(kj::mv(constText.decl)),
kj::strTree(),
kj::strTree(),

Expand Down
12 changes: 6 additions & 6 deletions runtime/src/main/java/org/capnproto/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public final int size() {
}

public ByteBuffer asByteBuffer() {
// not thread safe
this.buffer.position(this.offset);
ByteBuffer result = this.buffer.slice();
ByteBuffer dup = this.buffer.duplicate();
dup.position(this.offset);
ByteBuffer result = dup.slice();
result.limit(this.size);
return result;
}

public byte[] asArray() {
// not thread safe
ByteBuffer dup = this.buffer.duplicate();
byte result[] = new byte[this.size];
this.buffer.position(this.offset);
this.buffer.get(result, 0, this.size);
dup.position(this.offset);
dup.get(result, 0, this.size);
return result;
}
}
Expand Down
10 changes: 6 additions & 4 deletions runtime/src/main/java/org/capnproto/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public final int size() {
public final String toString() {
byte[] bytes = new byte[this.size];

this.buffer.position(this.offset);
this.buffer.get(bytes, 0, this.size);
ByteBuffer dup = this.buffer.duplicate();
dup.position(this.offset);
dup.get(bytes, 0, this.size);

try {
return new String(bytes, "UTF-8");
Expand All @@ -61,8 +62,9 @@ public Builder(ByteBuffer buffer, int offset, int size) {
public final String toString() {
byte[] bytes = new byte[this.size];

this.buffer.position(this.offset);
this.buffer.get(bytes, 0, this.size);
ByteBuffer dup = this.buffer.duplicate();
dup.position(this.offset);
dup.get(bytes, 0, this.size);

try {
return new String(bytes, "UTF-8");
Expand Down

0 comments on commit b269458

Please sign in to comment.