зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1322700 - Enable range-for with nsDeque - r=froydnj
It's now possible to write: for (void* item : deque) { ... } MozReview-Commit-ID: FLoczCZd77y --HG-- extra : rebase_source : 237293e94b478beb2bf352c1179d42c289dda145
This commit is contained in:
Родитель
7ec24adbdb
Коммит
d5ce43788f
|
@ -59,8 +59,6 @@ public:
|
|||
* The deque stores pointers to items.
|
||||
*/
|
||||
|
||||
class nsDequeIterator;
|
||||
|
||||
class nsDeque
|
||||
{
|
||||
typedef mozilla::fallible_t fallible_t;
|
||||
|
@ -156,6 +154,21 @@ public:
|
|||
*/
|
||||
void ForEach(nsDequeFunctor& aFunctor) const;
|
||||
|
||||
class ConstIterator
|
||||
{
|
||||
public:
|
||||
ConstIterator(const nsDeque& aDeque, size_t aIndex) : mDeque(aDeque), mIndex(aIndex) { }
|
||||
ConstIterator& operator++() { ++mIndex; return *this; }
|
||||
bool operator==(const ConstIterator& aOther) const { return mIndex == aOther.mIndex; }
|
||||
bool operator!=(const ConstIterator& aOther) const { return mIndex != aOther.mIndex; }
|
||||
void* operator*() const { return mDeque.ObjectAt(mIndex); }
|
||||
private:
|
||||
const nsDeque& mDeque;
|
||||
size_t mIndex;
|
||||
};
|
||||
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
||||
ConstIterator end() const { return ConstIterator(*this, mSize); }
|
||||
|
||||
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
||||
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
||||
|
||||
|
|
|
@ -13,15 +13,15 @@
|
|||
Now define the token deallocator class...
|
||||
**************************************************************/
|
||||
namespace TestNsDeque {
|
||||
|
||||
class _Dealloc: public nsDequeFunctor
|
||||
|
||||
class _Dealloc: public nsDequeFunctor
|
||||
{
|
||||
virtual void* operator()(void* aObject) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
static bool VerifyContents(const nsDeque& aDeque, const int* aContents, size_t aLength)
|
||||
static bool VerifyContents(const nsDeque& aDeque, const int* aContents, size_t aLength)
|
||||
{
|
||||
for (size_t i=0; i<aLength; ++i) {
|
||||
if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
|
||||
|
@ -33,9 +33,9 @@ namespace TestNsDeque {
|
|||
|
||||
class Deallocator: public nsDequeFunctor
|
||||
{
|
||||
virtual void* operator()(void* aObject)
|
||||
virtual void* operator()(void* aObject)
|
||||
{
|
||||
if (aObject)
|
||||
if (aObject)
|
||||
{
|
||||
// Set value to -1, to use in test function.
|
||||
*((int*)aObject) = -1;
|
||||
|
@ -47,9 +47,9 @@ namespace TestNsDeque {
|
|||
|
||||
class ForEachAdder: public nsDequeFunctor
|
||||
{
|
||||
virtual void* operator()(void* aObject)
|
||||
virtual void* operator()(void* aObject)
|
||||
{
|
||||
if (aObject)
|
||||
if (aObject)
|
||||
{
|
||||
sum += *(int*)aObject;
|
||||
}
|
||||
|
@ -59,10 +59,10 @@ namespace TestNsDeque {
|
|||
|
||||
private:
|
||||
int sum = 0;
|
||||
|
||||
|
||||
public:
|
||||
int GetSum() { return sum; }
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ TEST(NsDeque, OriginalTest)
|
|||
size_t i=0;
|
||||
int temp;
|
||||
nsDeque theDeque(new _Dealloc); //construct a simple one...
|
||||
|
||||
|
||||
// ints = [0...199]
|
||||
for (i=0;i<size;i++) { //initialize'em
|
||||
ints[i]=static_cast<int>(i);
|
||||
|
@ -312,7 +312,7 @@ TEST(NsDeque,TestEraseShouldCallDeallocator)
|
|||
|
||||
// Now check it again.
|
||||
CheckIfQueueEmpty(d);
|
||||
|
||||
|
||||
for (size_t i=0; i < NumTestValues; i++)
|
||||
{
|
||||
EXPECT_EQ(-1, *(testArray[i])) << "Erase should call deallocator: " << *(testArray[i]);
|
||||
|
@ -340,3 +340,27 @@ TEST(NsDeque, TestForEach)
|
|||
|
||||
d.Erase();
|
||||
}
|
||||
|
||||
TEST(NsDeque, TestRangeFor)
|
||||
{
|
||||
nsDeque d(new Deallocator());
|
||||
const size_t NumTestValues = 8;
|
||||
int sum = 0;
|
||||
|
||||
int* testArray[NumTestValues];
|
||||
for (size_t i=0; i < NumTestValues; i++)
|
||||
{
|
||||
testArray[i] = new int();
|
||||
*(testArray[i]) = i;
|
||||
sum += i;
|
||||
d.Push((void*)testArray[i]);
|
||||
}
|
||||
|
||||
int added = 0;
|
||||
for (void* ob : d) {
|
||||
added += *(int*)ob;
|
||||
}
|
||||
EXPECT_EQ(sum, added) << "Range-for should iterate over values";
|
||||
|
||||
d.Erase();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче