From bd86bf60ecd1108947cf3c9b60d630b00b4eab5b Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 16 Jul 2014 12:07:24 -0700 Subject: [PATCH] Made sure tests.cpp is testing the new vector iterator functionality. Also fixes a potential big-endian bug, and makes iterators work correctly with pointer types. Change-Id: Ib7f88fe9e6053d1a9afa7895fba0695627c158b1 Tested: on Windows and Linux --- include/flatbuffers/flatbuffers.h | 16 ++++++++++------ tests/test.cpp | 23 ++++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 02f29e9..ebd0c37 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -163,15 +164,14 @@ template struct IndirectHelper> { static const size_t element_stride = sizeof(uoffset_t); static return_type Read(const uint8_t *p, uoffset_t i) { p += i * sizeof(uoffset_t); - return EndianScalar(reinterpret_cast( - p + ReadScalar(p))); + return reinterpret_cast(p + ReadScalar(p)); } }; template struct IndirectHelper { - typedef const T &return_type; + typedef const T *return_type; static const size_t element_stride = sizeof(T); static return_type Read(const uint8_t *p, uoffset_t i) { - return *reinterpret_cast(p + i * sizeof(T)); + return reinterpret_cast(p + i * sizeof(T)); } }; @@ -213,12 +213,16 @@ public: return data_ != other.data_; } + ptrdiff_t operator-(const VectorIterator& other) const { + return (data_ - other.data_) / IndirectHelper::element_stride; + } + typename super_type::value_type operator *() const { return IndirectHelper::Read(data_, 0); } - typename super_type::pointer operator->() const { - return &IndirectHelper::Read(data_, 0); + typename super_type::value_type operator->() const { + return IndirectHelper::Read(data_, 0); } VectorIterator &operator++() { diff --git a/tests/test.cpp b/tests/test.cpp index 96b144d..b3dcab3 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -135,8 +135,8 @@ void AccessFlatBufferTest(const std::string &flatbuf) { auto inventory = monster->inventory(); TEST_NOTNULL(inventory); unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - for (flatbuffers::uoffset_t i = 0; i < inventory->Length(); i++) - TEST_EQ(inventory->Get(i), inv_data[i]); + for (auto it = inventory->begin(); it != inventory->end(); ++it) + TEST_EQ(*it, inv_data[it - inventory->begin()]); // Example of accessing a union: TEST_EQ(monster->test_type(), Any_Monster); // First make sure which it is. @@ -153,7 +153,8 @@ void AccessFlatBufferTest(const std::string &flatbuf) { // Example of accessing a vector of tables: auto vecoftables = monster->testarrayoftables(); TEST_EQ(vecoftables->Length(), 1U); - TEST_EQ(vecoftables->Get(0)->hp(), 20); + for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it) + TEST_EQ(it->hp(), 20); // Since Flatbuffers uses explicit mechanisms to override the default // compiler alignment, double check that the compiler indeed obeys them: @@ -163,12 +164,16 @@ void AccessFlatBufferTest(const std::string &flatbuf) { auto tests = monster->test4(); TEST_NOTNULL(tests); - auto &test_0 = tests->Get(0); - auto &test_1 = tests->Get(1); - TEST_EQ(test_0.a(), 10); - TEST_EQ(test_0.b(), 20); - TEST_EQ(test_1.a(), 30); - TEST_EQ(test_1.b(), 40); + auto test_0 = tests->Get(0); + auto test_1 = tests->Get(1); + TEST_EQ(test_0->a(), 10); + TEST_EQ(test_0->b(), 20); + TEST_EQ(test_1->a(), 30); + TEST_EQ(test_1->b(), 40); + for (auto it = tests->begin(); it != tests->end(); ++it) { + TEST_EQ(it->a() == 10 || it->a() == 30, true); // Just testing iterators. + } + } // example of parsing text straight into a buffer, and generating -- 2.7.4