Chris@50: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@50: // Licensed under the MIT License: Chris@50: // Chris@50: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@50: // of this software and associated documentation files (the "Software"), to deal Chris@50: // in the Software without restriction, including without limitation the rights Chris@50: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@50: // copies of the Software, and to permit persons to whom the Software is Chris@50: // furnished to do so, subject to the following conditions: Chris@50: // Chris@50: // The above copyright notice and this permission notice shall be included in Chris@50: // all copies or substantial portions of the Software. Chris@50: // Chris@50: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@50: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@50: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@50: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@50: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@50: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@50: // THE SOFTWARE. Chris@50: Chris@50: #ifndef CAPNP_LIST_H_ Chris@50: #define CAPNP_LIST_H_ Chris@50: Chris@50: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@50: #pragma GCC system_header Chris@50: #endif Chris@50: Chris@50: #include "layout.h" Chris@50: #include "orphan.h" Chris@50: #include Chris@50: #ifdef KJ_STD_COMPAT Chris@50: #include Chris@50: #endif // KJ_STD_COMPAT Chris@50: Chris@50: namespace capnp { Chris@50: namespace _ { // private Chris@50: Chris@50: template Chris@50: class TemporaryPointer { Chris@50: // This class is a little hack which lets us define operator->() in cases where it needs to Chris@50: // return a pointer to a temporary value. We instead construct a TemporaryPointer and return that Chris@50: // (by value). The compiler then invokes operator->() on the TemporaryPointer, which itself is Chris@50: // able to return a real pointer to its member. Chris@50: Chris@50: public: Chris@50: TemporaryPointer(T&& value): value(kj::mv(value)) {} Chris@50: TemporaryPointer(const T& value): value(value) {} Chris@50: Chris@50: inline T* operator->() { return &value; } Chris@50: private: Chris@50: T value; Chris@50: }; Chris@50: Chris@50: template Chris@50: class IndexingIterator { Chris@50: public: Chris@50: IndexingIterator() = default; Chris@50: Chris@50: inline Element operator*() const { return (*container)[index]; } Chris@50: inline TemporaryPointer operator->() const { Chris@50: return TemporaryPointer((*container)[index]); Chris@50: } Chris@50: inline Element operator[]( int off) const { return (*container)[index]; } Chris@50: inline Element operator[](uint off) const { return (*container)[index]; } Chris@50: Chris@50: inline IndexingIterator& operator++() { ++index; return *this; } Chris@50: inline IndexingIterator operator++(int) { IndexingIterator other = *this; ++index; return other; } Chris@50: inline IndexingIterator& operator--() { --index; return *this; } Chris@50: inline IndexingIterator operator--(int) { IndexingIterator other = *this; --index; return other; } Chris@50: Chris@50: inline IndexingIterator operator+(uint amount) const { return IndexingIterator(container, index + amount); } Chris@50: inline IndexingIterator operator-(uint amount) const { return IndexingIterator(container, index - amount); } Chris@50: inline IndexingIterator operator+( int amount) const { return IndexingIterator(container, index + amount); } Chris@50: inline IndexingIterator operator-( int amount) const { return IndexingIterator(container, index - amount); } Chris@50: Chris@50: inline int operator-(const IndexingIterator& other) const { return index - other.index; } Chris@50: Chris@50: inline IndexingIterator& operator+=(uint amount) { index += amount; return *this; } Chris@50: inline IndexingIterator& operator-=(uint amount) { index -= amount; return *this; } Chris@50: inline IndexingIterator& operator+=( int amount) { index += amount; return *this; } Chris@50: inline IndexingIterator& operator-=( int amount) { index -= amount; return *this; } Chris@50: Chris@50: // STL says comparing iterators of different containers is not allowed, so we only compare Chris@50: // indices here. Chris@50: inline bool operator==(const IndexingIterator& other) const { return index == other.index; } Chris@50: inline bool operator!=(const IndexingIterator& other) const { return index != other.index; } Chris@50: inline bool operator<=(const IndexingIterator& other) const { return index <= other.index; } Chris@50: inline bool operator>=(const IndexingIterator& other) const { return index >= other.index; } Chris@50: inline bool operator< (const IndexingIterator& other) const { return index < other.index; } Chris@50: inline bool operator> (const IndexingIterator& other) const { return index > other.index; } Chris@50: Chris@50: private: Chris@50: Container* container; Chris@50: uint index; Chris@50: Chris@50: friend Container; Chris@50: inline IndexingIterator(Container* container, uint index) Chris@50: : container(container), index(index) {} Chris@50: }; Chris@50: Chris@50: } // namespace _ (private) Chris@50: Chris@50: template Chris@50: struct List { Chris@50: // List of primitives. Chris@50: Chris@50: List() = delete; Chris@50: Chris@50: class Reader { Chris@50: public: Chris@50: typedef List Reads; Chris@50: Chris@50: inline Reader(): reader(_::elementSizeForType()) {} Chris@50: inline explicit Reader(_::ListReader reader): reader(reader) {} Chris@50: Chris@50: inline uint size() const { return reader.size() / ELEMENTS; } Chris@50: inline T operator[](uint index) const { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return reader.template getDataElement(index * ELEMENTS); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListReader reader; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: template Chris@50: friend struct List; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Builder { Chris@50: public: Chris@50: typedef List Builds; Chris@50: Chris@50: inline Builder(): builder(_::elementSizeForType()) {} Chris@50: inline Builder(decltype(nullptr)) {} Chris@50: inline explicit Builder(_::ListBuilder builder): builder(builder) {} Chris@50: Chris@50: inline operator Reader() const { return Reader(builder.asReader()); } Chris@50: inline Reader asReader() const { return Reader(builder.asReader()); } Chris@50: Chris@50: inline uint size() const { return builder.size() / ELEMENTS; } Chris@50: inline T operator[](uint index) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return builder.template getDataElement(index * ELEMENTS); Chris@50: } Chris@50: inline void set(uint index, T value) { Chris@50: // Alas, it is not possible to make operator[] return a reference to which you can assign, Chris@50: // since the encoded representation does not necessarily match the compiler's representation Chris@50: // of the type. We can't even return a clever class that implements operator T() and Chris@50: // operator=() because it will lead to surprising behavior when using type inference (e.g. Chris@50: // calling a template function with inferred argument types, or using "auto" or "decltype"). Chris@50: Chris@50: builder.template setDataElement(index * ELEMENTS, value); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() { return Iterator(this, 0); } Chris@50: inline Iterator end() { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListBuilder builder; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Pipeline {}; Chris@50: Chris@50: private: Chris@50: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { Chris@50: return builder.initList(_::elementSizeForType(), size * ELEMENTS); Chris@50: } Chris@50: inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { Chris@50: return builder.getList(_::elementSizeForType(), defaultValue); Chris@50: } Chris@50: inline static _::ListReader getFromPointer( Chris@50: const _::PointerReader& reader, const word* defaultValue) { Chris@50: return reader.getList(_::elementSizeForType(), defaultValue); Chris@50: } Chris@50: Chris@50: template Chris@50: friend struct List; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: }; Chris@50: Chris@50: template Chris@50: struct List: public List {}; Chris@50: Chris@50: template Chris@50: struct List { Chris@50: // List of structs. Chris@50: Chris@50: List() = delete; Chris@50: Chris@50: class Reader { Chris@50: public: Chris@50: typedef List Reads; Chris@50: Chris@50: inline Reader(): reader(ElementSize::INLINE_COMPOSITE) {} Chris@50: inline explicit Reader(_::ListReader reader): reader(reader) {} Chris@50: Chris@50: inline uint size() const { return reader.size() / ELEMENTS; } Chris@50: inline typename T::Reader operator[](uint index) const { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return typename T::Reader(reader.getStructElement(index * ELEMENTS)); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListReader reader; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: template Chris@50: friend struct List; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Builder { Chris@50: public: Chris@50: typedef List Builds; Chris@50: Chris@50: inline Builder(): builder(ElementSize::INLINE_COMPOSITE) {} Chris@50: inline Builder(decltype(nullptr)) {} Chris@50: inline explicit Builder(_::ListBuilder builder): builder(builder) {} Chris@50: Chris@50: inline operator Reader() const { return Reader(builder.asReader()); } Chris@50: inline Reader asReader() const { return Reader(builder.asReader()); } Chris@50: Chris@50: inline uint size() const { return builder.size() / ELEMENTS; } Chris@50: inline typename T::Builder operator[](uint index) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return typename T::Builder(builder.getStructElement(index * ELEMENTS)); Chris@50: } Chris@50: Chris@50: inline void adoptWithCaveats(uint index, Orphan&& orphan) { Chris@50: // Mostly behaves like you'd expect `adopt` to behave, but with two caveats originating from Chris@50: // the fact that structs in a struct list are allocated inline rather than by pointer: Chris@50: // * This actually performs a shallow copy, effectively adopting each of the orphan's Chris@50: // children rather than adopting the orphan itself. The orphan ends up being discarded, Chris@50: // possibly wasting space in the message object. Chris@50: // * If the orphan is larger than the target struct -- say, because the orphan was built Chris@50: // using a newer version of the schema that has additional fields -- it will be truncated, Chris@50: // losing data. Chris@50: Chris@50: KJ_IREQUIRE(index < size()); Chris@50: Chris@50: // We pass a zero-valued StructSize to asStruct() because we do not want the struct to be Chris@50: // expanded under any circumstances. We're just going to throw it away anyway, and Chris@50: // transferContentFrom() already carefully compares the struct sizes before transferring. Chris@50: builder.getStructElement(index * ELEMENTS).transferContentFrom( Chris@50: orphan.builder.asStruct(_::StructSize(0 * WORDS, 0 * POINTERS))); Chris@50: } Chris@50: inline void setWithCaveats(uint index, const typename T::Reader& reader) { Chris@50: // Mostly behaves like you'd expect `set` to behave, but with a caveat originating from Chris@50: // the fact that structs in a struct list are allocated inline rather than by pointer: Chris@50: // If the source struct is larger than the target struct -- say, because the source was built Chris@50: // using a newer version of the schema that has additional fields -- it will be truncated, Chris@50: // losing data. Chris@50: // Chris@50: // Note: If you are trying to concatenate some lists, use Orphanage::newOrphanConcat() to Chris@50: // do it without losing any data in case the source lists come from a newer version of the Chris@50: // protocol. (Plus, it's easier to use anyhow.) Chris@50: Chris@50: KJ_IREQUIRE(index < size()); Chris@50: builder.getStructElement(index * ELEMENTS).copyContentFrom(reader._reader); Chris@50: } Chris@50: Chris@50: // There are no init(), set(), adopt(), or disown() methods for lists of structs because the Chris@50: // elements of the list are inlined and are initialized when the list is initialized. This Chris@50: // means that init() would be redundant, and set() would risk data loss if the input struct Chris@50: // were from a newer version of the protocol. Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() { return Iterator(this, 0); } Chris@50: inline Iterator end() { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListBuilder builder; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Pipeline {}; Chris@50: Chris@50: private: Chris@50: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { Chris@50: return builder.initStructList(size * ELEMENTS, _::structSize()); Chris@50: } Chris@50: inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { Chris@50: return builder.getStructList(_::structSize(), defaultValue); Chris@50: } Chris@50: inline static _::ListReader getFromPointer( Chris@50: const _::PointerReader& reader, const word* defaultValue) { Chris@50: return reader.getList(ElementSize::INLINE_COMPOSITE, defaultValue); Chris@50: } Chris@50: Chris@50: template Chris@50: friend struct List; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: }; Chris@50: Chris@50: template Chris@50: struct List, Kind::LIST> { Chris@50: // List of lists. Chris@50: Chris@50: List() = delete; Chris@50: Chris@50: class Reader { Chris@50: public: Chris@50: typedef List> Reads; Chris@50: Chris@50: inline Reader(): reader(ElementSize::POINTER) {} Chris@50: inline explicit Reader(_::ListReader reader): reader(reader) {} Chris@50: Chris@50: inline uint size() const { return reader.size() / ELEMENTS; } Chris@50: inline typename List::Reader operator[](uint index) const { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return typename List::Reader( Chris@50: _::PointerHelpers>::get(reader.getPointerElement(index * ELEMENTS))); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator::Reader> Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListReader reader; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: template Chris@50: friend struct List; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Builder { Chris@50: public: Chris@50: typedef List> Builds; Chris@50: Chris@50: inline Builder(): builder(ElementSize::POINTER) {} Chris@50: inline Builder(decltype(nullptr)) {} Chris@50: inline explicit Builder(_::ListBuilder builder): builder(builder) {} Chris@50: Chris@50: inline operator Reader() const { return Reader(builder.asReader()); } Chris@50: inline Reader asReader() const { return Reader(builder.asReader()); } Chris@50: Chris@50: inline uint size() const { return builder.size() / ELEMENTS; } Chris@50: inline typename List::Builder operator[](uint index) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return typename List::Builder( Chris@50: _::PointerHelpers>::get(builder.getPointerElement(index * ELEMENTS))); Chris@50: } Chris@50: inline typename List::Builder init(uint index, uint size) { Chris@50: KJ_IREQUIRE(index < this->size()); Chris@50: return typename List::Builder( Chris@50: _::PointerHelpers>::init(builder.getPointerElement(index * ELEMENTS), size)); Chris@50: } Chris@50: inline void set(uint index, typename List::Reader value) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: builder.getPointerElement(index * ELEMENTS).setList(value.reader); Chris@50: } Chris@50: void set(uint index, std::initializer_list> value) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: auto l = init(index, value.size()); Chris@50: uint i = 0; Chris@50: for (auto& element: value) { Chris@50: l.set(i++, element); Chris@50: } Chris@50: } Chris@50: inline void adopt(uint index, Orphan&& value) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: builder.getPointerElement(index * ELEMENTS).adopt(kj::mv(value.builder)); Chris@50: } Chris@50: inline Orphan disown(uint index) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return Orphan(builder.getPointerElement(index * ELEMENTS).disown()); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator::Builder> Iterator; Chris@50: inline Iterator begin() { return Iterator(this, 0); } Chris@50: inline Iterator end() { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListBuilder builder; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Pipeline {}; Chris@50: Chris@50: private: Chris@50: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { Chris@50: return builder.initList(ElementSize::POINTER, size * ELEMENTS); Chris@50: } Chris@50: inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { Chris@50: return builder.getList(ElementSize::POINTER, defaultValue); Chris@50: } Chris@50: inline static _::ListReader getFromPointer( Chris@50: const _::PointerReader& reader, const word* defaultValue) { Chris@50: return reader.getList(ElementSize::POINTER, defaultValue); Chris@50: } Chris@50: Chris@50: template Chris@50: friend struct List; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: }; Chris@50: Chris@50: template Chris@50: struct List { Chris@50: List() = delete; Chris@50: Chris@50: class Reader { Chris@50: public: Chris@50: typedef List Reads; Chris@50: Chris@50: inline Reader(): reader(ElementSize::POINTER) {} Chris@50: inline explicit Reader(_::ListReader reader): reader(reader) {} Chris@50: Chris@50: inline uint size() const { return reader.size() / ELEMENTS; } Chris@50: inline typename T::Reader operator[](uint index) const { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return reader.getPointerElement(index * ELEMENTS).template getBlob(nullptr, 0 * BYTES); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() const { return Iterator(this, 0); } Chris@50: inline Iterator end() const { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListReader reader; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: template Chris@50: friend struct List; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Builder { Chris@50: public: Chris@50: typedef List Builds; Chris@50: Chris@50: inline Builder(): builder(ElementSize::POINTER) {} Chris@50: inline Builder(decltype(nullptr)) {} Chris@50: inline explicit Builder(_::ListBuilder builder): builder(builder) {} Chris@50: Chris@50: inline operator Reader() const { return Reader(builder.asReader()); } Chris@50: inline Reader asReader() const { return Reader(builder.asReader()); } Chris@50: Chris@50: inline uint size() const { return builder.size() / ELEMENTS; } Chris@50: inline typename T::Builder operator[](uint index) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return builder.getPointerElement(index * ELEMENTS).template getBlob(nullptr, 0 * BYTES); Chris@50: } Chris@50: inline void set(uint index, typename T::Reader value) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: builder.getPointerElement(index * ELEMENTS).template setBlob(value); Chris@50: } Chris@50: inline typename T::Builder init(uint index, uint size) { Chris@50: KJ_IREQUIRE(index < this->size()); Chris@50: return builder.getPointerElement(index * ELEMENTS).template initBlob(size * BYTES); Chris@50: } Chris@50: inline void adopt(uint index, Orphan&& value) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: builder.getPointerElement(index * ELEMENTS).adopt(kj::mv(value.builder)); Chris@50: } Chris@50: inline Orphan disown(uint index) { Chris@50: KJ_IREQUIRE(index < size()); Chris@50: return Orphan(builder.getPointerElement(index * ELEMENTS).disown()); Chris@50: } Chris@50: Chris@50: typedef _::IndexingIterator Iterator; Chris@50: inline Iterator begin() { return Iterator(this, 0); } Chris@50: inline Iterator end() { return Iterator(this, size()); } Chris@50: Chris@50: private: Chris@50: _::ListBuilder builder; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: friend class Orphanage; Chris@50: template Chris@50: friend struct ToDynamic_; Chris@50: }; Chris@50: Chris@50: class Pipeline {}; Chris@50: Chris@50: private: Chris@50: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { Chris@50: return builder.initList(ElementSize::POINTER, size * ELEMENTS); Chris@50: } Chris@50: inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { Chris@50: return builder.getList(ElementSize::POINTER, defaultValue); Chris@50: } Chris@50: inline static _::ListReader getFromPointer( Chris@50: const _::PointerReader& reader, const word* defaultValue) { Chris@50: return reader.getList(ElementSize::POINTER, defaultValue); Chris@50: } Chris@50: Chris@50: template Chris@50: friend struct List; Chris@50: template Chris@50: friend struct _::PointerHelpers; Chris@50: }; Chris@50: Chris@50: } // namespace capnp Chris@50: Chris@50: #ifdef KJ_STD_COMPAT Chris@50: namespace std { Chris@50: Chris@50: template Chris@50: struct iterator_traits> Chris@50: : public std::iterator {}; Chris@50: Chris@50: } // namespace std Chris@50: #endif // KJ_STD_COMPAT Chris@50: Chris@50: #endif // CAPNP_LIST_H_