1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla SVG Project code.
16 : *
17 : * The Initial Developer of the Original Code is the Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #ifndef MOZILLA_SVGNUMBERLIST_H__
38 : #define MOZILLA_SVGNUMBERLIST_H__
39 :
40 : #include "nsCOMPtr.h"
41 : #include "nsDebug.h"
42 : #include "nsIContent.h"
43 : #include "nsINode.h"
44 : #include "nsIWeakReferenceUtils.h"
45 : #include "nsSVGElement.h"
46 : #include "nsTArray.h"
47 :
48 : namespace mozilla {
49 :
50 : /**
51 : * ATTENTION! WARNING! WATCH OUT!!
52 : *
53 : * Consumers that modify objects of this type absolutely MUST keep the DOM
54 : * wrappers for those lists (if any) in sync!! That's why this class is so
55 : * locked down.
56 : *
57 : * The DOM wrapper class for this class is DOMSVGNumberList.
58 : */
59 : class SVGNumberList
60 : {
61 : friend class SVGAnimatedNumberList;
62 : friend class DOMSVGNumberList;
63 : friend class DOMSVGNumber;
64 :
65 : public:
66 :
67 0 : SVGNumberList(){}
68 0 : ~SVGNumberList(){}
69 :
70 : // Only methods that don't make/permit modification to this list are public.
71 : // Only our friend classes can access methods that may change us.
72 :
73 : /// This may return an incomplete string on OOM, but that's acceptable.
74 : void GetValueAsString(nsAString& aValue) const;
75 :
76 : bool IsEmpty() const {
77 : return mNumbers.IsEmpty();
78 : }
79 :
80 0 : PRUint32 Length() const {
81 0 : return mNumbers.Length();
82 : }
83 :
84 0 : const float& operator[](PRUint32 aIndex) const {
85 0 : return mNumbers[aIndex];
86 : }
87 :
88 0 : bool operator==(const SVGNumberList& rhs) const {
89 0 : return mNumbers == rhs.mNumbers;
90 : }
91 :
92 0 : bool SetCapacity(PRUint32 size) {
93 0 : return mNumbers.SetCapacity(size);
94 : }
95 :
96 : void Compact() {
97 : mNumbers.Compact();
98 : }
99 :
100 : // Access to methods that can modify objects of this type is deliberately
101 : // limited. This is to reduce the chances of someone modifying objects of
102 : // this type without taking the necessary steps to keep DOM wrappers in sync.
103 : // If you need wider access to these methods, consider adding a method to
104 : // SVGAnimatedNumberList and having that class act as an intermediary so it
105 : // can take care of keeping DOM wrappers in sync.
106 :
107 : protected:
108 :
109 : /**
110 : * This may fail on OOM if the internal capacity needs to be increased, in
111 : * which case the list will be left unmodified.
112 : */
113 : nsresult CopyFrom(const SVGNumberList& rhs);
114 :
115 0 : float& operator[](PRUint32 aIndex) {
116 0 : return mNumbers[aIndex];
117 : }
118 :
119 : /**
120 : * This may fail (return false) on OOM if the internal capacity is being
121 : * increased, in which case the list will be left unmodified.
122 : */
123 0 : bool SetLength(PRUint32 aNumberOfItems) {
124 0 : return mNumbers.SetLength(aNumberOfItems);
125 : }
126 :
127 : private:
128 :
129 : // Marking the following private only serves to show which methods are only
130 : // used by our friend classes (as opposed to our subclasses) - it doesn't
131 : // really provide additional safety.
132 :
133 : nsresult SetValueFromString(const nsAString& aValue);
134 :
135 0 : void Clear() {
136 0 : mNumbers.Clear();
137 0 : }
138 :
139 0 : bool InsertItem(PRUint32 aIndex, const float &aNumber) {
140 0 : if (aIndex >= mNumbers.Length()) {
141 0 : aIndex = mNumbers.Length();
142 : }
143 0 : return !!mNumbers.InsertElementAt(aIndex, aNumber);
144 : }
145 :
146 : void ReplaceItem(PRUint32 aIndex, const float &aNumber) {
147 : NS_ABORT_IF_FALSE(aIndex < mNumbers.Length(),
148 : "DOM wrapper caller should have raised INDEX_SIZE_ERR");
149 : mNumbers[aIndex] = aNumber;
150 : }
151 :
152 0 : void RemoveItem(PRUint32 aIndex) {
153 0 : NS_ABORT_IF_FALSE(aIndex < mNumbers.Length(),
154 : "DOM wrapper caller should have raised INDEX_SIZE_ERR");
155 0 : mNumbers.RemoveElementAt(aIndex);
156 0 : }
157 :
158 0 : bool AppendItem(float aNumber) {
159 0 : return !!mNumbers.AppendElement(aNumber);
160 : }
161 :
162 : protected:
163 :
164 : /* See SVGLengthList for the rationale for using nsTArray<float> instead
165 : * of nsTArray<float, 1>.
166 : */
167 : nsTArray<float> mNumbers;
168 : };
169 :
170 :
171 : /**
172 : * This SVGNumberList subclass is used by the SMIL code when a number list
173 : * is to be stored in an nsISMILValue instance. Since nsISMILValue objects may
174 : * be cached, it is necessary for us to hold a strong reference to our element
175 : * so that it doesn't disappear out from under us if, say, the element is
176 : * removed from the DOM tree.
177 : */
178 : class SVGNumberListAndInfo : public SVGNumberList
179 0 : {
180 : public:
181 :
182 0 : SVGNumberListAndInfo()
183 0 : : mElement(nsnull)
184 0 : {}
185 :
186 : SVGNumberListAndInfo(nsSVGElement *aElement)
187 : : mElement(do_GetWeakReference(static_cast<nsINode*>(aElement)))
188 : {}
189 :
190 0 : void SetInfo(nsSVGElement *aElement) {
191 0 : mElement = do_GetWeakReference(static_cast<nsINode*>(aElement));
192 0 : }
193 :
194 0 : nsSVGElement* Element() const {
195 0 : nsCOMPtr<nsIContent> e = do_QueryReferent(mElement);
196 0 : return static_cast<nsSVGElement*>(e.get());
197 : }
198 :
199 0 : nsresult CopyFrom(const SVGNumberListAndInfo& rhs) {
200 0 : mElement = rhs.mElement;
201 0 : return SVGNumberList::CopyFrom(rhs);
202 : }
203 :
204 : // Instances of this special subclass do not have DOM wrappers that we need
205 : // to worry about keeping in sync, so it's safe to expose any hidden base
206 : // class methods required by the SMIL code, as we do below.
207 :
208 : /**
209 : * Exposed so that SVGNumberList baseVals can be copied to
210 : * SVGNumberListAndInfo objects. Note that callers should also call
211 : * SetInfo() when using this method!
212 : */
213 0 : nsresult CopyFrom(const SVGNumberList& rhs) {
214 0 : return SVGNumberList::CopyFrom(rhs);
215 : }
216 0 : const float& operator[](PRUint32 aIndex) const {
217 0 : return SVGNumberList::operator[](aIndex);
218 : }
219 0 : float& operator[](PRUint32 aIndex) {
220 0 : return SVGNumberList::operator[](aIndex);
221 : }
222 0 : bool SetLength(PRUint32 aNumberOfItems) {
223 0 : return SVGNumberList::SetLength(aNumberOfItems);
224 : }
225 :
226 : private:
227 : // We must keep a weak reference to our element because we may belong to a
228 : // cached baseVal nsSMILValue. See the comments starting at:
229 : // https://bugzilla.mozilla.org/show_bug.cgi?id=515116#c15
230 : // See also https://bugzilla.mozilla.org/show_bug.cgi?id=653497
231 : nsWeakPtr mElement;
232 : };
233 :
234 : } // namespace mozilla
235 :
236 : #endif // MOZILLA_SVGNUMBERLIST_H__
|