Bug 1684463 - [devtools] Part 1: Shorten the _createAttribute function by refactoring...
[gecko.git] / layout / style / nsStyleAutoArray.h
blob368b7c6a7bc3b651b75c8aea725b7eff13c2d601
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsStyleAutoArray_h_
8 #define nsStyleAutoArray_h_
10 #include "nsTArray.h"
11 #include "mozilla/Assertions.h"
13 /**
14 * An array of objects, similar to AutoTArray<T,1> but which is memmovable. It
15 * always has length >= 1.
17 template <typename T>
18 class nsStyleAutoArray {
19 public:
20 // This constructor places a single element in mFirstElement.
21 enum WithSingleInitialElement { WITH_SINGLE_INITIAL_ELEMENT };
22 explicit nsStyleAutoArray(WithSingleInitialElement) {}
24 nsStyleAutoArray(const nsStyleAutoArray&) = delete;
25 nsStyleAutoArray& operator=(const nsStyleAutoArray&) = delete;
27 nsStyleAutoArray(nsStyleAutoArray&&) = default;
28 nsStyleAutoArray& operator=(nsStyleAutoArray&&) = default;
30 bool Assign(const nsStyleAutoArray& aOther, mozilla::fallible_t) {
31 mFirstElement = aOther.mFirstElement;
32 return mOtherElements.Assign(aOther.mOtherElements, mozilla::fallible);
35 nsStyleAutoArray Clone() const {
36 nsStyleAutoArray res(WITH_SINGLE_INITIAL_ELEMENT);
37 res.mFirstElement = mFirstElement;
38 res.mOtherElements = mOtherElements.Clone();
39 return res;
42 bool operator==(const nsStyleAutoArray& aOther) const {
43 return Length() == aOther.Length() &&
44 mFirstElement == aOther.mFirstElement &&
45 mOtherElements == aOther.mOtherElements;
47 bool operator!=(const nsStyleAutoArray& aOther) const {
48 return !(*this == aOther);
51 size_t Length() const { return mOtherElements.Length() + 1; }
52 const T& operator[](size_t aIndex) const {
53 return aIndex == 0 ? mFirstElement : mOtherElements[aIndex - 1];
55 T& operator[](size_t aIndex) {
56 return aIndex == 0 ? mFirstElement : mOtherElements[aIndex - 1];
59 void EnsureLengthAtLeast(size_t aMinLen) {
60 if (aMinLen > 0) {
61 mOtherElements.EnsureLengthAtLeast(aMinLen - 1);
65 void SetLengthNonZero(size_t aNewLen) {
66 MOZ_ASSERT(aNewLen > 0);
67 mOtherElements.SetLength(aNewLen - 1);
70 void TruncateLengthNonZero(size_t aNewLen) {
71 MOZ_ASSERT(aNewLen > 0);
72 MOZ_ASSERT(aNewLen <= Length());
73 mOtherElements.TruncateLength(aNewLen - 1);
76 private:
77 T mFirstElement;
78 nsTArray<T> mOtherElements;
81 #endif /* nsStyleAutoArray_h_ */