From 78959fda88730580a09df23944dc8f15b160dc0c Mon Sep 17 00:00:00 2001 From: Vaci Koblizek Date: Wed, 16 Dec 2020 14:17:37 +0000 Subject: [PATCH] generate AnyStruct and AnyList --- compiler/src/main/cpp/capnpc-java.c++ | 18 ++++- compiler/src/test/schema/test.capnp | 5 ++ .../src/main/java/org/capnproto/AnyList.java | 77 ++++++++++++++++++ .../main/java/org/capnproto/AnyStruct.java | 81 +++++++++++++++++++ 4 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 runtime/src/main/java/org/capnproto/AnyList.java create mode 100644 runtime/src/main/java/org/capnproto/AnyStruct.java diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index 86d558be..197d5e5a 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -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); + } } } } @@ -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 : { diff --git a/compiler/src/test/schema/test.capnp b/compiler/src/test/schema/test.capnp index af2f92da..712bc3cd 100644 --- a/compiler/src/test/schema/test.capnp +++ b/compiler/src/test/schema/test.capnp @@ -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; diff --git a/runtime/src/main/java/org/capnproto/AnyList.java b/runtime/src/main/java/org/capnproto/AnyList.java new file mode 100644 index 00000000..80fa06c0 --- /dev/null +++ b/runtime/src/main/java/org/capnproto/AnyList.java @@ -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 { + Factory() { + super(ElementSize.VOID); + } + + 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 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 initAs(Factory factory) { + return factory.constructBuilder(this.segment, this.ptr, this.elementCount, this.step, this.structDataSize, this.structPointerCount); + } + + public final T setAs(Factory 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 getAs(Factory factory) { + return factory.constructReader(this.segment, this.ptr, this.elementCount, this.step, this.structDataSize, this.structPointerCount, this.nestingLimit); + } + } +} diff --git a/runtime/src/main/java/org/capnproto/AnyStruct.java b/runtime/src/main/java/org/capnproto/AnyStruct.java new file mode 100644 index 00000000..50222bee --- /dev/null +++ b/runtime/src/main/java/org/capnproto/AnyStruct.java @@ -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 { + 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 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 initAs(StructBuilder.Factory factory) { + return factory.constructBuilder(this.segment, this.data, this.pointers, this.dataSize, this.pointerCount); + } + + public final T setAs(StructBuilder.Factory 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 getAs(StructReader.Factory factory) { + return factory.constructReader(this.segment, this.data, this.pointers, this.dataSize, this.pointerCount, this.nestingLimit); + } + } +}