Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing 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/. */
6 /* computed CSS Variable values */
8 #include "CSSVariableValues.h"
10 #include "CSSVariableResolver.h"
12 namespace mozilla {
14 CSSVariableValues::CSSVariableValues()
15 {
16 MOZ_COUNT_CTOR(CSSVariableValues);
17 }
19 CSSVariableValues::CSSVariableValues(const CSSVariableValues& aOther)
20 {
21 MOZ_COUNT_CTOR(CSSVariableValues);
22 CopyVariablesFrom(aOther);
23 }
25 #ifdef DEBUG
26 CSSVariableValues::~CSSVariableValues()
27 {
28 MOZ_COUNT_DTOR(CSSVariableValues);
29 }
30 #endif
32 CSSVariableValues&
33 CSSVariableValues::operator=(const CSSVariableValues& aOther)
34 {
35 if (this == &aOther) {
36 return *this;
37 }
39 mVariableIDs.Clear();
40 mVariables.Clear();
41 CopyVariablesFrom(aOther);
42 return *this;
43 }
45 bool
46 CSSVariableValues::operator==(const CSSVariableValues& aOther) const
47 {
48 if (mVariables.Length() != aOther.mVariables.Length()) {
49 return false;
50 }
52 for (size_t thisIndex = 0; thisIndex < mVariables.Length(); ++thisIndex) {
53 size_t otherIndex;
54 if (!aOther.mVariableIDs.Get(mVariables[thisIndex].mVariableName,
55 &otherIndex)) {
56 return false;
57 }
58 const nsString& otherValue = aOther.mVariables[otherIndex].mValue;
59 if (!mVariables[thisIndex].mValue.Equals(otherValue)) {
60 return false;
61 }
62 }
64 return true;
65 }
67 size_t
68 CSSVariableValues::Count() const
69 {
70 return mVariables.Length();
71 }
73 bool
74 CSSVariableValues::Get(const nsAString& aName, nsString& aValue) const
75 {
76 size_t id;
77 if (!mVariableIDs.Get(aName, &id)) {
78 return false;
79 }
80 aValue = mVariables[id].mValue;
81 return true;
82 }
84 bool
85 CSSVariableValues::Get(const nsAString& aName,
86 nsString& aValue,
87 nsCSSTokenSerializationType& aFirstToken,
88 nsCSSTokenSerializationType& aLastToken) const
89 {
90 size_t id;
91 if (!mVariableIDs.Get(aName, &id)) {
92 return false;
93 }
94 aValue = mVariables[id].mValue;
95 aFirstToken = mVariables[id].mFirstToken;
96 aLastToken = mVariables[id].mLastToken;
97 return true;
98 }
100 void
101 CSSVariableValues::GetVariableAt(size_t aIndex, nsAString& aName) const
102 {
103 aName = mVariables[aIndex].mVariableName;
104 }
106 void
107 CSSVariableValues::Put(const nsAString& aName,
108 nsString aValue,
109 nsCSSTokenSerializationType aFirstToken,
110 nsCSSTokenSerializationType aLastToken)
111 {
112 size_t id;
113 if (mVariableIDs.Get(aName, &id)) {
114 mVariables[id].mValue = aValue;
115 mVariables[id].mFirstToken = aFirstToken;
116 mVariables[id].mLastToken = aLastToken;
117 } else {
118 id = mVariables.Length();
119 mVariableIDs.Put(aName, id);
120 mVariables.AppendElement(Variable(aName, aValue, aFirstToken, aLastToken));
121 }
122 }
124 void
125 CSSVariableValues::CopyVariablesFrom(const CSSVariableValues& aOther)
126 {
127 for (size_t i = 0, n = aOther.mVariables.Length(); i < n; i++) {
128 Put(aOther.mVariables[i].mVariableName,
129 aOther.mVariables[i].mValue,
130 aOther.mVariables[i].mFirstToken,
131 aOther.mVariables[i].mLastToken);
132 }
133 }
135 void
136 CSSVariableValues::AddVariablesToResolver(CSSVariableResolver* aResolver) const
137 {
138 for (size_t i = 0, n = mVariables.Length(); i < n; i++) {
139 aResolver->Put(mVariables[i].mVariableName,
140 mVariables[i].mValue,
141 mVariables[i].mFirstToken,
142 mVariables[i].mLastToken,
143 true);
144 }
145 }
147 } // namespace mozilla