Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate AnyStruct and AnyList #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions compiler/src/main/cpp/capnpc-java.c++
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,14 @@ private:
"_", kj::hex(brandParam->scopeId), "_", suffix);

} else {
return kj::strTree("org.capnproto.AnyPointer.", suffix);
switch (type.whichAnyPointerKind()) {
case schema::Type::AnyPointer::Unconstrained::STRUCT:
return kj::strTree("org.capnproto.AnyStruct.", suffix);
case schema::Type::AnyPointer::Unconstrained::LIST:
return kj::strTree("org.capnproto.AnyList.", suffix);
default:
return kj::strTree("org.capnproto.AnyPointer.", suffix);
}
}
}
}
Expand Down Expand Up @@ -747,7 +754,14 @@ private:
"_", kj::hex(brandParam->scopeId), "_Factory");

} else {
return kj::str("org.capnproto.AnyPointer.factory");
switch (type.whichAnyPointerKind()) {
case schema::Type::AnyPointer::Unconstrained::STRUCT:
return kj::str("org.capnproto.AnyStruct.factory");
case schema::Type::AnyPointer::Unconstrained::LIST:
return kj::str("org.capnproto.AnyList.factory");
default:
return kj::str("org.capnproto.AnyPointer.factory");
}
}
}
case schema::Type::STRUCT : {
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/test/schema/test.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ struct TestAnyPointer {
# in the struct.
}

struct TestAnyOthers {
anyStructField @0 :AnyStruct;
anyListField @1 :AnyList;
}

struct TestOutOfOrder {
foo @3 :Text;
bar @2 :Text;
Expand Down
77 changes: 77 additions & 0 deletions runtime/src/main/java/org/capnproto/AnyList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package org.capnproto;

public final class AnyList {

public static final class Factory extends ListFactory<Builder, Reader> {
Factory() {
super(ElementSize.VOID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this end up giving a wrong value for the expectedElementSize parameter of WireHelpers.readListPointer()? Maybe we need to add a checkElementSize parameter, like in capnproto-c++? https://github.com/capnproto/capnproto/blob/master/c%2B%2B/src/capnp/layout.c%2B%2B#L2237

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That probably explains why my round-trip test for AnyList fails!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right in thinking that the actual element size has to be written out when initListPointer is called?
In that case, I think I need to allow the user to pass the appropriate factory to the generated accessor when initiialising the list for building.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. For a field

  foo @1 :AnyList

I think we should generate a method like public <B> B initFooAs(ListFactory<B, ?> factory, int size).
(capnproto-c++ does something similar)

}

public final Reader asReader(Builder builder) {
return builder.asReader();
}

@Override
public Builder constructBuilder(SegmentBuilder segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount) {
return new Builder(segment, ptr, elementCount, step, structDataSize, structPointerCount);
}

@Override
public Reader constructReader(SegmentReader segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount, int nestingLimit) {
return new Reader(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit);
}
}

public static final Factory factory = new Factory();

public static final ListList.Factory<Builder,Reader> listFactory = new ListList.Factory<>(factory);

public static final class Builder extends ListBuilder {
Builder(SegmentBuilder segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount) {
super(segment, ptr, elementCount, step, structDataSize, structPointerCount);
}

public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step, this.structDataSize, this.structPointerCount, 0x7fffffff);
}

public final <T> T initAs(Factory<T> factory) {
return factory.constructBuilder(this.segment, this.ptr, this.elementCount, this.step, this.structDataSize, this.structPointerCount);
}

public final <T> T setAs(Factory<T> factory) {
return factory.constructBuilder(this.segment, this.ptr, this.elementCount, this.step, this.structDataSize, this.structPointerCount);
}
}

public static final class Reader extends ListReader {
Reader(SegmentReader segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount, int nestingLimit) {
super(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit);
}

public final <T> T getAs(Factory<T> factory) {
return factory.constructReader(this.segment, this.ptr, this.elementCount, this.step, this.structDataSize, this.structPointerCount, this.nestingLimit);
}
}
}
81 changes: 81 additions & 0 deletions runtime/src/main/java/org/capnproto/AnyStruct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package org.capnproto;

public final class AnyStruct {

public static final StructSize STRUCT_SIZE = new StructSize((short)0,(short)0);

public static final class Factory extends StructFactory<Builder, Reader> {
public Factory() {
}

public final Reader constructReader(SegmentReader segment, int data,int pointers, int dataSize, short pointerCount, int nestingLimit) {
return new Reader(segment, data, pointers, dataSize, pointerCount, nestingLimit);
}

public final Builder constructBuilder(SegmentBuilder segment, int data,int pointers, int dataSize, short pointerCount) {
return new Builder(segment, data, pointers, dataSize, pointerCount);
}

public final StructSize structSize() {
return AnyStruct.STRUCT_SIZE;
}

public final Reader asReader(Builder builder) {
return builder.asReader();
}
}

public static final Factory factory = new Factory();

public static final StructList.Factory<Builder, Reader> listFactory =
new StructList.Factory<>(factory);

public static final class Builder extends StructBuilder {
Builder(SegmentBuilder segment, int data, int pointers,int dataSize, short pointerCount){
super(segment, data, pointers, dataSize, pointerCount);
}

public final Reader asReader() {
return new Reader(segment, data, pointers, dataSize, pointerCount, 0x7fffffff);
}

public final <T> T initAs(StructBuilder.Factory<T> factory) {
return factory.constructBuilder(this.segment, this.data, this.pointers, this.dataSize, this.pointerCount);
}

public final <T> T setAs(StructBuilder.Factory<T> factory) {
return factory.constructBuilder(this.segment, this.data, this.pointers, this.dataSize, this.pointerCount);
}
}

public static final class Reader extends StructReader {
Reader(SegmentReader segment, int data, int pointers,int dataSize, short pointerCount, int nestingLimit){
super(segment, data, pointers, dataSize, pointerCount, nestingLimit);
}

public final <T> T getAs(StructReader.Factory<T> factory) {
return factory.constructReader(this.segment, this.data, this.pointers, this.dataSize, this.pointerCount, this.nestingLimit);
}
}
}