DOMSVGNumber.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_DOMSVGNUMBER_H__
  6. #define MOZILLA_DOMSVGNUMBER_H__
  7. #include "DOMSVGNumberList.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsTArray.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/ErrorResult.h"
  12. #include "nsWrapperCache.h"
  13. class nsSVGElement;
  14. #define MOZ_SVG_LIST_INDEX_BIT_COUNT 27 // supports > 134 million list items
  15. namespace mozilla {
  16. /**
  17. * Class DOMSVGNumber
  18. *
  19. * This class creates the DOM objects that wrap internal SVGNumber objects that
  20. * are in an SVGNumberList. It is also used to create the objects returned by
  21. * SVGSVGElement.createSVGNumber().
  22. *
  23. * For the DOM wrapper classes for non-list SVGNumber, see nsSVGNumber2.h.
  24. *
  25. * See the architecture comment in DOMSVGAnimatedNumberList.h.
  26. *
  27. * See the comment in DOMSVGLength.h (yes, LENGTH), which applies here too.
  28. */
  29. class DOMSVGNumber final : public nsISupports
  30. , public nsWrapperCache
  31. {
  32. friend class AutoChangeNumberNotifier;
  33. ~DOMSVGNumber() {
  34. // Our mList's weak ref to us must be nulled out when we die. If GC has
  35. // unlinked us using the cycle collector code, then that has already
  36. // happened, and mList is null.
  37. if (mList) {
  38. mList->mItems[mListIndex] = nullptr;
  39. }
  40. }
  41. public:
  42. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  43. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGNumber)
  44. /**
  45. * Generic ctor for DOMSVGNumber objects that are created for an attribute.
  46. */
  47. DOMSVGNumber(DOMSVGNumberList *aList,
  48. uint8_t aAttrEnum,
  49. uint32_t aListIndex,
  50. bool aIsAnimValItem);
  51. /**
  52. * Ctor for creating the objects returned by SVGSVGElement.createSVGNumber(),
  53. * which do not initially belong to an attribute.
  54. */
  55. explicit DOMSVGNumber(nsISupports* aParent);
  56. /**
  57. * Create an unowned copy. The caller is responsible for the first AddRef().
  58. */
  59. DOMSVGNumber* Clone() {
  60. DOMSVGNumber *clone = new DOMSVGNumber(mParent);
  61. clone->mValue = ToSVGNumber();
  62. return clone;
  63. }
  64. bool IsInList() const {
  65. return !!mList;
  66. }
  67. /**
  68. * In future, if this class is used for non-list numbers, this will be
  69. * different to IsInList().
  70. */
  71. bool HasOwner() const {
  72. return !!mList;
  73. }
  74. /**
  75. * This method is called to notify this DOM object that it is being inserted
  76. * into a list, and give it the information it needs as a result.
  77. *
  78. * This object MUST NOT already belong to a list when this method is called.
  79. * That's not to say that script can't move these DOM objects between
  80. * lists - it can - it's just that the logic to handle that (and send out
  81. * the necessary notifications) is located elsewhere (in DOMSVGNumberList).)
  82. */
  83. void InsertingIntoList(DOMSVGNumberList *aList,
  84. uint8_t aAttrEnum,
  85. uint32_t aListIndex,
  86. bool aIsAnimValItem);
  87. static uint32_t MaxListIndex() {
  88. return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1;
  89. }
  90. /// This method is called to notify this object that its list index changed.
  91. void UpdateListIndex(uint32_t aListIndex) {
  92. mListIndex = aListIndex;
  93. }
  94. /**
  95. * This method is called to notify this DOM object that it is about to be
  96. * removed from its current DOM list so that it can first make a copy of its
  97. * internal counterpart's value. (If it didn't do this, then it would
  98. * "lose" its value on being removed.)
  99. */
  100. void RemovingFromList();
  101. float ToSVGNumber();
  102. nsISupports* GetParentObject()
  103. {
  104. return mParent;
  105. }
  106. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  107. static already_AddRefed<DOMSVGNumber>
  108. Constructor(const dom::GlobalObject& aGlobal, ErrorResult& aRv);
  109. static already_AddRefed<DOMSVGNumber>
  110. Constructor(const dom::GlobalObject& aGlobal, float aValue, ErrorResult& aRv);
  111. float Value();
  112. void SetValue(float aValue, ErrorResult& aRv);
  113. private:
  114. nsSVGElement* Element() {
  115. return mList->Element();
  116. }
  117. uint8_t AttrEnum() const {
  118. return mAttrEnum;
  119. }
  120. /**
  121. * Get a reference to the internal SVGNumber list item that this DOM wrapper
  122. * object currently wraps.
  123. *
  124. * To simplify the code we just have this one method for obtaining both
  125. * baseVal and animVal internal items. This means that animVal items don't
  126. * get const protection, but then our setter methods guard against changing
  127. * animVal items.
  128. */
  129. float& InternalItem();
  130. #ifdef DEBUG
  131. bool IndexIsValid();
  132. #endif
  133. RefPtr<DOMSVGNumberList> mList;
  134. nsCOMPtr<nsISupports> mParent;
  135. // Bounds for the following are checked in the ctor, so be sure to update
  136. // that if you change the capacity of any of the following.
  137. uint32_t mListIndex:MOZ_SVG_LIST_INDEX_BIT_COUNT;
  138. uint32_t mAttrEnum:4; // supports up to 16 attributes
  139. uint32_t mIsAnimValItem:1;
  140. // The following member is only used when we're not in a list:
  141. float mValue;
  142. };
  143. } // namespace mozilla
  144. #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
  145. #endif // MOZILLA_DOMSVGNUMBER_H__