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