annotate win64-msvc/include/capnp/pointer-helpers.h @ 74:2f2b27544483

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