annotate win32-mingw/include/capnp/pointer-helpers.h @ 50:37d53a7e8262

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