Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsStyleAutoArray_h_
7 : #define nsStyleAutoArray_h_
8 :
9 : #include "nsTArray.h"
10 : #include "mozilla/Assertions.h"
11 :
12 : /**
13 : * An array of objects, similar to AutoTArray<T,1> but which is memmovable. It
14 : * always has length >= 1.
15 : */
16 : template<typename T>
17 929 : class nsStyleAutoArray
18 : {
19 : public:
20 : // This constructor places a single element in mFirstElement.
21 : enum WithSingleInitialElement { WITH_SINGLE_INITIAL_ELEMENT };
22 389 : explicit nsStyleAutoArray(WithSingleInitialElement) {}
23 1431 : nsStyleAutoArray(const nsStyleAutoArray& aOther) { *this = aOther; }
24 1431 : nsStyleAutoArray& operator=(const nsStyleAutoArray& aOther) {
25 1431 : mFirstElement = aOther.mFirstElement;
26 1431 : mOtherElements = aOther.mOtherElements;
27 1431 : return *this;
28 : }
29 :
30 688 : bool operator==(const nsStyleAutoArray& aOther) const {
31 1376 : return Length() == aOther.Length() &&
32 1372 : mFirstElement == aOther.mFirstElement &&
33 1372 : mOtherElements == aOther.mOtherElements;
34 : }
35 688 : bool operator!=(const nsStyleAutoArray& aOther) const {
36 688 : return !(*this == aOther);
37 : }
38 :
39 0 : nsStyleAutoArray& operator=(nsStyleAutoArray&& aOther) {
40 0 : mFirstElement = aOther.mFirstElement;
41 0 : mOtherElements.SwapElements(aOther.mOtherElements);
42 :
43 0 : return *this;
44 : }
45 :
46 2071 : size_t Length() const {
47 2071 : return mOtherElements.Length() + 1;
48 : }
49 23056 : const T& operator[](size_t aIndex) const {
50 23056 : return aIndex == 0 ? mFirstElement : mOtherElements[aIndex - 1];
51 : }
52 2238 : T& operator[](size_t aIndex) {
53 2238 : return aIndex == 0 ? mFirstElement : mOtherElements[aIndex - 1];
54 : }
55 :
56 164 : void EnsureLengthAtLeast(size_t aMinLen) {
57 164 : if (aMinLen > 0) {
58 164 : mOtherElements.EnsureLengthAtLeast(aMinLen - 1);
59 : }
60 164 : }
61 :
62 1342 : void SetLengthNonZero(size_t aNewLen) {
63 1342 : MOZ_ASSERT(aNewLen > 0);
64 1342 : mOtherElements.SetLength(aNewLen - 1);
65 1342 : }
66 :
67 45 : void TruncateLengthNonZero(size_t aNewLen) {
68 45 : MOZ_ASSERT(aNewLen > 0);
69 45 : MOZ_ASSERT(aNewLen <= Length());
70 45 : mOtherElements.TruncateLength(aNewLen - 1);
71 45 : }
72 :
73 : private:
74 : T mFirstElement;
75 : nsTArray<T> mOtherElements;
76 : };
77 :
78 : #endif /* nsStyleAutoArray_h_ */
|