capnproto-java-rpc/runtime/src/main/java/org/capnproto/AnyPointer.java

100 lines
3.9 KiB
Java
Raw Normal View History

2014-10-08 20:20:15 +00:00
// 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 AnyPointer {
2014-10-28 01:14:16 +00:00
public static final class Factory implements PointerFactory<Builder, Reader> {
public final Reader fromPointerReader(SegmentReader segment, int pointer, int nestingLimit) {
return new Reader(segment, pointer, nestingLimit);
}
public final Builder fromPointerBuilder(SegmentBuilder segment, int pointer) {
return new Builder(segment, pointer);
}
2014-10-28 16:51:01 +00:00
public final Builder initFromPointerBuilder(SegmentBuilder segment, int pointer, int elementCount) {
Builder result = new Builder(segment, pointer);
result.clear();
return result;
}
2014-10-28 01:14:16 +00:00
}
2014-10-28 22:23:51 +00:00
public static final Factory factory = new Factory();
public final static class Reader {
2014-10-08 15:12:52 +00:00
final SegmentReader segment;
2014-10-15 00:17:07 +00:00
final int pointer; // offset in words
2014-10-08 15:12:52 +00:00
final int nestingLimit;
public Reader(SegmentReader segment, int pointer, int nestingLimit) {
this.segment = segment;
this.pointer = pointer;
this.nestingLimit = nestingLimit;
}
2014-10-15 00:17:07 +00:00
public final boolean isNull() {
return WirePointer.isNull(this.segment.buffer.getLong(this.pointer * Constants.BYTES_PER_WORD));
}
2014-10-08 02:35:08 +00:00
public final <T> T getAs(FromPointerReader<T> factory) {
2014-10-08 17:37:26 +00:00
return factory.fromPointerReader(this.segment, this.pointer, this.nestingLimit);
2014-10-06 15:15:33 +00:00
}
}
2014-05-24 14:12:44 +00:00
public static final class Builder {
2014-10-08 16:22:58 +00:00
final SegmentBuilder segment;
final int pointer;
2014-05-24 14:12:44 +00:00
2014-10-08 16:22:58 +00:00
public Builder(SegmentBuilder segment, int pointer) {
this.segment = segment;
this.pointer = pointer;
2014-05-24 14:12:44 +00:00
}
2014-10-15 00:17:07 +00:00
public final boolean isNull() {
return WirePointer.isNull(this.segment.buffer.getLong(this.pointer * Constants.BYTES_PER_WORD));
}
2014-10-08 19:16:17 +00:00
public final <T> T getAs(FromPointerBuilder<T> factory) {
return factory.fromPointerBuilder(this.segment, this.pointer);
}
2014-10-28 16:51:01 +00:00
public final <T> T initAs(FromPointerBuilder<T> factory) {
return factory.initFromPointerBuilder(this.segment, this.pointer, 0);
2014-10-08 02:35:08 +00:00
}
2014-10-28 16:51:01 +00:00
public final <T> T initAs(FromPointerBuilder<T> factory, int elementCount) {
return factory.initFromPointerBuilder(this.segment, this.pointer, elementCount);
2014-05-24 14:12:44 +00:00
}
2015-03-09 00:39:44 +00:00
public final <T, U> void setAs(SetPointerBuilder<T, U> factory, U reader) {
factory.setPointerBuilder(this.segment, this.pointer, reader);
}
public final Reader asReader() {
return new Reader(segment, pointer, 0x7fffffff);
}
public final void clear() {
2014-10-08 16:22:58 +00:00
WireHelpers.zeroObject(this.segment, this.pointer);
this.segment.buffer.putLong(this.pointer * 8, 0L);
}
2014-05-24 14:12:44 +00:00
}
}