SVGStringList.h (4011B)
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/. */ 6 7 #ifndef DOM_SVG_SVGSTRINGLIST_H_ 8 #define DOM_SVG_SVGSTRINGLIST_H_ 9 10 #include "nsDebug.h" 11 #include "nsString.h" // IWYU pragma: keep 12 #include "nsTArray.h" 13 14 namespace mozilla { 15 16 namespace dom { 17 class DOMSVGStringList; 18 } 19 20 /** 21 * 22 * The DOM wrapper class for this class is DOMSVGStringList. 23 */ 24 class SVGStringList { 25 friend class dom::DOMSVGStringList; 26 27 public: 28 SVGStringList() : mIsSet(false), mIsCommaSeparated(false) {} 29 ~SVGStringList() = default; 30 31 void SetIsCommaSeparated(bool aIsCommaSeparated) { 32 mIsCommaSeparated = aIsCommaSeparated; 33 } 34 nsresult SetValue(const nsAString& aValue); 35 36 void Clear() { 37 mStrings.Clear(); 38 mIsSet = false; 39 } 40 41 /// This may return an incomplete string on OOM, but that's acceptable. 42 void GetValue(nsAString& aValue) const; 43 44 bool IsEmpty() const { return mStrings.IsEmpty(); } 45 46 uint32_t Length() const { return mStrings.Length(); } 47 48 const nsAString& operator[](uint32_t aIndex) const { 49 return mStrings[aIndex]; 50 } 51 52 bool operator==(const SVGStringList& rhs) const { 53 return mStrings == rhs.mStrings; 54 } 55 56 bool SetCapacity(uint32_t size) { 57 return mStrings.SetCapacity(size, fallible); 58 } 59 60 void Compact() { mStrings.Compact(); } 61 62 // Returns true if the value of this stringlist has been explicitly 63 // set by markup or a DOM call, false otherwise. 64 bool IsExplicitlySet() const { return mIsSet; } 65 66 // Access to methods that can modify objects of this type is deliberately 67 // limited. This is to reduce the chances of someone modifying objects of 68 // this type without taking the necessary steps to keep DOM wrappers in sync. 69 // If you need wider access to these methods, consider adding a method to 70 // DOMSVGAnimatedStringList and having that class act as an intermediary so it 71 // can take care of keeping DOM wrappers in sync. 72 73 protected: 74 /** 75 * This may fail on OOM if the internal capacity needs to be increased, in 76 * which case the list will be left unmodified. 77 */ 78 nsresult CopyFrom(const SVGStringList& rhs); 79 80 nsAString& operator[](uint32_t aIndex) { return mStrings[aIndex]; } 81 82 /** 83 * This may fail (return false) on OOM if the internal capacity is being 84 * increased, in which case the list will be left unmodified. 85 */ 86 bool SetLength(uint32_t aStringOfItems) { 87 return mStrings.SetLength(aStringOfItems, fallible); 88 } 89 90 private: 91 // Marking the following private only serves to show which methods are only 92 // used by our friend classes (as opposed to our subclasses) - it doesn't 93 // really provide additional safety. 94 95 bool InsertItem(uint32_t aIndex, const nsAString& aString) { 96 if (aIndex >= mStrings.Length()) { 97 aIndex = mStrings.Length(); 98 } 99 if (mStrings.InsertElementAt(aIndex, aString, fallible)) { 100 mIsSet = true; 101 return true; 102 } 103 return false; 104 } 105 106 void ReplaceItem(uint32_t aIndex, const nsAString& aString) { 107 MOZ_ASSERT(aIndex < mStrings.Length(), 108 "DOM wrapper caller should have raised INDEX_SIZE_ERR"); 109 mStrings[aIndex] = aString; 110 } 111 112 void RemoveItem(uint32_t aIndex) { 113 MOZ_ASSERT(aIndex < mStrings.Length(), 114 "DOM wrapper caller should have raised INDEX_SIZE_ERR"); 115 mStrings.RemoveElementAt(aIndex); 116 } 117 118 bool AppendItem(const nsAString& aString) { 119 if (mStrings.AppendElement(aString, fallible)) { 120 mIsSet = true; 121 return true; 122 } 123 return false; 124 } 125 126 protected: 127 /* See SVGLengthList for the rationale for using FallibleTArray<float> instead 128 * of FallibleTArray<float, 1>. 129 */ 130 FallibleTArray<nsString> mStrings; 131 bool mIsSet; 132 bool mIsCommaSeparated; 133 }; 134 135 } // namespace mozilla 136 137 #endif // DOM_SVG_SVGSTRINGLIST_H_