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