annotate win64-msvc/include/capnp/list.h @ 74:2f2b27544483

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