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