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

78 lines
3 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 {
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-08 02:35:08 +00:00
public final <T> T initAs(InitFromPointerBuilder<T> factory) {
2014-10-08 16:22:58 +00:00
return factory.initFromPointerBuilder(this.segment, this.pointer);
2014-10-08 02:35:08 +00:00
}
public final <T> T initAs(InitSizedFromPointerBuilder<T> factory, int elementCount) {
2014-10-08 16:22:58 +00:00
return factory.initSizedFromPointerBuilder(this.segment, this.pointer, elementCount);
2014-05-24 14:12:44 +00:00
}
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
}
}