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
18 : * Robert Longson <longsonr@gmail.com>
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef MOZILLA_SVGSTRINGLIST_H__
39 : #define MOZILLA_SVGSTRINGLIST_H__
40 :
41 : #include "nsDebug.h"
42 : #include "nsTArray.h"
43 : #include "nsString.h" // IWYU pragma: keep
44 :
45 : namespace mozilla {
46 :
47 : /**
48 : *
49 : * The DOM wrapper class for this class is DOMSVGStringList.
50 : */
51 : class SVGStringList
52 : {
53 : friend class DOMSVGStringList;
54 :
55 : public:
56 :
57 0 : SVGStringList() : mIsSet(false), mIsCommaSeparated(false) {}
58 0 : ~SVGStringList(){}
59 :
60 0 : void SetIsCommaSeparated(bool aIsCommaSeparated) {
61 0 : mIsCommaSeparated = aIsCommaSeparated;
62 0 : }
63 : nsresult SetValue(const nsAString& aValue);
64 :
65 0 : void Clear() {
66 0 : mStrings.Clear();
67 0 : mIsSet = false;
68 0 : }
69 :
70 : /// This may return an incomplete string on OOM, but that's acceptable.
71 : void GetValue(nsAString& aValue) const;
72 :
73 0 : bool IsEmpty() const {
74 0 : return mStrings.IsEmpty();
75 : }
76 :
77 0 : PRUint32 Length() const {
78 0 : return mStrings.Length();
79 : }
80 :
81 0 : const nsAString& operator[](PRUint32 aIndex) const {
82 0 : return mStrings[aIndex];
83 : }
84 :
85 : bool operator==(const SVGStringList& rhs) const {
86 : return mStrings == rhs.mStrings;
87 : }
88 :
89 0 : bool SetCapacity(PRUint32 size) {
90 0 : return mStrings.SetCapacity(size);
91 : }
92 :
93 : void Compact() {
94 : mStrings.Compact();
95 : }
96 :
97 : // Returns true if the value of this stringlist has been explicitly
98 : // set by markup or a DOM call, false otherwise.
99 0 : bool IsExplicitlySet() const
100 0 : { return mIsSet; }
101 :
102 : // Access to methods that can modify objects of this type is deliberately
103 : // limited. This is to reduce the chances of someone modifying objects of
104 : // this type without taking the necessary steps to keep DOM wrappers in sync.
105 : // If you need wider access to these methods, consider adding a method to
106 : // SVGAnimatedStringList and having that class act as an intermediary so it
107 : // can take care of keeping DOM wrappers in sync.
108 :
109 : protected:
110 :
111 : /**
112 : * This may fail on OOM if the internal capacity needs to be increased, in
113 : * which case the list will be left unmodified.
114 : */
115 : nsresult CopyFrom(const SVGStringList& rhs);
116 :
117 0 : nsAString& operator[](PRUint32 aIndex) {
118 0 : return mStrings[aIndex];
119 : }
120 :
121 : /**
122 : * This may fail (return false) on OOM if the internal capacity is being
123 : * increased, in which case the list will be left unmodified.
124 : */
125 : bool SetLength(PRUint32 aStringOfItems) {
126 : return mStrings.SetLength(aStringOfItems);
127 : }
128 :
129 : private:
130 :
131 : // Marking the following private only serves to show which methods are only
132 : // used by our friend classes (as opposed to our subclasses) - it doesn't
133 : // really provide additional safety.
134 :
135 0 : bool InsertItem(PRUint32 aIndex, const nsAString &aString) {
136 0 : if (aIndex >= mStrings.Length()) {
137 0 : aIndex = mStrings.Length();
138 : }
139 0 : if (mStrings.InsertElementAt(aIndex, aString)) {
140 0 : mIsSet = true;
141 0 : return true;
142 : }
143 0 : return false;
144 : }
145 :
146 0 : void ReplaceItem(PRUint32 aIndex, const nsAString &aString) {
147 0 : NS_ABORT_IF_FALSE(aIndex < mStrings.Length(),
148 : "DOM wrapper caller should have raised INDEX_SIZE_ERR");
149 0 : mStrings[aIndex] = aString;
150 0 : }
151 :
152 0 : void RemoveItem(PRUint32 aIndex) {
153 0 : NS_ABORT_IF_FALSE(aIndex < mStrings.Length(),
154 : "DOM wrapper caller should have raised INDEX_SIZE_ERR");
155 0 : mStrings.RemoveElementAt(aIndex);
156 0 : }
157 :
158 0 : bool AppendItem(const nsAString &aString) {
159 0 : if (mStrings.AppendElement(aString)) {
160 0 : mIsSet = true;
161 0 : return true;
162 : }
163 0 : return false;
164 : }
165 :
166 : protected:
167 :
168 : /* See SVGLengthList for the rationale for using nsTArray<float> instead
169 : * of nsTArray<float, 1>.
170 : */
171 : nsTArray<nsString> mStrings;
172 : bool mIsSet;
173 : bool mIsCommaSeparated;
174 : };
175 :
176 : } // namespace mozilla
177 :
178 : #endif // MOZILLA_SVGSTRINGLIST_H__
|