Bug 1184235 - Add mozilla::Vector::emplaceBack; r=froydnj

This commit is contained in:
Nick Fitzgerald 2015-07-20 11:13:28 -07:00
parent e5df54a8f1
commit 801e65b0ec
2 changed files with 79 additions and 0 deletions

View File

@ -564,6 +564,18 @@ public:
*/
template<typename U> bool append(U&& aU);
/**
* Construct a T in-place as a new entry at the end of this vector.
*/
template<typename... Args>
bool emplaceBack(Args&&... aArgs)
{
if (!growByUninitialized(1))
return false;
new (&back()) T(Forward<Args>(aArgs)...);
return true;
}
template<typename U, size_t O, class BP, class UV>
bool appendAll(const VectorBase<U, O, BP, UV>& aU);
bool appendN(const T& aT, size_t aN);

View File

@ -5,16 +5,20 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"
using mozilla::detail::VectorTesting;
using mozilla::MakeUnique;
using mozilla::Move;
using mozilla::UniquePtr;
using mozilla::Vector;
struct mozilla::detail::VectorTesting
{
static void testReserved();
static void testConstRange();
static void testEmplaceBack();
};
void
@ -97,10 +101,73 @@ mozilla::detail::VectorTesting::testConstRange()
#endif
}
namespace {
struct S
{
size_t j;
UniquePtr<size_t> k;
static size_t constructCount;
static size_t moveCount;
S(size_t j, size_t k)
: j(j)
, k(MakeUnique<size_t>(k))
{
constructCount++;
}
S(S&& rhs)
: j(rhs.j)
, k(Move(rhs.k))
{
rhs.~S();
moveCount++;
}
S(const S&) = delete;
S& operator=(const S&) = delete;
};
size_t S::constructCount = 0;
size_t S::moveCount = 0;
}
void
mozilla::detail::VectorTesting::testEmplaceBack()
{
Vector<S> vec;
MOZ_RELEASE_ASSERT(vec.reserve(20));
for (size_t i = 0; i < 10; i++) {
S s(i, i*i);
MOZ_RELEASE_ASSERT(vec.append(Move(s)));
}
MOZ_RELEASE_ASSERT(vec.length() == 10);
MOZ_RELEASE_ASSERT(S::constructCount == 10);
MOZ_RELEASE_ASSERT(S::moveCount == 10);
for (size_t i = 10; i < 20; i++) {
MOZ_RELEASE_ASSERT(vec.emplaceBack(i, i*i));
}
MOZ_RELEASE_ASSERT(vec.length() == 20);
MOZ_RELEASE_ASSERT(S::constructCount == 20);
MOZ_RELEASE_ASSERT(S::moveCount == 10);
for (size_t i = 0; i < 20; i++) {
MOZ_RELEASE_ASSERT(vec[i].j == i);
MOZ_RELEASE_ASSERT(*vec[i].k == i*i);
}
}
int
main()
{
VectorTesting::testReserved();
VectorTesting::testConstRange();
VectorTesting::testEmplaceBack();
}