Bug 1139993 - Add VectorBase::ConstRange that gives out |const T&| entries. r=Waldo

This commit is contained in:
Nick Fitzgerald 2015-03-06 15:37:00 -05:00
parent eab815bb2e
commit c2d50838e1
2 changed files with 45 additions and 1 deletions

View File

@ -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 */

View File

@ -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<int> vec;
for (int i = 0; i < 10; i++) {
MOZ_RELEASE_ASSERT(vec.append(i));
}
const auto &vecRef = vec;
Vector<int>::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();
}