Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Class that represents the name (nodeinfo or atom) of an attribute; |
michael@0 | 8 | * using nodeinfos all the time is too slow, so we use atoms when we |
michael@0 | 9 | * can. |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef nsAttrName_h___ |
michael@0 | 13 | #define nsAttrName_h___ |
michael@0 | 14 | |
michael@0 | 15 | #include "nsINodeInfo.h" |
michael@0 | 16 | #include "nsIAtom.h" |
michael@0 | 17 | #include "nsDOMString.h" |
michael@0 | 18 | |
michael@0 | 19 | #define NS_ATTRNAME_NODEINFO_BIT 1 |
michael@0 | 20 | class nsAttrName |
michael@0 | 21 | { |
michael@0 | 22 | public: |
michael@0 | 23 | nsAttrName(const nsAttrName& aOther) |
michael@0 | 24 | : mBits(aOther.mBits) |
michael@0 | 25 | { |
michael@0 | 26 | AddRefInternalName(); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | explicit nsAttrName(nsIAtom* aAtom) |
michael@0 | 30 | : mBits(reinterpret_cast<uintptr_t>(aAtom)) |
michael@0 | 31 | { |
michael@0 | 32 | NS_ASSERTION(aAtom, "null atom-name in nsAttrName"); |
michael@0 | 33 | NS_ADDREF(aAtom); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | explicit nsAttrName(nsINodeInfo* aNodeInfo) |
michael@0 | 37 | { |
michael@0 | 38 | NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName"); |
michael@0 | 39 | if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) { |
michael@0 | 40 | mBits = reinterpret_cast<uintptr_t>(aNodeInfo->NameAtom()); |
michael@0 | 41 | NS_ADDREF(aNodeInfo->NameAtom()); |
michael@0 | 42 | } |
michael@0 | 43 | else { |
michael@0 | 44 | mBits = reinterpret_cast<uintptr_t>(aNodeInfo) | |
michael@0 | 45 | NS_ATTRNAME_NODEINFO_BIT; |
michael@0 | 46 | NS_ADDREF(aNodeInfo); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | ~nsAttrName() |
michael@0 | 51 | { |
michael@0 | 52 | ReleaseInternalName(); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | void SetTo(nsINodeInfo* aNodeInfo) |
michael@0 | 56 | { |
michael@0 | 57 | NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName"); |
michael@0 | 58 | |
michael@0 | 59 | ReleaseInternalName(); |
michael@0 | 60 | if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) { |
michael@0 | 61 | mBits = reinterpret_cast<uintptr_t>(aNodeInfo->NameAtom()); |
michael@0 | 62 | NS_ADDREF(aNodeInfo->NameAtom()); |
michael@0 | 63 | } |
michael@0 | 64 | else { |
michael@0 | 65 | mBits = reinterpret_cast<uintptr_t>(aNodeInfo) | |
michael@0 | 66 | NS_ATTRNAME_NODEINFO_BIT; |
michael@0 | 67 | NS_ADDREF(aNodeInfo); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | void SetTo(nsIAtom* aAtom) |
michael@0 | 72 | { |
michael@0 | 73 | NS_ASSERTION(aAtom, "null atom-name in nsAttrName"); |
michael@0 | 74 | |
michael@0 | 75 | ReleaseInternalName(); |
michael@0 | 76 | mBits = reinterpret_cast<uintptr_t>(aAtom); |
michael@0 | 77 | NS_ADDREF(aAtom); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool IsAtom() const |
michael@0 | 81 | { |
michael@0 | 82 | return !(mBits & NS_ATTRNAME_NODEINFO_BIT); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | nsINodeInfo* NodeInfo() const |
michael@0 | 86 | { |
michael@0 | 87 | NS_ASSERTION(!IsAtom(), "getting nodeinfo-value of atom-name"); |
michael@0 | 88 | return reinterpret_cast<nsINodeInfo*>(mBits & ~NS_ATTRNAME_NODEINFO_BIT); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | nsIAtom* Atom() const |
michael@0 | 92 | { |
michael@0 | 93 | NS_ASSERTION(IsAtom(), "getting atom-value of nodeinfo-name"); |
michael@0 | 94 | return reinterpret_cast<nsIAtom*>(mBits); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | bool Equals(const nsAttrName& aOther) const |
michael@0 | 98 | { |
michael@0 | 99 | return mBits == aOther.mBits; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | // Faster comparison in the case we know the namespace is null |
michael@0 | 103 | bool Equals(nsIAtom* aAtom) const |
michael@0 | 104 | { |
michael@0 | 105 | return reinterpret_cast<uintptr_t>(aAtom) == mBits; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | // And the same but without forcing callers to atomize |
michael@0 | 109 | bool Equals(const nsAString& aLocalName) const |
michael@0 | 110 | { |
michael@0 | 111 | return IsAtom() && Atom()->Equals(aLocalName); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | bool Equals(nsIAtom* aLocalName, int32_t aNamespaceID) const |
michael@0 | 115 | { |
michael@0 | 116 | if (aNamespaceID == kNameSpaceID_None) { |
michael@0 | 117 | return Equals(aLocalName); |
michael@0 | 118 | } |
michael@0 | 119 | return !IsAtom() && NodeInfo()->Equals(aLocalName, aNamespaceID); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | bool Equals(nsINodeInfo* aNodeInfo) const |
michael@0 | 123 | { |
michael@0 | 124 | return Equals(aNodeInfo->NameAtom(), aNodeInfo->NamespaceID()); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | int32_t NamespaceID() const |
michael@0 | 128 | { |
michael@0 | 129 | return IsAtom() ? kNameSpaceID_None : NodeInfo()->NamespaceID(); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | int32_t NamespaceEquals(int32_t aNamespaceID) const |
michael@0 | 133 | { |
michael@0 | 134 | return aNamespaceID == kNameSpaceID_None ? |
michael@0 | 135 | IsAtom() : |
michael@0 | 136 | (!IsAtom() && NodeInfo()->NamespaceEquals(aNamespaceID)); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | nsIAtom* LocalName() const |
michael@0 | 140 | { |
michael@0 | 141 | return IsAtom() ? Atom() : NodeInfo()->NameAtom(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | nsIAtom* GetPrefix() const |
michael@0 | 145 | { |
michael@0 | 146 | return IsAtom() ? nullptr : NodeInfo()->GetPrefixAtom(); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | bool QualifiedNameEquals(const nsAString& aName) const |
michael@0 | 150 | { |
michael@0 | 151 | return IsAtom() ? Atom()->Equals(aName) : |
michael@0 | 152 | NodeInfo()->QualifiedNameEquals(aName); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | void GetQualifiedName(nsAString& aStr) const |
michael@0 | 156 | { |
michael@0 | 157 | if (IsAtom()) { |
michael@0 | 158 | Atom()->ToString(aStr); |
michael@0 | 159 | } |
michael@0 | 160 | else { |
michael@0 | 161 | aStr = NodeInfo()->QualifiedName(); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 166 | void GetPrefix(nsAString& aStr) const |
michael@0 | 167 | { |
michael@0 | 168 | if (IsAtom()) { |
michael@0 | 169 | SetDOMStringToNull(aStr); |
michael@0 | 170 | } |
michael@0 | 171 | else { |
michael@0 | 172 | NodeInfo()->GetPrefix(aStr); |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | #endif |
michael@0 | 176 | |
michael@0 | 177 | uint32_t HashValue() const |
michael@0 | 178 | { |
michael@0 | 179 | // mBits and uint32_t might have different size. This should silence |
michael@0 | 180 | // any warnings or compile-errors. This is what the implementation of |
michael@0 | 181 | // NS_PTR_TO_INT32 does to take care of the same problem. |
michael@0 | 182 | return mBits - 0; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | bool IsSmaller(nsIAtom* aOther) const |
michael@0 | 186 | { |
michael@0 | 187 | return mBits < reinterpret_cast<uintptr_t>(aOther); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | private: |
michael@0 | 191 | |
michael@0 | 192 | void AddRefInternalName() |
michael@0 | 193 | { |
michael@0 | 194 | // Since both nsINodeInfo and nsIAtom inherit nsISupports as its first |
michael@0 | 195 | // interface we can safely assume that it's first in the vtable |
michael@0 | 196 | nsISupports* name = reinterpret_cast<nsISupports *> |
michael@0 | 197 | (mBits & ~NS_ATTRNAME_NODEINFO_BIT); |
michael@0 | 198 | |
michael@0 | 199 | NS_ADDREF(name); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | void ReleaseInternalName() |
michael@0 | 203 | { |
michael@0 | 204 | // Since both nsINodeInfo and nsIAtom inherit nsISupports as its first |
michael@0 | 205 | // interface we can safely assume that it's first in the vtable |
michael@0 | 206 | nsISupports* name = reinterpret_cast<nsISupports *> |
michael@0 | 207 | (mBits & ~NS_ATTRNAME_NODEINFO_BIT); |
michael@0 | 208 | |
michael@0 | 209 | NS_RELEASE(name); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | uintptr_t mBits; |
michael@0 | 213 | }; |
michael@0 | 214 | |
michael@0 | 215 | #endif |