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