annotate win64-msvc/include/capnp/orphan.h @ 166:cbd6d7e562c7

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