annotate win64-msvc/include/capnp/list.h @ 48:9530b331f8c1

Add Cap'n Proto source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 11:17:01 +0100
parents d93140aac40b
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef CAPNP_LIST_H_
Chris@47 23 #define CAPNP_LIST_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #include "layout.h"
Chris@47 30 #include "orphan.h"
Chris@47 31 #include <initializer_list>
Chris@47 32 #ifdef KJ_STD_COMPAT
Chris@47 33 #include <iterator>
Chris@47 34 #endif // KJ_STD_COMPAT
Chris@47 35
Chris@47 36 namespace capnp {
Chris@47 37 namespace _ { // private
Chris@47 38
Chris@47 39 template <typename T>
Chris@47 40 class TemporaryPointer {
Chris@47 41 // This class is a little hack which lets us define operator->() in cases where it needs to
Chris@47 42 // return a pointer to a temporary value. We instead construct a TemporaryPointer and return that
Chris@47 43 // (by value). The compiler then invokes operator->() on the TemporaryPointer, which itself is
Chris@47 44 // able to return a real pointer to its member.
Chris@47 45
Chris@47 46 public:
Chris@47 47 TemporaryPointer(T&& value): value(kj::mv(value)) {}
Chris@47 48 TemporaryPointer(const T& value): value(value) {}
Chris@47 49
Chris@47 50 inline T* operator->() { return &value; }
Chris@47 51 private:
Chris@47 52 T value;
Chris@47 53 };
Chris@47 54
Chris@47 55 template <typename Container, typename Element>
Chris@47 56 class IndexingIterator {
Chris@47 57 public:
Chris@47 58 IndexingIterator() = default;
Chris@47 59
Chris@47 60 inline Element operator*() const { return (*container)[index]; }
Chris@47 61 inline TemporaryPointer<Element> operator->() const {
Chris@47 62 return TemporaryPointer<Element>((*container)[index]);
Chris@47 63 }
Chris@47 64 inline Element operator[]( int off) const { return (*container)[index]; }
Chris@47 65 inline Element operator[](uint off) const { return (*container)[index]; }
Chris@47 66
Chris@47 67 inline IndexingIterator& operator++() { ++index; return *this; }
Chris@47 68 inline IndexingIterator operator++(int) { IndexingIterator other = *this; ++index; return other; }
Chris@47 69 inline IndexingIterator& operator--() { --index; return *this; }
Chris@47 70 inline IndexingIterator operator--(int) { IndexingIterator other = *this; --index; return other; }
Chris@47 71
Chris@47 72 inline IndexingIterator operator+(uint amount) const { return IndexingIterator(container, index + amount); }
Chris@47 73 inline IndexingIterator operator-(uint amount) const { return IndexingIterator(container, index - amount); }
Chris@47 74 inline IndexingIterator operator+( int amount) const { return IndexingIterator(container, index + amount); }
Chris@47 75 inline IndexingIterator operator-( int amount) const { return IndexingIterator(container, index - amount); }
Chris@47 76
Chris@47 77 inline int operator-(const IndexingIterator& other) const { return index - other.index; }
Chris@47 78
Chris@47 79 inline IndexingIterator& operator+=(uint amount) { index += amount; return *this; }
Chris@47 80 inline IndexingIterator& operator-=(uint amount) { index -= amount; return *this; }
Chris@47 81 inline IndexingIterator& operator+=( int amount) { index += amount; return *this; }
Chris@47 82 inline IndexingIterator& operator-=( int amount) { index -= amount; return *this; }
Chris@47 83
Chris@47 84 // STL says comparing iterators of different containers is not allowed, so we only compare
Chris@47 85 // indices here.
Chris@47 86 inline bool operator==(const IndexingIterator& other) const { return index == other.index; }
Chris@47 87 inline bool operator!=(const IndexingIterator& other) const { return index != other.index; }
Chris@47 88 inline bool operator<=(const IndexingIterator& other) const { return index <= other.index; }
Chris@47 89 inline bool operator>=(const IndexingIterator& other) const { return index >= other.index; }
Chris@47 90 inline bool operator< (const IndexingIterator& other) const { return index < other.index; }
Chris@47 91 inline bool operator> (const IndexingIterator& other) const { return index > other.index; }
Chris@47 92
Chris@47 93 private:
Chris@47 94 Container* container;
Chris@47 95 uint index;
Chris@47 96
Chris@47 97 friend Container;
Chris@47 98 inline IndexingIterator(Container* container, uint index)
Chris@47 99 : container(container), index(index) {}
Chris@47 100 };
Chris@47 101
Chris@47 102 } // namespace _ (private)
Chris@47 103
Chris@47 104 template <typename T>
Chris@47 105 struct List<T, Kind::PRIMITIVE> {
Chris@47 106 // List of primitives.
Chris@47 107
Chris@47 108 List() = delete;
Chris@47 109
Chris@47 110 class Reader {
Chris@47 111 public:
Chris@47 112 typedef List<T> Reads;
Chris@47 113
Chris@47 114 inline Reader(): reader(_::elementSizeForType<T>()) {}
Chris@47 115 inline explicit Reader(_::ListReader reader): reader(reader) {}
Chris@47 116
Chris@47 117 inline uint size() const { return reader.size() / ELEMENTS; }
Chris@47 118 inline T operator[](uint index) const {
Chris@47 119 KJ_IREQUIRE(index < size());
Chris@47 120 return reader.template getDataElement<T>(index * ELEMENTS);
Chris@47 121 }
Chris@47 122
Chris@47 123 typedef _::IndexingIterator<const Reader, T> Iterator;
Chris@47 124 inline Iterator begin() const { return Iterator(this, 0); }
Chris@47 125 inline Iterator end() const { return Iterator(this, size()); }
Chris@47 126
Chris@47 127 private:
Chris@47 128 _::ListReader reader;
Chris@47 129 template <typename U, Kind K>
Chris@47 130 friend struct _::PointerHelpers;
Chris@47 131 template <typename U, Kind K>
Chris@47 132 friend struct List;
Chris@47 133 friend class Orphanage;
Chris@47 134 template <typename U, Kind K>
Chris@47 135 friend struct ToDynamic_;
Chris@47 136 };
Chris@47 137
Chris@47 138 class Builder {
Chris@47 139 public:
Chris@47 140 typedef List<T> Builds;
Chris@47 141
Chris@47 142 inline Builder(): builder(_::elementSizeForType<T>()) {}
Chris@47 143 inline Builder(decltype(nullptr)) {}
Chris@47 144 inline explicit Builder(_::ListBuilder builder): builder(builder) {}
Chris@47 145
Chris@47 146 inline operator Reader() const { return Reader(builder.asReader()); }
Chris@47 147 inline Reader asReader() const { return Reader(builder.asReader()); }
Chris@47 148
Chris@47 149 inline uint size() const { return builder.size() / ELEMENTS; }
Chris@47 150 inline T operator[](uint index) {
Chris@47 151 KJ_IREQUIRE(index < size());
Chris@47 152 return builder.template getDataElement<T>(index * ELEMENTS);
Chris@47 153 }
Chris@47 154 inline void set(uint index, T value) {
Chris@47 155 // Alas, it is not possible to make operator[] return a reference to which you can assign,
Chris@47 156 // since the encoded representation does not necessarily match the compiler's representation
Chris@47 157 // of the type. We can't even return a clever class that implements operator T() and
Chris@47 158 // operator=() because it will lead to surprising behavior when using type inference (e.g.
Chris@47 159 // calling a template function with inferred argument types, or using "auto" or "decltype").
Chris@47 160
Chris@47 161 builder.template setDataElement<T>(index * ELEMENTS, value);
Chris@47 162 }
Chris@47 163
Chris@47 164 typedef _::IndexingIterator<Builder, T> Iterator;
Chris@47 165 inline Iterator begin() { return Iterator(this, 0); }
Chris@47 166 inline Iterator end() { return Iterator(this, size()); }
Chris@47 167
Chris@47 168 private:
Chris@47 169 _::ListBuilder builder;
Chris@47 170 template <typename U, Kind K>
Chris@47 171 friend struct _::PointerHelpers;
Chris@47 172 friend class Orphanage;
Chris@47 173 template <typename U, Kind K>
Chris@47 174 friend struct ToDynamic_;
Chris@47 175 };
Chris@47 176
Chris@47 177 class Pipeline {};
Chris@47 178
Chris@47 179 private:
Chris@47 180 inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
Chris@47 181 return builder.initList(_::elementSizeForType<T>(), size * ELEMENTS);
Chris@47 182 }
Chris@47 183 inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
Chris@47 184 return builder.getList(_::elementSizeForType<T>(), defaultValue);
Chris@47 185 }
Chris@47 186 inline static _::ListReader getFromPointer(
Chris@47 187 const _::PointerReader& reader, const word* defaultValue) {
Chris@47 188 return reader.getList(_::elementSizeForType<T>(), defaultValue);
Chris@47 189 }
Chris@47 190
Chris@47 191 template <typename U, Kind k>
Chris@47 192 friend struct List;
Chris@47 193 template <typename U, Kind K>
Chris@47 194 friend struct _::PointerHelpers;
Chris@47 195 };
Chris@47 196
Chris@47 197 template <typename T>
Chris@47 198 struct List<T, Kind::ENUM>: public List<T, Kind::PRIMITIVE> {};
Chris@47 199
Chris@47 200 template <typename T>
Chris@47 201 struct List<T, Kind::STRUCT> {
Chris@47 202 // List of structs.
Chris@47 203
Chris@47 204 List() = delete;
Chris@47 205
Chris@47 206 class Reader {
Chris@47 207 public:
Chris@47 208 typedef List<T> Reads;
Chris@47 209
Chris@47 210 inline Reader(): reader(ElementSize::INLINE_COMPOSITE) {}
Chris@47 211 inline explicit Reader(_::ListReader reader): reader(reader) {}
Chris@47 212
Chris@47 213 inline uint size() const { return reader.size() / ELEMENTS; }
Chris@47 214 inline typename T::Reader operator[](uint index) const {
Chris@47 215 KJ_IREQUIRE(index < size());
Chris@47 216 return typename T::Reader(reader.getStructElement(index * ELEMENTS));
Chris@47 217 }
Chris@47 218
Chris@47 219 typedef _::IndexingIterator<const Reader, typename T::Reader> Iterator;
Chris@47 220 inline Iterator begin() const { return Iterator(this, 0); }
Chris@47 221 inline Iterator end() const { return Iterator(this, size()); }
Chris@47 222
Chris@47 223 private:
Chris@47 224 _::ListReader reader;
Chris@47 225 template <typename U, Kind K>
Chris@47 226 friend struct _::PointerHelpers;
Chris@47 227 template <typename U, Kind K>
Chris@47 228 friend struct List;
Chris@47 229 friend class Orphanage;
Chris@47 230 template <typename U, Kind K>
Chris@47 231 friend struct ToDynamic_;
Chris@47 232 };
Chris@47 233
Chris@47 234 class Builder {
Chris@47 235 public:
Chris@47 236 typedef List<T> Builds;
Chris@47 237
Chris@47 238 inline Builder(): builder(ElementSize::INLINE_COMPOSITE) {}
Chris@47 239 inline Builder(decltype(nullptr)) {}
Chris@47 240 inline explicit Builder(_::ListBuilder builder): builder(builder) {}
Chris@47 241
Chris@47 242 inline operator Reader() const { return Reader(builder.asReader()); }
Chris@47 243 inline Reader asReader() const { return Reader(builder.asReader()); }
Chris@47 244
Chris@47 245 inline uint size() const { return builder.size() / ELEMENTS; }
Chris@47 246 inline typename T::Builder operator[](uint index) {
Chris@47 247 KJ_IREQUIRE(index < size());
Chris@47 248 return typename T::Builder(builder.getStructElement(index * ELEMENTS));
Chris@47 249 }
Chris@47 250
Chris@47 251 inline void adoptWithCaveats(uint index, Orphan<T>&& orphan) {
Chris@47 252 // Mostly behaves like you'd expect `adopt` to behave, but with two caveats originating from
Chris@47 253 // the fact that structs in a struct list are allocated inline rather than by pointer:
Chris@47 254 // * This actually performs a shallow copy, effectively adopting each of the orphan's
Chris@47 255 // children rather than adopting the orphan itself. The orphan ends up being discarded,
Chris@47 256 // possibly wasting space in the message object.
Chris@47 257 // * If the orphan is larger than the target struct -- say, because the orphan was built
Chris@47 258 // using a newer version of the schema that has additional fields -- it will be truncated,
Chris@47 259 // losing data.
Chris@47 260
Chris@47 261 KJ_IREQUIRE(index < size());
Chris@47 262
Chris@47 263 // We pass a zero-valued StructSize to asStruct() because we do not want the struct to be
Chris@47 264 // expanded under any circumstances. We're just going to throw it away anyway, and
Chris@47 265 // transferContentFrom() already carefully compares the struct sizes before transferring.
Chris@47 266 builder.getStructElement(index * ELEMENTS).transferContentFrom(
Chris@47 267 orphan.builder.asStruct(_::StructSize(0 * WORDS, 0 * POINTERS)));
Chris@47 268 }
Chris@47 269 inline void setWithCaveats(uint index, const typename T::Reader& reader) {
Chris@47 270 // Mostly behaves like you'd expect `set` to behave, but with a caveat originating from
Chris@47 271 // the fact that structs in a struct list are allocated inline rather than by pointer:
Chris@47 272 // If the source struct is larger than the target struct -- say, because the source was built
Chris@47 273 // using a newer version of the schema that has additional fields -- it will be truncated,
Chris@47 274 // losing data.
Chris@47 275 //
Chris@47 276 // Note: If you are trying to concatenate some lists, use Orphanage::newOrphanConcat() to
Chris@47 277 // do it without losing any data in case the source lists come from a newer version of the
Chris@47 278 // protocol. (Plus, it's easier to use anyhow.)
Chris@47 279
Chris@47 280 KJ_IREQUIRE(index < size());
Chris@47 281 builder.getStructElement(index * ELEMENTS).copyContentFrom(reader._reader);
Chris@47 282 }
Chris@47 283
Chris@47 284 // There are no init(), set(), adopt(), or disown() methods for lists of structs because the
Chris@47 285 // elements of the list are inlined and are initialized when the list is initialized. This
Chris@47 286 // means that init() would be redundant, and set() would risk data loss if the input struct
Chris@47 287 // were from a newer version of the protocol.
Chris@47 288
Chris@47 289 typedef _::IndexingIterator<Builder, typename T::Builder> Iterator;
Chris@47 290 inline Iterator begin() { return Iterator(this, 0); }
Chris@47 291 inline Iterator end() { return Iterator(this, size()); }
Chris@47 292
Chris@47 293 private:
Chris@47 294 _::ListBuilder builder;
Chris@47 295 template <typename U, Kind K>
Chris@47 296 friend struct _::PointerHelpers;
Chris@47 297 friend class Orphanage;
Chris@47 298 template <typename U, Kind K>
Chris@47 299 friend struct ToDynamic_;
Chris@47 300 };
Chris@47 301
Chris@47 302 class Pipeline {};
Chris@47 303
Chris@47 304 private:
Chris@47 305 inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
Chris@47 306 return builder.initStructList(size * ELEMENTS, _::structSize<T>());
Chris@47 307 }
Chris@47 308 inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
Chris@47 309 return builder.getStructList(_::structSize<T>(), defaultValue);
Chris@47 310 }
Chris@47 311 inline static _::ListReader getFromPointer(
Chris@47 312 const _::PointerReader& reader, const word* defaultValue) {
Chris@47 313 return reader.getList(ElementSize::INLINE_COMPOSITE, defaultValue);
Chris@47 314 }
Chris@47 315
Chris@47 316 template <typename U, Kind k>
Chris@47 317 friend struct List;
Chris@47 318 template <typename U, Kind K>
Chris@47 319 friend struct _::PointerHelpers;
Chris@47 320 };
Chris@47 321
Chris@47 322 template <typename T>
Chris@47 323 struct List<List<T>, Kind::LIST> {
Chris@47 324 // List of lists.
Chris@47 325
Chris@47 326 List() = delete;
Chris@47 327
Chris@47 328 class Reader {
Chris@47 329 public:
Chris@47 330 typedef List<List<T>> Reads;
Chris@47 331
Chris@47 332 inline Reader(): reader(ElementSize::POINTER) {}
Chris@47 333 inline explicit Reader(_::ListReader reader): reader(reader) {}
Chris@47 334
Chris@47 335 inline uint size() const { return reader.size() / ELEMENTS; }
Chris@47 336 inline typename List<T>::Reader operator[](uint index) const {
Chris@47 337 KJ_IREQUIRE(index < size());
Chris@47 338 return typename List<T>::Reader(
Chris@47 339 _::PointerHelpers<List<T>>::get(reader.getPointerElement(index * ELEMENTS)));
Chris@47 340 }
Chris@47 341
Chris@47 342 typedef _::IndexingIterator<const Reader, typename List<T>::Reader> Iterator;
Chris@47 343 inline Iterator begin() const { return Iterator(this, 0); }
Chris@47 344 inline Iterator end() const { return Iterator(this, size()); }
Chris@47 345
Chris@47 346 private:
Chris@47 347 _::ListReader reader;
Chris@47 348 template <typename U, Kind K>
Chris@47 349 friend struct _::PointerHelpers;
Chris@47 350 template <typename U, Kind K>
Chris@47 351 friend struct List;
Chris@47 352 friend class Orphanage;
Chris@47 353 template <typename U, Kind K>
Chris@47 354 friend struct ToDynamic_;
Chris@47 355 };
Chris@47 356
Chris@47 357 class Builder {
Chris@47 358 public:
Chris@47 359 typedef List<List<T>> Builds;
Chris@47 360
Chris@47 361 inline Builder(): builder(ElementSize::POINTER) {}
Chris@47 362 inline Builder(decltype(nullptr)) {}
Chris@47 363 inline explicit Builder(_::ListBuilder builder): builder(builder) {}
Chris@47 364
Chris@47 365 inline operator Reader() const { return Reader(builder.asReader()); }
Chris@47 366 inline Reader asReader() const { return Reader(builder.asReader()); }
Chris@47 367
Chris@47 368 inline uint size() const { return builder.size() / ELEMENTS; }
Chris@47 369 inline typename List<T>::Builder operator[](uint index) {
Chris@47 370 KJ_IREQUIRE(index < size());
Chris@47 371 return typename List<T>::Builder(
Chris@47 372 _::PointerHelpers<List<T>>::get(builder.getPointerElement(index * ELEMENTS)));
Chris@47 373 }
Chris@47 374 inline typename List<T>::Builder init(uint index, uint size) {
Chris@47 375 KJ_IREQUIRE(index < this->size());
Chris@47 376 return typename List<T>::Builder(
Chris@47 377 _::PointerHelpers<List<T>>::init(builder.getPointerElement(index * ELEMENTS), size));
Chris@47 378 }
Chris@47 379 inline void set(uint index, typename List<T>::Reader value) {
Chris@47 380 KJ_IREQUIRE(index < size());
Chris@47 381 builder.getPointerElement(index * ELEMENTS).setList(value.reader);
Chris@47 382 }
Chris@47 383 void set(uint index, std::initializer_list<ReaderFor<T>> value) {
Chris@47 384 KJ_IREQUIRE(index < size());
Chris@47 385 auto l = init(index, value.size());
Chris@47 386 uint i = 0;
Chris@47 387 for (auto& element: value) {
Chris@47 388 l.set(i++, element);
Chris@47 389 }
Chris@47 390 }
Chris@47 391 inline void adopt(uint index, Orphan<T>&& value) {
Chris@47 392 KJ_IREQUIRE(index < size());
Chris@47 393 builder.getPointerElement(index * ELEMENTS).adopt(kj::mv(value.builder));
Chris@47 394 }
Chris@47 395 inline Orphan<T> disown(uint index) {
Chris@47 396 KJ_IREQUIRE(index < size());
Chris@47 397 return Orphan<T>(builder.getPointerElement(index * ELEMENTS).disown());
Chris@47 398 }
Chris@47 399
Chris@47 400 typedef _::IndexingIterator<Builder, typename List<T>::Builder> Iterator;
Chris@47 401 inline Iterator begin() { return Iterator(this, 0); }
Chris@47 402 inline Iterator end() { return Iterator(this, size()); }
Chris@47 403
Chris@47 404 private:
Chris@47 405 _::ListBuilder builder;
Chris@47 406 template <typename U, Kind K>
Chris@47 407 friend struct _::PointerHelpers;
Chris@47 408 friend class Orphanage;
Chris@47 409 template <typename U, Kind K>
Chris@47 410 friend struct ToDynamic_;
Chris@47 411 };
Chris@47 412
Chris@47 413 class Pipeline {};
Chris@47 414
Chris@47 415 private:
Chris@47 416 inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
Chris@47 417 return builder.initList(ElementSize::POINTER, size * ELEMENTS);
Chris@47 418 }
Chris@47 419 inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
Chris@47 420 return builder.getList(ElementSize::POINTER, defaultValue);
Chris@47 421 }
Chris@47 422 inline static _::ListReader getFromPointer(
Chris@47 423 const _::PointerReader& reader, const word* defaultValue) {
Chris@47 424 return reader.getList(ElementSize::POINTER, defaultValue);
Chris@47 425 }
Chris@47 426
Chris@47 427 template <typename U, Kind k>
Chris@47 428 friend struct List;
Chris@47 429 template <typename U, Kind K>
Chris@47 430 friend struct _::PointerHelpers;
Chris@47 431 };
Chris@47 432
Chris@47 433 template <typename T>
Chris@47 434 struct List<T, Kind::BLOB> {
Chris@47 435 List() = delete;
Chris@47 436
Chris@47 437 class Reader {
Chris@47 438 public:
Chris@47 439 typedef List<T> Reads;
Chris@47 440
Chris@47 441 inline Reader(): reader(ElementSize::POINTER) {}
Chris@47 442 inline explicit Reader(_::ListReader reader): reader(reader) {}
Chris@47 443
Chris@47 444 inline uint size() const { return reader.size() / ELEMENTS; }
Chris@47 445 inline typename T::Reader operator[](uint index) const {
Chris@47 446 KJ_IREQUIRE(index < size());
Chris@47 447 return reader.getPointerElement(index * ELEMENTS).template getBlob<T>(nullptr, 0 * BYTES);
Chris@47 448 }
Chris@47 449
Chris@47 450 typedef _::IndexingIterator<const Reader, typename T::Reader> Iterator;
Chris@47 451 inline Iterator begin() const { return Iterator(this, 0); }
Chris@47 452 inline Iterator end() const { return Iterator(this, size()); }
Chris@47 453
Chris@47 454 private:
Chris@47 455 _::ListReader reader;
Chris@47 456 template <typename U, Kind K>
Chris@47 457 friend struct _::PointerHelpers;
Chris@47 458 template <typename U, Kind K>
Chris@47 459 friend struct List;
Chris@47 460 friend class Orphanage;
Chris@47 461 template <typename U, Kind K>
Chris@47 462 friend struct ToDynamic_;
Chris@47 463 };
Chris@47 464
Chris@47 465 class Builder {
Chris@47 466 public:
Chris@47 467 typedef List<T> Builds;
Chris@47 468
Chris@47 469 inline Builder(): builder(ElementSize::POINTER) {}
Chris@47 470 inline Builder(decltype(nullptr)) {}
Chris@47 471 inline explicit Builder(_::ListBuilder builder): builder(builder) {}
Chris@47 472
Chris@47 473 inline operator Reader() const { return Reader(builder.asReader()); }
Chris@47 474 inline Reader asReader() const { return Reader(builder.asReader()); }
Chris@47 475
Chris@47 476 inline uint size() const { return builder.size() / ELEMENTS; }
Chris@47 477 inline typename T::Builder operator[](uint index) {
Chris@47 478 KJ_IREQUIRE(index < size());
Chris@47 479 return builder.getPointerElement(index * ELEMENTS).template getBlob<T>(nullptr, 0 * BYTES);
Chris@47 480 }
Chris@47 481 inline void set(uint index, typename T::Reader value) {
Chris@47 482 KJ_IREQUIRE(index < size());
Chris@47 483 builder.getPointerElement(index * ELEMENTS).template setBlob<T>(value);
Chris@47 484 }
Chris@47 485 inline typename T::Builder init(uint index, uint size) {
Chris@47 486 KJ_IREQUIRE(index < this->size());
Chris@47 487 return builder.getPointerElement(index * ELEMENTS).template initBlob<T>(size * BYTES);
Chris@47 488 }
Chris@47 489 inline void adopt(uint index, Orphan<T>&& value) {
Chris@47 490 KJ_IREQUIRE(index < size());
Chris@47 491 builder.getPointerElement(index * ELEMENTS).adopt(kj::mv(value.builder));
Chris@47 492 }
Chris@47 493 inline Orphan<T> disown(uint index) {
Chris@47 494 KJ_IREQUIRE(index < size());
Chris@47 495 return Orphan<T>(builder.getPointerElement(index * ELEMENTS).disown());
Chris@47 496 }
Chris@47 497
Chris@47 498 typedef _::IndexingIterator<Builder, typename T::Builder> Iterator;
Chris@47 499 inline Iterator begin() { return Iterator(this, 0); }
Chris@47 500 inline Iterator end() { return Iterator(this, size()); }
Chris@47 501
Chris@47 502 private:
Chris@47 503 _::ListBuilder builder;
Chris@47 504 template <typename U, Kind K>
Chris@47 505 friend struct _::PointerHelpers;
Chris@47 506 friend class Orphanage;
Chris@47 507 template <typename U, Kind K>
Chris@47 508 friend struct ToDynamic_;
Chris@47 509 };
Chris@47 510
Chris@47 511 class Pipeline {};
Chris@47 512
Chris@47 513 private:
Chris@47 514 inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
Chris@47 515 return builder.initList(ElementSize::POINTER, size * ELEMENTS);
Chris@47 516 }
Chris@47 517 inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
Chris@47 518 return builder.getList(ElementSize::POINTER, defaultValue);
Chris@47 519 }
Chris@47 520 inline static _::ListReader getFromPointer(
Chris@47 521 const _::PointerReader& reader, const word* defaultValue) {
Chris@47 522 return reader.getList(ElementSize::POINTER, defaultValue);
Chris@47 523 }
Chris@47 524
Chris@47 525 template <typename U, Kind k>
Chris@47 526 friend struct List;
Chris@47 527 template <typename U, Kind K>
Chris@47 528 friend struct _::PointerHelpers;
Chris@47 529 };
Chris@47 530
Chris@47 531 } // namespace capnp
Chris@47 532
Chris@47 533 #ifdef KJ_STD_COMPAT
Chris@47 534 namespace std {
Chris@47 535
Chris@47 536 template <typename Container, typename Element>
Chris@47 537 struct iterator_traits<capnp::_::IndexingIterator<Container, Element>>
Chris@47 538 : public std::iterator<std::random_access_iterator_tag, Element, int> {};
Chris@47 539
Chris@47 540 } // namespace std
Chris@47 541 #endif // KJ_STD_COMPAT
Chris@47 542
Chris@47 543 #endif // CAPNP_LIST_H_