comparison osx/include/capnp/list.h @ 49:3ab5a40c4e3b

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