Bug 1645447 - Add ForwardIterator::Remove and EndLimitedIterator::Remove functions. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D79512
This commit is contained in:
Simon Giesecke 2020-06-15 17:04:08 +00:00
Родитель 87a2eb06f5
Коммит 08dfb8127e
2 изменённых файлов: 35 добавлений и 0 удалений

Просмотреть файл

@ -332,6 +332,13 @@ class nsAutoTObserverArray : protected nsTObserverArray_base {
NS_ASSERTION(HasMore(), "iterating beyond end of array");
return base_type::mArray.Elements()[base_type::mPosition++];
}
// Removes the element at the current iterator position.
// (the last element returned from |GetNext()|)
// This will not affect the next call to |GetNext()|
void Remove() {
return base_type::mArray.RemoveElementAt(base_type::mPosition - 1);
}
};
// EndLimitedIterator works like ForwardIterator, but will not iterate new
@ -357,6 +364,13 @@ class nsAutoTObserverArray : protected nsTObserverArray_base {
return base_type::mArray.Elements()[base_type::mPosition++];
}
// Removes the element at the current iterator position.
// (the last element returned from |GetNext()|)
// This will not affect the next call to |GetNext()|
void Remove() {
return base_type::mArray.RemoveElementAt(base_type::mPosition - 1);
}
private:
ForwardIterator mEnd;
};

Просмотреть файл

@ -30,6 +30,7 @@ typedef nsTObserverArray<int> IntArray;
<< "During test " << testNum << ", iterator finished too early"; \
} while (0)
// XXX Split this up into independent test cases
TEST(ObserverArray, Tests)
{
IntArray arr;
@ -156,6 +157,24 @@ TEST(ObserverArray, Tests)
*/
}
TEST(ObserverArray, ForwardIterator_Remove)
{
static const int expected[] = {3, 4};
IntArray arr;
arr.AppendElement(3);
arr.AppendElement(4);
size_t count = 0;
for (auto iter = IntArray::ForwardIterator{arr}; iter.HasMore();) {
const int next = iter.GetNext();
iter.Remove();
ASSERT_EQ(expected[count++], next);
}
ASSERT_EQ(2u, count);
}
TEST(ObserverArray, RangeBasedFor_Value_Forward_NonEmpty)
{
IntArray arr;
@ -550,3 +569,5 @@ TEST(ObserverArray, RangeBasedFor_Reference_NonObserving_NonEmpty)
EXPECT_EQ(2u, iterations);
EXPECT_EQ(7, sum);
}
// TODO add tests for EndLimitedIterator