annotate win32-mingw/include/capnp/orphan.h @ 135:38d1c0e7850b

Headers for KJ/Capnp Win32
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 26 Oct 2016 13:18:45 +0100
parents
children eccd51b72864
rev   line source
cannam@135 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@135 2 // Licensed under the MIT License:
cannam@135 3 //
cannam@135 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@135 5 // of this software and associated documentation files (the "Software"), to deal
cannam@135 6 // in the Software without restriction, including without limitation the rights
cannam@135 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@135 8 // copies of the Software, and to permit persons to whom the Software is
cannam@135 9 // furnished to do so, subject to the following conditions:
cannam@135 10 //
cannam@135 11 // The above copyright notice and this permission notice shall be included in
cannam@135 12 // all copies or substantial portions of the Software.
cannam@135 13 //
cannam@135 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@135 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@135 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@135 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@135 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@135 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@135 20 // THE SOFTWARE.
cannam@135 21
cannam@135 22 #ifndef CAPNP_ORPHAN_H_
cannam@135 23 #define CAPNP_ORPHAN_H_
cannam@135 24
cannam@135 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@135 26 #pragma GCC system_header
cannam@135 27 #endif
cannam@135 28
cannam@135 29 #include "layout.h"
cannam@135 30
cannam@135 31 namespace capnp {
cannam@135 32
cannam@135 33 class StructSchema;
cannam@135 34 class ListSchema;
cannam@135 35 struct DynamicStruct;
cannam@135 36 struct DynamicList;
cannam@135 37 namespace _ { struct OrphanageInternal; }
cannam@135 38
cannam@135 39 template <typename T>
cannam@135 40 class Orphan {
cannam@135 41 // Represents an object which is allocated within some message builder but has no pointers
cannam@135 42 // pointing at it. An Orphan can later be "adopted" by some other object as one of that object's
cannam@135 43 // fields, without having to copy the orphan. For a field `foo` of pointer type, the generated
cannam@135 44 // code will define builder methods `void adoptFoo(Orphan<T>)` and `Orphan<T> disownFoo()`.
cannam@135 45 // Orphans can also be created independently of any parent using an Orphanage.
cannam@135 46 //
cannam@135 47 // `Orphan<T>` can be moved but not copied, like `Own<T>`, so that it is impossible for one
cannam@135 48 // orphan to be adopted multiple times. If an orphan is destroyed without being adopted, its
cannam@135 49 // contents are zero'd out (and possibly reused, if we ever implement the ability to reuse space
cannam@135 50 // in a message arena).
cannam@135 51
cannam@135 52 public:
cannam@135 53 Orphan() = default;
cannam@135 54 KJ_DISALLOW_COPY(Orphan);
cannam@135 55 Orphan(Orphan&&) = default;
cannam@135 56 Orphan& operator=(Orphan&&) = default;
cannam@135 57 inline Orphan(_::OrphanBuilder&& builder): builder(kj::mv(builder)) {}
cannam@135 58
cannam@135 59 inline BuilderFor<T> get();
cannam@135 60 // Get the underlying builder. If the orphan is null, this will allocate and return a default
cannam@135 61 // object rather than crash. This is done for security -- otherwise, you might enable a DoS
cannam@135 62 // attack any time you disown a field and fail to check if it is null. In the case of structs,
cannam@135 63 // this means that the orphan is no longer null after get() returns. In the case of lists,
cannam@135 64 // no actual object is allocated since a simple empty ListBuilder can be returned.
cannam@135 65
cannam@135 66 inline ReaderFor<T> getReader() const;
cannam@135 67
cannam@135 68 inline bool operator==(decltype(nullptr)) const { return builder == nullptr; }
cannam@135 69 inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; }
cannam@135 70
cannam@135 71 inline void truncate(uint size);
cannam@135 72 // Resize an object (which must be a list or a blob) to the given size.
cannam@135 73 //
cannam@135 74 // If the new size is less than the original, the remaining elements will be discarded. The
cannam@135 75 // list is never moved in this case. If the list happens to be located at the end of its segment
cannam@135 76 // (which is always true if the list was the last thing allocated), the removed memory will be
cannam@135 77 // reclaimed (reducing the messag size), otherwise it is simply zeroed. The reclaiming behavior
cannam@135 78 // is particularly useful for allocating buffer space when you aren't sure how much space you
cannam@135 79 // actually need: you can pre-allocate, say, a 4k byte array, read() from a file into it, and
cannam@135 80 // then truncate it back to the amount of space actually used.
cannam@135 81 //
cannam@135 82 // If the new size is greater than the original, the list is extended with default values. If
cannam@135 83 // the list is the last object in its segment *and* there is enough space left in the segment to
cannam@135 84 // extend it to cover the new values, then the list is extended in-place. Otherwise, it must be
cannam@135 85 // moved to a new location, leaving a zero'd hole in the previous space that won't be filled.
cannam@135 86 // This copy is shallow; sub-objects will simply be reparented, not copied.
cannam@135 87 //
cannam@135 88 // Any existing readers or builders pointing at the object are invalidated by this call (even if
cannam@135 89 // it doesn't move). You must call `get()` or `getReader()` again to get the new, valid pointer.
cannam@135 90
cannam@135 91 private:
cannam@135 92 _::OrphanBuilder builder;
cannam@135 93
cannam@135 94 template <typename, Kind>
cannam@135 95 friend struct _::PointerHelpers;
cannam@135 96 template <typename, Kind>
cannam@135 97 friend struct List;
cannam@135 98 template <typename U>
cannam@135 99 friend class Orphan;
cannam@135 100 friend class Orphanage;
cannam@135 101 friend class MessageBuilder;
cannam@135 102 };
cannam@135 103
cannam@135 104 class Orphanage: private kj::DisallowConstCopy {
cannam@135 105 // Use to directly allocate Orphan objects, without having a parent object allocate and then
cannam@135 106 // disown the object.
cannam@135 107
cannam@135 108 public:
cannam@135 109 inline Orphanage(): arena(nullptr) {}
cannam@135 110
cannam@135 111 template <typename BuilderType>
cannam@135 112 static Orphanage getForMessageContaining(BuilderType builder);
cannam@135 113 // Construct an Orphanage that allocates within the message containing the given Builder. This
cannam@135 114 // allows the constructed Orphans to be adopted by objects within said message.
cannam@135 115 //
cannam@135 116 // This constructor takes the builder rather than having the builder have a getOrphanage() method
cannam@135 117 // because this is an advanced feature and we don't want to pollute the builder APIs with it.
cannam@135 118 //
cannam@135 119 // Note that if you have a direct pointer to the `MessageBuilder`, you can simply call its
cannam@135 120 // `getOrphanage()` method.
cannam@135 121
cannam@135 122 template <typename RootType>
cannam@135 123 Orphan<RootType> newOrphan() const;
cannam@135 124 // Allocate a new orphaned struct.
cannam@135 125
cannam@135 126 template <typename RootType>
cannam@135 127 Orphan<RootType> newOrphan(uint size) const;
cannam@135 128 // Allocate a new orphaned list or blob.
cannam@135 129
cannam@135 130 Orphan<DynamicStruct> newOrphan(StructSchema schema) const;
cannam@135 131 // Dynamically create an orphan struct with the given schema. You must
cannam@135 132 // #include <capnp/dynamic.h> to use this.
cannam@135 133
cannam@135 134 Orphan<DynamicList> newOrphan(ListSchema schema, uint size) const;
cannam@135 135 // Dynamically create an orphan list with the given schema. You must #include <capnp/dynamic.h>
cannam@135 136 // to use this.
cannam@135 137
cannam@135 138 template <typename Reader>
cannam@135 139 Orphan<FromReader<Reader>> newOrphanCopy(Reader copyFrom) const;
cannam@135 140 // Allocate a new orphaned object (struct, list, or blob) and initialize it as a copy of the
cannam@135 141 // given object.
cannam@135 142
cannam@135 143 template <typename T>
cannam@135 144 Orphan<List<ListElementType<FromReader<T>>>> newOrphanConcat(kj::ArrayPtr<T> lists) const;
cannam@135 145 template <typename T>
cannam@135 146 Orphan<List<ListElementType<FromReader<T>>>> newOrphanConcat(kj::ArrayPtr<const T> lists) const;
cannam@135 147 // Given an array of List readers, copy and concatenate the lists, creating a new Orphan.
cannam@135 148 //
cannam@135 149 // Note that compared to allocating the list yourself and using `setWithCaveats()` to set each
cannam@135 150 // item, this method avoids the "caveats": the new list will be allocated with the element size
cannam@135 151 // being the maximum of that from all the input lists. This is particularly important when
cannam@135 152 // concatenating struct lists: if the lists were created using a newer version of the protocol
cannam@135 153 // in which some new fields had been added to the struct, using `setWithCaveats()` would
cannam@135 154 // truncate off those new fields.
cannam@135 155
cannam@135 156 Orphan<Data> referenceExternalData(Data::Reader data) const;
cannam@135 157 // Creates an Orphan<Data> that points at an existing region of memory (e.g. from another message)
cannam@135 158 // without copying it. There are some SEVERE restrictions on how this can be used:
cannam@135 159 // - The memory must remain valid until the `MessageBuilder` is destroyed (even if the orphan is
cannam@135 160 // abandoned).
cannam@135 161 // - Because the data is const, you will not be allowed to obtain a `Data::Builder`
cannam@135 162 // for this blob. Any call which would return such a builder will throw an exception. You
cannam@135 163 // can, however, obtain a Reader, e.g. via orphan.getReader() or from a parent Reader (once
cannam@135 164 // the orphan is adopted). It is your responsibility to make sure your code can deal with
cannam@135 165 // these problems when using this optimization; if you can't, allocate a copy instead.
cannam@135 166 // - `data.begin()` must be aligned to a machine word boundary (32-bit or 64-bit depending on
cannam@135 167 // the CPU). Any pointer returned by malloc() as well as any data blob obtained from another
cannam@135 168 // Cap'n Proto message satisfies this.
cannam@135 169 // - If `data.size()` is not a multiple of 8, extra bytes past data.end() up until the next 8-byte
cannam@135 170 // boundary will be visible in the raw message when it is written out. Thus, there must be no
cannam@135 171 // secrets in these bytes. Data blobs obtained from other Cap'n Proto messages should be safe
cannam@135 172 // as these bytes should be zero (unless the sender had the same problem).
cannam@135 173 //
cannam@135 174 // The array will actually become one of the message's segments. The data can thus be adopted
cannam@135 175 // into the message tree without copying it. This is particularly useful when referencing very
cannam@135 176 // large blobs, such as whole mmap'd files.
cannam@135 177
cannam@135 178 private:
cannam@135 179 _::BuilderArena* arena;
cannam@135 180 _::CapTableBuilder* capTable;
cannam@135 181
cannam@135 182 inline explicit Orphanage(_::BuilderArena* arena, _::CapTableBuilder* capTable)
cannam@135 183 : arena(arena), capTable(capTable) {}
cannam@135 184
cannam@135 185 template <typename T, Kind = CAPNP_KIND(T)>
cannam@135 186 struct GetInnerBuilder;
cannam@135 187 template <typename T, Kind = CAPNP_KIND(T)>
cannam@135 188 struct GetInnerReader;
cannam@135 189 template <typename T>
cannam@135 190 struct NewOrphanListImpl;
cannam@135 191
cannam@135 192 friend class MessageBuilder;
cannam@135 193 friend struct _::OrphanageInternal;
cannam@135 194 };
cannam@135 195
cannam@135 196 // =======================================================================================
cannam@135 197 // Inline implementation details.
cannam@135 198
cannam@135 199 namespace _ { // private
cannam@135 200
cannam@135 201 template <typename T, Kind = CAPNP_KIND(T)>
cannam@135 202 struct OrphanGetImpl;
cannam@135 203
cannam@135 204 template <typename T>
cannam@135 205 struct OrphanGetImpl<T, Kind::PRIMITIVE> {
cannam@135 206 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 207 builder.truncate(size, _::elementSizeForType<T>());
cannam@135 208 }
cannam@135 209 };
cannam@135 210
cannam@135 211 template <typename T>
cannam@135 212 struct OrphanGetImpl<T, Kind::STRUCT> {
cannam@135 213 static inline typename T::Builder apply(_::OrphanBuilder& builder) {
cannam@135 214 return typename T::Builder(builder.asStruct(_::structSize<T>()));
cannam@135 215 }
cannam@135 216 static inline typename T::Reader applyReader(const _::OrphanBuilder& builder) {
cannam@135 217 return typename T::Reader(builder.asStructReader(_::structSize<T>()));
cannam@135 218 }
cannam@135 219 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 220 builder.truncate(size, _::structSize<T>());
cannam@135 221 }
cannam@135 222 };
cannam@135 223
cannam@135 224 #if !CAPNP_LITE
cannam@135 225 template <typename T>
cannam@135 226 struct OrphanGetImpl<T, Kind::INTERFACE> {
cannam@135 227 static inline typename T::Client apply(_::OrphanBuilder& builder) {
cannam@135 228 return typename T::Client(builder.asCapability());
cannam@135 229 }
cannam@135 230 static inline typename T::Client applyReader(const _::OrphanBuilder& builder) {
cannam@135 231 return typename T::Client(builder.asCapability());
cannam@135 232 }
cannam@135 233 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 234 builder.truncate(size, ElementSize::POINTER);
cannam@135 235 }
cannam@135 236 };
cannam@135 237 #endif // !CAPNP_LITE
cannam@135 238
cannam@135 239 template <typename T, Kind k>
cannam@135 240 struct OrphanGetImpl<List<T, k>, Kind::LIST> {
cannam@135 241 static inline typename List<T>::Builder apply(_::OrphanBuilder& builder) {
cannam@135 242 return typename List<T>::Builder(builder.asList(_::ElementSizeForType<T>::value));
cannam@135 243 }
cannam@135 244 static inline typename List<T>::Reader applyReader(const _::OrphanBuilder& builder) {
cannam@135 245 return typename List<T>::Reader(builder.asListReader(_::ElementSizeForType<T>::value));
cannam@135 246 }
cannam@135 247 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 248 builder.truncate(size, ElementSize::POINTER);
cannam@135 249 }
cannam@135 250 };
cannam@135 251
cannam@135 252 template <typename T>
cannam@135 253 struct OrphanGetImpl<List<T, Kind::STRUCT>, Kind::LIST> {
cannam@135 254 static inline typename List<T>::Builder apply(_::OrphanBuilder& builder) {
cannam@135 255 return typename List<T>::Builder(builder.asStructList(_::structSize<T>()));
cannam@135 256 }
cannam@135 257 static inline typename List<T>::Reader applyReader(const _::OrphanBuilder& builder) {
cannam@135 258 return typename List<T>::Reader(builder.asListReader(_::ElementSizeForType<T>::value));
cannam@135 259 }
cannam@135 260 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 261 builder.truncate(size, ElementSize::POINTER);
cannam@135 262 }
cannam@135 263 };
cannam@135 264
cannam@135 265 template <>
cannam@135 266 struct OrphanGetImpl<Text, Kind::BLOB> {
cannam@135 267 static inline Text::Builder apply(_::OrphanBuilder& builder) {
cannam@135 268 return Text::Builder(builder.asText());
cannam@135 269 }
cannam@135 270 static inline Text::Reader applyReader(const _::OrphanBuilder& builder) {
cannam@135 271 return Text::Reader(builder.asTextReader());
cannam@135 272 }
cannam@135 273 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 274 builder.truncate(size, ElementSize::POINTER);
cannam@135 275 }
cannam@135 276 };
cannam@135 277
cannam@135 278 template <>
cannam@135 279 struct OrphanGetImpl<Data, Kind::BLOB> {
cannam@135 280 static inline Data::Builder apply(_::OrphanBuilder& builder) {
cannam@135 281 return Data::Builder(builder.asData());
cannam@135 282 }
cannam@135 283 static inline Data::Reader applyReader(const _::OrphanBuilder& builder) {
cannam@135 284 return Data::Reader(builder.asDataReader());
cannam@135 285 }
cannam@135 286 static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) {
cannam@135 287 builder.truncate(size, ElementSize::POINTER);
cannam@135 288 }
cannam@135 289 };
cannam@135 290
cannam@135 291 struct OrphanageInternal {
cannam@135 292 static inline _::BuilderArena* getArena(Orphanage orphanage) { return orphanage.arena; }
cannam@135 293 static inline _::CapTableBuilder* getCapTable(Orphanage orphanage) { return orphanage.capTable; }
cannam@135 294 };
cannam@135 295
cannam@135 296 } // namespace _ (private)
cannam@135 297
cannam@135 298 template <typename T>
cannam@135 299 inline BuilderFor<T> Orphan<T>::get() {
cannam@135 300 return _::OrphanGetImpl<T>::apply(builder);
cannam@135 301 }
cannam@135 302
cannam@135 303 template <typename T>
cannam@135 304 inline ReaderFor<T> Orphan<T>::getReader() const {
cannam@135 305 return _::OrphanGetImpl<T>::applyReader(builder);
cannam@135 306 }
cannam@135 307
cannam@135 308 template <typename T>
cannam@135 309 inline void Orphan<T>::truncate(uint size) {
cannam@135 310 _::OrphanGetImpl<ListElementType<T>>::truncateListOf(builder, size * ELEMENTS);
cannam@135 311 }
cannam@135 312
cannam@135 313 template <>
cannam@135 314 inline void Orphan<Text>::truncate(uint size) {
cannam@135 315 builder.truncateText(size * ELEMENTS);
cannam@135 316 }
cannam@135 317
cannam@135 318 template <>
cannam@135 319 inline void Orphan<Data>::truncate(uint size) {
cannam@135 320 builder.truncate(size * ELEMENTS, ElementSize::BYTE);
cannam@135 321 }
cannam@135 322
cannam@135 323 template <typename T>
cannam@135 324 struct Orphanage::GetInnerBuilder<T, Kind::STRUCT> {
cannam@135 325 static inline _::StructBuilder apply(typename T::Builder& t) {
cannam@135 326 return t._builder;
cannam@135 327 }
cannam@135 328 };
cannam@135 329
cannam@135 330 template <typename T>
cannam@135 331 struct Orphanage::GetInnerBuilder<T, Kind::LIST> {
cannam@135 332 static inline _::ListBuilder apply(typename T::Builder& t) {
cannam@135 333 return t.builder;
cannam@135 334 }
cannam@135 335 };
cannam@135 336
cannam@135 337 template <typename BuilderType>
cannam@135 338 Orphanage Orphanage::getForMessageContaining(BuilderType builder) {
cannam@135 339 auto inner = GetInnerBuilder<FromBuilder<BuilderType>>::apply(builder);
cannam@135 340 return Orphanage(inner.getArena(), inner.getCapTable());
cannam@135 341 }
cannam@135 342
cannam@135 343 template <typename RootType>
cannam@135 344 Orphan<RootType> Orphanage::newOrphan() const {
cannam@135 345 return Orphan<RootType>(_::OrphanBuilder::initStruct(arena, capTable, _::structSize<RootType>()));
cannam@135 346 }
cannam@135 347
cannam@135 348 template <typename T, Kind k>
cannam@135 349 struct Orphanage::NewOrphanListImpl<List<T, k>> {
cannam@135 350 static inline _::OrphanBuilder apply(
cannam@135 351 _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) {
cannam@135 352 return _::OrphanBuilder::initList(
cannam@135 353 arena, capTable, size * ELEMENTS, _::ElementSizeForType<T>::value);
cannam@135 354 }
cannam@135 355 };
cannam@135 356
cannam@135 357 template <typename T>
cannam@135 358 struct Orphanage::NewOrphanListImpl<List<T, Kind::STRUCT>> {
cannam@135 359 static inline _::OrphanBuilder apply(
cannam@135 360 _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) {
cannam@135 361 return _::OrphanBuilder::initStructList(
cannam@135 362 arena, capTable, size * ELEMENTS, _::structSize<T>());
cannam@135 363 }
cannam@135 364 };
cannam@135 365
cannam@135 366 template <>
cannam@135 367 struct Orphanage::NewOrphanListImpl<Text> {
cannam@135 368 static inline _::OrphanBuilder apply(
cannam@135 369 _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) {
cannam@135 370 return _::OrphanBuilder::initText(arena, capTable, size * BYTES);
cannam@135 371 }
cannam@135 372 };
cannam@135 373
cannam@135 374 template <>
cannam@135 375 struct Orphanage::NewOrphanListImpl<Data> {
cannam@135 376 static inline _::OrphanBuilder apply(
cannam@135 377 _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) {
cannam@135 378 return _::OrphanBuilder::initData(arena, capTable, size * BYTES);
cannam@135 379 }
cannam@135 380 };
cannam@135 381
cannam@135 382 template <typename RootType>
cannam@135 383 Orphan<RootType> Orphanage::newOrphan(uint size) const {
cannam@135 384 return Orphan<RootType>(NewOrphanListImpl<RootType>::apply(arena, capTable, size));
cannam@135 385 }
cannam@135 386
cannam@135 387 template <typename T>
cannam@135 388 struct Orphanage::GetInnerReader<T, Kind::STRUCT> {
cannam@135 389 static inline _::StructReader apply(const typename T::Reader& t) {
cannam@135 390 return t._reader;
cannam@135 391 }
cannam@135 392 };
cannam@135 393
cannam@135 394 template <typename T>
cannam@135 395 struct Orphanage::GetInnerReader<T, Kind::LIST> {
cannam@135 396 static inline _::ListReader apply(const typename T::Reader& t) {
cannam@135 397 return t.reader;
cannam@135 398 }
cannam@135 399 };
cannam@135 400
cannam@135 401 template <typename T>
cannam@135 402 struct Orphanage::GetInnerReader<T, Kind::BLOB> {
cannam@135 403 static inline const typename T::Reader& apply(const typename T::Reader& t) {
cannam@135 404 return t;
cannam@135 405 }
cannam@135 406 };
cannam@135 407
cannam@135 408 template <typename Reader>
cannam@135 409 inline Orphan<FromReader<Reader>> Orphanage::newOrphanCopy(Reader copyFrom) const {
cannam@135 410 return Orphan<FromReader<Reader>>(_::OrphanBuilder::copy(
cannam@135 411 arena, capTable, GetInnerReader<FromReader<Reader>>::apply(copyFrom)));
cannam@135 412 }
cannam@135 413
cannam@135 414 template <typename T>
cannam@135 415 inline Orphan<List<ListElementType<FromReader<T>>>>
cannam@135 416 Orphanage::newOrphanConcat(kj::ArrayPtr<T> lists) const {
cannam@135 417 return newOrphanConcat(kj::implicitCast<kj::ArrayPtr<const T>>(lists));
cannam@135 418 }
cannam@135 419 template <typename T>
cannam@135 420 inline Orphan<List<ListElementType<FromReader<T>>>>
cannam@135 421 Orphanage::newOrphanConcat(kj::ArrayPtr<const T> lists) const {
cannam@135 422 // Optimization / simplification: Rely on List<T>::Reader containing nothing except a
cannam@135 423 // _::ListReader.
cannam@135 424 static_assert(sizeof(T) == sizeof(_::ListReader), "lists are not bare readers?");
cannam@135 425 kj::ArrayPtr<const _::ListReader> raw(
cannam@135 426 reinterpret_cast<const _::ListReader*>(lists.begin()), lists.size());
cannam@135 427 typedef ListElementType<FromReader<T>> Element;
cannam@135 428 return Orphan<List<Element>>(
cannam@135 429 _::OrphanBuilder::concat(arena, capTable,
cannam@135 430 _::elementSizeForType<Element>(),
cannam@135 431 _::minStructSizeForElement<Element>(), raw));
cannam@135 432 }
cannam@135 433
cannam@135 434 inline Orphan<Data> Orphanage::referenceExternalData(Data::Reader data) const {
cannam@135 435 return Orphan<Data>(_::OrphanBuilder::referenceExternalData(arena, data));
cannam@135 436 }
cannam@135 437
cannam@135 438 } // namespace capnp
cannam@135 439
cannam@135 440 #endif // CAPNP_ORPHAN_H_