diff options
author | Henry Lee <lee0906@hotmail.com> | 2019-01-25 08:24:01 +1100 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-01-24 13:24:01 -0800 |
commit | c2f40c37b2c0cf2c7fbb82b4b760276771abe7a1 (patch) | |
tree | 930e2c3d1ab6e90061f8d47665e778d6d85b0280 /include | |
parent | 63d51afd1196336a7d1f56a988091ef05deb1c62 (diff) | |
download | flatbuffers-c2f40c37b2c0cf2c7fbb82b4b760276771abe7a1.tar.gz flatbuffers-c2f40c37b2c0cf2c7fbb82b4b760276771abe7a1.tar.bz2 flatbuffers-c2f40c37b2c0cf2c7fbb82b4b760276771abe7a1.zip |
[C++]Support reverse iterator in Vector (#5128)
* Add `const` keyword to the `operator-(const uoffset_t &)` function in
`VectorIterator`
* Support reverse iterator in Vector
Introduced a new VectorReverseIterator type. We cannot directly use
`std::reverse_iterator<VectorIterator>` because the signature of
`operator*` and `operator->` in the VectorIterator class are not
standard signatures.
Also added `rbegin()`, `rend()`, `cbegin()`, `cend()`, `crbegin()`
and `crend()` in the Vector class.
Diffstat (limited to 'include')
-rw-r--r-- | include/flatbuffers/flatbuffers.h | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 3f7cbbeb..4a292f1d 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -116,6 +116,7 @@ template<typename T, typename IT> struct VectorIterator { VectorIterator(const uint8_t *data, uoffset_t i) : data_(data + IndirectHelper<T>::element_stride * i) {} VectorIterator(const VectorIterator &other) : data_(other.data_) {} + VectorIterator() : data_(nullptr) {} VectorIterator &operator=(const VectorIterator &other) { data_ = other.data_; @@ -183,7 +184,7 @@ template<typename T, typename IT> struct VectorIterator { return temp; } - VectorIterator operator-(const uoffset_t &offset) { + VectorIterator operator-(const uoffset_t &offset) const { return VectorIterator(data_ - offset * IndirectHelper<T>::element_stride, 0); } @@ -197,6 +198,19 @@ template<typename T, typename IT> struct VectorIterator { const uint8_t *data_; }; +template<typename Iterator> struct VectorReverseIterator : + public std::reverse_iterator<Iterator> { + + explicit VectorReverseIterator(Iterator iter) : iter_(iter) {} + + typename Iterator::value_type operator*() const { return *(iter_ - 1); } + + typename Iterator::value_type operator->() const { return *(iter_ - 1); } + + private: + Iterator iter_; +}; + struct String; // This is used as a helper type for accessing vectors. @@ -207,6 +221,8 @@ template<typename T> class Vector { iterator; typedef VectorIterator<T, typename IndirectHelper<T>::return_type> const_iterator; + typedef VectorReverseIterator<iterator> reverse_iterator; + typedef VectorReverseIterator<const_iterator> const_reverse_iterator; uoffset_t size() const { return EndianScalar(length_); } @@ -253,6 +269,20 @@ template<typename T> class Vector { iterator end() { return iterator(Data(), size()); } const_iterator end() const { return const_iterator(Data(), size()); } + reverse_iterator rbegin() { return reverse_iterator(end()); } + const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + + reverse_iterator rend() { return reverse_iterator(end()); } + const_reverse_iterator rend() const { return const_reverse_iterator(end()); } + + const_iterator cbegin() const { return begin(); } + + const_iterator cend() const { return end(); } + + const_reverse_iterator crbegin() const { return rbegin(); } + + const_reverse_iterator crend() const { return rend(); } + // Change elements if you have a non-const pointer to this object. // Scalars only. See reflection.h, and the documentation. void Mutate(uoffset_t i, const T &val) { |