mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-28 15:23:51 +00:00
Bug 1595750 - Add MakeBackInserter function to create a back-inserting output iterator for nsTArray. r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D52685 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
ae692f9401
commit
28f1dd4c5b
@ -2730,6 +2730,30 @@ Span<const ElementType> MakeSpan(
|
||||
return aTArray;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class nsTArrayBackInserter
|
||||
: public std::iterator<std::output_iterator_tag, void, void, void, void> {
|
||||
nsTArray<T>* mArray;
|
||||
|
||||
public:
|
||||
explicit nsTArrayBackInserter(nsTArray<T>& aArray) : mArray{&aArray} {}
|
||||
|
||||
template <typename O>
|
||||
nsTArrayBackInserter& operator=(O&& aValue) {
|
||||
mArray->EmplaceBack(std::forward<O>(aValue));
|
||||
return *this;
|
||||
}
|
||||
|
||||
nsTArrayBackInserter& operator*() { return *this; }
|
||||
|
||||
void operator++() {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
auto MakeBackInserter(nsTArray<T>& aArray) {
|
||||
return nsTArrayBackInserter<T>{aArray};
|
||||
}
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
// MOZ_DBG support
|
||||
|
@ -443,4 +443,22 @@ TEST(TArray, RemoveElementsAt_ByIterator)
|
||||
ASSERT_EQ(expected, array);
|
||||
}
|
||||
|
||||
static_assert(std::is_copy_assignable<decltype(
|
||||
MakeBackInserter(std::declval<nsTArray<int>&>()))>::value,
|
||||
"output iteraror must be copy-assignable");
|
||||
static_assert(std::is_copy_constructible<decltype(
|
||||
MakeBackInserter(std::declval<nsTArray<int>&>()))>::value,
|
||||
"output iterator must be copy-constructible");
|
||||
|
||||
TEST(TArray, MakeBackInserter)
|
||||
{
|
||||
const std::vector<int> src{1, 2, 3, 4};
|
||||
nsTArray<int> dst;
|
||||
|
||||
std::copy(src.begin(), src.end(), MakeBackInserter(dst));
|
||||
|
||||
const nsTArray<int> expected{1, 2, 3, 4};
|
||||
ASSERT_EQ(expected, dst);
|
||||
}
|
||||
|
||||
} // namespace TestTArray
|
||||
|
Loading…
Reference in New Issue
Block a user