annotate win32-mingw/include/capnp/orphan.h @ 149:279b18cc7785

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