From c2d50838e17b8870f835ae8c218ec8abb4bf1580 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 6 Mar 2015 15:37:00 -0500 Subject: [PATCH] Bug 1139993 - Add VectorBase::ConstRange that gives out |const T&| entries. r=Waldo --- mfbt/Vector.h | 22 +++++++++++++++++++++- mfbt/tests/TestVector.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/mfbt/Vector.h b/mfbt/Vector.h index 989841df897..874e56f712c 100644 --- a/mfbt/Vector.h +++ b/mfbt/Vector.h @@ -473,7 +473,6 @@ public: } public: - Range() {} bool empty() const { return mCur == mEnd; } size_t remain() const { return PointerRangeSize(mCur, mEnd); } T& front() const { MOZ_ASSERT(!empty()); return *mCur; } @@ -481,7 +480,28 @@ public: T popCopyFront() { MOZ_ASSERT(!empty()); return *mCur++; } }; + class ConstRange + { + friend class VectorBase; + const T* mCur; + const T* mEnd; + ConstRange(const T* aCur, const T* aEnd) + : mCur(aCur) + , mEnd(aEnd) + { + MOZ_ASSERT(aCur <= aEnd); + } + + public: + bool empty() const { return mCur == mEnd; } + size_t remain() const { return PointerRangeSize(mCur, mEnd); } + const T& front() const { MOZ_ASSERT(!empty()); return *mCur; } + void popFront() { MOZ_ASSERT(!empty()); ++mCur; } + T popCopyFront() { MOZ_ASSERT(!empty()); return *mCur++; } + }; + Range all() { return Range(begin(), end()); } + ConstRange all() const { return ConstRange(begin(), end()); } /* mutators */ diff --git a/mfbt/tests/TestVector.cpp b/mfbt/tests/TestVector.cpp index dff589e3f16..794c3f08fdf 100644 --- a/mfbt/tests/TestVector.cpp +++ b/mfbt/tests/TestVector.cpp @@ -14,6 +14,7 @@ using mozilla::Vector; struct mozilla::detail::VectorTesting { static void testReserved(); + static void testConstRange(); }; void @@ -75,8 +76,31 @@ mozilla::detail::VectorTesting::testReserved() #endif } +void +mozilla::detail::VectorTesting::testConstRange() +{ +#ifdef DEBUG + Vector vec; + + for (int i = 0; i < 10; i++) { + MOZ_RELEASE_ASSERT(vec.append(i)); + } + + const auto &vecRef = vec; + + Vector::ConstRange range = vecRef.all(); + for (int i = 0; i < 10; i++) { + MOZ_RELEASE_ASSERT(!range.empty()); + MOZ_RELEASE_ASSERT(range.front() == i); + range.popFront(); + } +#endif +} + + int main() { VectorTesting::testReserved(); + VectorTesting::testConstRange(); }