mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 22:43:57 +00:00
Makes VectorIterator compatible with STL iterators. (#4768)
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
ecc07e7793
commit
c7a797b966
@@ -91,7 +91,7 @@ template<typename T> struct IndirectHelper<const T *> {
|
|||||||
template<typename T, typename IT> struct VectorIterator {
|
template<typename T, typename IT> struct VectorIterator {
|
||||||
typedef std::random_access_iterator_tag iterator_category;
|
typedef std::random_access_iterator_tag iterator_category;
|
||||||
typedef IT value_type;
|
typedef IT value_type;
|
||||||
typedef uoffset_t difference_type;
|
typedef ptrdiff_t difference_type;
|
||||||
typedef IT *pointer;
|
typedef IT *pointer;
|
||||||
typedef IT &reference;
|
typedef IT &reference;
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ template<typename T, typename IT> struct VectorIterator {
|
|||||||
return data_ != other.data_;
|
return data_ != other.data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptrdiff_t operator-(const VectorIterator &other) const {
|
difference_type operator-(const VectorIterator &other) const {
|
||||||
return (data_ - other.data_) / IndirectHelper<T>::element_stride;
|
return (data_ - other.data_) / IndirectHelper<T>::element_stride;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -273,8 +273,13 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
|
|||||||
TEST_EQ(VectorLength(inventory), 10UL); // Works even if inventory is null.
|
TEST_EQ(VectorLength(inventory), 10UL); // Works even if inventory is null.
|
||||||
TEST_NOTNULL(inventory);
|
TEST_NOTNULL(inventory);
|
||||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||||
for (auto it = inventory->begin(); it != inventory->end(); ++it)
|
// Check compatibilty of iterators with STL.
|
||||||
TEST_EQ(*it, inv_data[it - inventory->begin()]);
|
std::vector<unsigned char> inv_vec(inventory->begin(), inventory->end());
|
||||||
|
for (auto it = inventory->begin(); it != inventory->end(); ++it) {
|
||||||
|
auto indx = it - inventory->begin();
|
||||||
|
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
|
||||||
|
TEST_EQ(*it, inv_data[indx]);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_EQ(monster->color(), Color_Blue);
|
TEST_EQ(monster->color(), Color_Blue);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user