SVGStringList.h 3.8 KB

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