annotate win32-mingw/include/capnp/list.h @ 79:91c729825bca pa_catalina

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