Bug 1264830 - Part 1: Add an nsStyleAutoArray array type, similar to AutoTArray<...,1> but memmovable. r=bholley

Existing uses of AutoTArray in style structs makes them non-memmovable.
We introduce this AutoTArray-alike class for use by those style struct
members that really do need to use an auto array's built-in allocation.
This commit is contained in:
Cameron McCormack 2016-04-19 09:51:15 +10:00
Родитель d056b90405
Коммит 25fd6bca14
1 изменённых файлов: 59 добавлений и 0 удалений

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

@ -453,6 +453,65 @@ struct nsStyleColor
nscolor mColor; // [inherited] nscolor mColor; // [inherited]
}; };
/**
* An array of objects, similar to AutoTArray<T,1> but which is memmovable. It
* always has length >= 1.
*/
template<typename T>
class nsStyleAutoArray
{
public:
// This constructor places a single element in mFirstElement.
enum WithSingleInitialElement { WITH_SINGLE_INITIAL_ELEMENT };
explicit nsStyleAutoArray(WithSingleInitialElement) {}
nsStyleAutoArray(const nsStyleAutoArray& aOther) { *this = aOther; }
nsStyleAutoArray& operator=(const nsStyleAutoArray& aOther) {
mFirstElement = aOther.mFirstElement;
mOtherElements = aOther.mOtherElements;
return *this;
}
bool operator==(const nsStyleAutoArray& aOther) const {
return Length() == aOther.Length() &&
mFirstElement == aOther.mFirstElement &&
mOtherElements == aOther.mOtherElements;
}
bool operator!=(const nsStyleAutoArray& aOther) const {
return !(*this == aOther);
}
size_t Length() const {
return mOtherElements.Length() + 1;
}
const T& operator[](size_t aIndex) const {
return aIndex == 0 ? mFirstElement : mOtherElements[aIndex - 1];
}
T& operator[](size_t aIndex) {
return aIndex == 0 ? mFirstElement : mOtherElements[aIndex - 1];
}
void EnsureLengthAtLeast(size_t aMinLen) {
if (aMinLen > 0) {
mOtherElements.EnsureLengthAtLeast(aMinLen - 1);
}
}
void SetLengthNonZero(size_t aNewLen) {
MOZ_ASSERT(aNewLen > 0);
mOtherElements.SetLength(aNewLen - 1);
}
void TruncateLengthNonZero(size_t aNewLen) {
MOZ_ASSERT(aNewLen > 0);
MOZ_ASSERT(aNewLen <= Length());
mOtherElements.TruncateLength(aNewLen - 1);
}
private:
T mFirstElement;
nsTArray<T> mOtherElements;
};
struct nsStyleImageLayers { struct nsStyleImageLayers {
nsStyleImageLayers(); nsStyleImageLayers();
nsStyleImageLayers(const nsStyleImageLayers &aSource); nsStyleImageLayers(const nsStyleImageLayers &aSource);