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