annotate win32-mingw/include/capnp/list.h @ 50:37d53a7e8262

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