From f50a7d874f5ccff98992d76038d877a06e828063 Mon Sep 17 00:00:00 2001 From: "roc+%cs.cmu.edu" Date: Mon, 18 Dec 2006 01:17:23 +0000 Subject: [PATCH] Bug 363767. Add AppendElements() API (that appends empty elements) to nsTArray. r=sicking,sr=darin git-svn-id: svn://10.0.0.236/trunk@217103 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/glue/nsTArray.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mozilla/xpcom/glue/nsTArray.h b/mozilla/xpcom/glue/nsTArray.h index 8b2c7760eed..21a2b750646 100644 --- a/mozilla/xpcom/glue/nsTArray.h +++ b/mozilla/xpcom/glue/nsTArray.h @@ -519,16 +519,26 @@ class nsTArray : public nsTArray_base { return AppendElements(&item, 1); } + // Append new elements without copy-constructing. This is useful to avoid + // temporaries. + // @return A pointer to the newly appended elements, or null on OOM. + elem_type *AppendElements(size_type count) { + if (!EnsureCapacity(Length() + count, sizeof(elem_type))) + return nsnull; + elem_type *elems = Elements() + Length(); + size_type i; + for (i = 0; i < count; ++i) { + elem_traits::Construct(elems + i); + } + IncrementLength(count); + return elems; + } + // Append a new element without copy-constructing. This is useful to avoid // temporaries. // @return A pointer to the newly appended element, or null on OOM. elem_type *AppendElement() { - if (!EnsureCapacity(Length() + 1, sizeof(elem_type))) - return nsnull; - elem_type *elem = Elements() + Length(); - elem_traits::Construct(elem); - IncrementLength(1); - return elem; + return AppendElements(1); } // This method removes a range of elements from this array.