annotate win64-msvc/include/capnp/pointer-helpers.h @ 47:d93140aac40b

Current Capnp libs and headers from git
author Chris Cannam
date Thu, 20 Oct 2016 18:15:38 +0100
parents
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef CAPNP_POINTER_HELPERS_H_
Chris@47 23 #define CAPNP_POINTER_HELPERS_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #include "layout.h"
Chris@47 30 #include "list.h"
Chris@47 31
Chris@47 32 namespace capnp {
Chris@47 33 namespace _ { // private
Chris@47 34
Chris@47 35 // PointerHelpers is a template class that assists in wrapping/unwrapping the low-level types in
Chris@47 36 // layout.h with the high-level public API and generated types. This way, the code generator
Chris@47 37 // and other templates do not have to specialize on each kind of pointer.
Chris@47 38
Chris@47 39 template <typename T>
Chris@47 40 struct PointerHelpers<T, Kind::STRUCT> {
Chris@47 41 static inline typename T::Reader get(PointerReader reader, const word* defaultValue = nullptr) {
Chris@47 42 return typename T::Reader(reader.getStruct(defaultValue));
Chris@47 43 }
Chris@47 44 static inline typename T::Builder get(PointerBuilder builder,
Chris@47 45 const word* defaultValue = nullptr) {
Chris@47 46 return typename T::Builder(builder.getStruct(structSize<T>(), defaultValue));
Chris@47 47 }
Chris@47 48 static inline void set(PointerBuilder builder, typename T::Reader value) {
Chris@47 49 builder.setStruct(value._reader);
Chris@47 50 }
Chris@47 51 static inline void setCanonical(PointerBuilder builder, typename T::Reader value) {
Chris@47 52 builder.setStruct(value._reader, true);
Chris@47 53 }
Chris@47 54 static inline typename T::Builder init(PointerBuilder builder) {
Chris@47 55 return typename T::Builder(builder.initStruct(structSize<T>()));
Chris@47 56 }
Chris@47 57 static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
Chris@47 58 builder.adopt(kj::mv(value.builder));
Chris@47 59 }
Chris@47 60 static inline Orphan<T> disown(PointerBuilder builder) {
Chris@47 61 return Orphan<T>(builder.disown());
Chris@47 62 }
Chris@47 63 static inline _::StructReader getInternalReader(const typename T::Reader& reader) {
Chris@47 64 return reader._reader;
Chris@47 65 }
Chris@47 66 static inline _::StructBuilder getInternalBuilder(typename T::Builder&& builder) {
Chris@47 67 return builder._builder;
Chris@47 68 }
Chris@47 69 };
Chris@47 70
Chris@47 71 template <typename T>
Chris@47 72 struct PointerHelpers<List<T>, Kind::LIST> {
Chris@47 73 static inline typename List<T>::Reader get(PointerReader reader,
Chris@47 74 const word* defaultValue = nullptr) {
Chris@47 75 return typename List<T>::Reader(List<T>::getFromPointer(reader, defaultValue));
Chris@47 76 }
Chris@47 77 static inline typename List<T>::Builder get(PointerBuilder builder,
Chris@47 78 const word* defaultValue = nullptr) {
Chris@47 79 return typename List<T>::Builder(List<T>::getFromPointer(builder, defaultValue));
Chris@47 80 }
Chris@47 81 static inline void set(PointerBuilder builder, typename List<T>::Reader value) {
Chris@47 82 builder.setList(value.reader);
Chris@47 83 }
Chris@47 84 static inline void setCanonical(PointerBuilder builder, typename List<T>::Reader value) {
Chris@47 85 builder.setList(value.reader, true);
Chris@47 86 }
Chris@47 87 static void set(PointerBuilder builder, kj::ArrayPtr<const ReaderFor<T>> value) {
Chris@47 88 auto l = init(builder, value.size());
Chris@47 89 uint i = 0;
Chris@47 90 for (auto& element: value) {
Chris@47 91 l.set(i++, element);
Chris@47 92 }
Chris@47 93 }
Chris@47 94 static inline typename List<T>::Builder init(PointerBuilder builder, uint size) {
Chris@47 95 return typename List<T>::Builder(List<T>::initPointer(builder, size));
Chris@47 96 }
Chris@47 97 static inline void adopt(PointerBuilder builder, Orphan<List<T>>&& value) {
Chris@47 98 builder.adopt(kj::mv(value.builder));
Chris@47 99 }
Chris@47 100 static inline Orphan<List<T>> disown(PointerBuilder builder) {
Chris@47 101 return Orphan<List<T>>(builder.disown());
Chris@47 102 }
Chris@47 103 static inline _::ListReader getInternalReader(const typename List<T>::Reader& reader) {
Chris@47 104 return reader.reader;
Chris@47 105 }
Chris@47 106 static inline _::ListBuilder getInternalBuilder(typename List<T>::Builder&& builder) {
Chris@47 107 return builder.builder;
Chris@47 108 }
Chris@47 109 };
Chris@47 110
Chris@47 111 template <typename T>
Chris@47 112 struct PointerHelpers<T, Kind::BLOB> {
Chris@47 113 static inline typename T::Reader get(PointerReader reader,
Chris@47 114 const void* defaultValue = nullptr,
Chris@47 115 uint defaultBytes = 0) {
Chris@47 116 return reader.getBlob<T>(defaultValue, defaultBytes * BYTES);
Chris@47 117 }
Chris@47 118 static inline typename T::Builder get(PointerBuilder builder,
Chris@47 119 const void* defaultValue = nullptr,
Chris@47 120 uint defaultBytes = 0) {
Chris@47 121 return builder.getBlob<T>(defaultValue, defaultBytes * BYTES);
Chris@47 122 }
Chris@47 123 static inline void set(PointerBuilder builder, typename T::Reader value) {
Chris@47 124 builder.setBlob<T>(value);
Chris@47 125 }
Chris@47 126 static inline void setCanonical(PointerBuilder builder, typename T::Reader value) {
Chris@47 127 builder.setBlob<T>(value);
Chris@47 128 }
Chris@47 129 static inline typename T::Builder init(PointerBuilder builder, uint size) {
Chris@47 130 return builder.initBlob<T>(size * BYTES);
Chris@47 131 }
Chris@47 132 static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
Chris@47 133 builder.adopt(kj::mv(value.builder));
Chris@47 134 }
Chris@47 135 static inline Orphan<T> disown(PointerBuilder builder) {
Chris@47 136 return Orphan<T>(builder.disown());
Chris@47 137 }
Chris@47 138 };
Chris@47 139
Chris@47 140 struct UncheckedMessage {
Chris@47 141 typedef const word* Reader;
Chris@47 142 };
Chris@47 143
Chris@47 144 template <> struct Kind_<UncheckedMessage> { static constexpr Kind kind = Kind::OTHER; };
Chris@47 145
Chris@47 146 template <>
Chris@47 147 struct PointerHelpers<UncheckedMessage> {
Chris@47 148 // Reads an AnyPointer field as an unchecked message pointer. Requires that the containing
Chris@47 149 // message is itself unchecked. This hack is currently private. It is used to locate default
Chris@47 150 // values within encoded schemas.
Chris@47 151
Chris@47 152 static inline const word* get(PointerReader reader) {
Chris@47 153 return reader.getUnchecked();
Chris@47 154 }
Chris@47 155 };
Chris@47 156
Chris@47 157 } // namespace _ (private)
Chris@47 158 } // namespace capnp
Chris@47 159
Chris@47 160 #endif // CAPNP_POINTER_HELPERS_H_