2 * Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #ifndef QualifiedName_h
22 #define QualifiedName_h
24 #include "wtf/HashTableDeletedValueType.h"
25 #include "wtf/HashTraits.h"
26 #include "wtf/RefCounted.h"
27 #include "wtf/text/AtomicString.h"
31 struct QualifiedNameComponents {
33 StringImpl* m_localName;
34 StringImpl* m_namespace;
37 // This struct is used to pass data between QualifiedName and the QNameTranslator.
38 // For hashing and equality only the QualifiedNameComponents fields are used.
39 struct QualifiedNameData {
40 QualifiedNameComponents m_components;
45 WTF_MAKE_FAST_ALLOCATED;
47 class QualifiedNameImpl : public RefCounted<QualifiedNameImpl> {
49 static PassRefPtr<QualifiedNameImpl> create(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI, bool isStatic)
51 return adoptRef(new QualifiedNameImpl(prefix, localName, namespaceURI, isStatic));
56 unsigned computeHash() const;
58 bool hasOneRef() const
60 return m_isStatic || RefCounted<QualifiedNameImpl>::hasOneRef();
67 RefCounted<QualifiedNameImpl>::ref();
74 RefCounted<QualifiedNameImpl>::deref();
77 const AtomicString m_prefix;
78 const AtomicString m_localName;
79 const AtomicString m_namespace;
80 mutable AtomicString m_localNameUpper;
81 // We rely on StringHasher's hashMemory clearing out the top 8 bits when
82 // doing hashing and use one of the bits for the m_isStatic value.
83 mutable unsigned m_existingHash : 24;
84 unsigned m_isStatic : 1;
87 QualifiedNameImpl(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI, bool isStatic)
89 , m_localName(localName)
90 , m_namespace(namespaceURI)
92 , m_isStatic(isStatic)
95 ASSERT(!namespaceURI.isEmpty() || namespaceURI.isNull());
99 QualifiedName(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI);
102 QualifiedName(const QualifiedName& other) : m_impl(other.m_impl) { }
103 const QualifiedName& operator=(const QualifiedName& other) { m_impl = other.m_impl; return *this; }
105 // Hash table deleted values, which are only constructed and never copied or destroyed.
106 QualifiedName(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
107 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
109 bool operator==(const QualifiedName& other) const { return m_impl == other.m_impl; }
110 bool operator!=(const QualifiedName& other) const { return !(*this == other); }
112 bool matches(const QualifiedName& other) const { return m_impl == other.m_impl || (localName() == other.localName() && namespaceURI() == other.namespaceURI()); }
114 bool matchesPossiblyIgnoringCase(const QualifiedName& other, bool shouldIgnoreCase) const { return m_impl == other.m_impl || (equalPossiblyIgnoringCase(localName(), other.localName(), shouldIgnoreCase) && namespaceURI() == other.namespaceURI()); }
116 bool hasPrefix() const { return m_impl->m_prefix != nullAtom; }
117 void setPrefix(const AtomicString& prefix) { *this = QualifiedName(prefix, localName(), namespaceURI()); }
119 const AtomicString& prefix() const { return m_impl->m_prefix; }
120 const AtomicString& localName() const { return m_impl->m_localName; }
121 const AtomicString& namespaceURI() const { return m_impl->m_namespace; }
123 // Uppercased localName, cached for efficiency
124 const AtomicString& localNameUpper() const;
126 String toString() const;
128 QualifiedNameImpl* impl() const { return m_impl.get(); }
130 // Init routine for globals
133 static const QualifiedName& null();
135 // The below methods are only for creating static global QNames that need no ref counting.
136 static void createStatic(void* targetAddress, StringImpl* name);
137 static void createStatic(void* targetAddress, StringImpl* name, const AtomicString& nameNamespace);
140 // This constructor is used only to create global/static QNames that don't require any ref counting.
141 QualifiedName(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI, bool isStatic);
143 RefPtr<QualifiedNameImpl> m_impl;
146 extern const QualifiedName& anyName;
147 inline const QualifiedName& anyQName() { return anyName; }
149 inline bool operator==(const AtomicString& a, const QualifiedName& q) { return a == q.localName(); }
150 inline bool operator!=(const AtomicString& a, const QualifiedName& q) { return a != q.localName(); }
151 inline bool operator==(const QualifiedName& q, const AtomicString& a) { return a == q.localName(); }
152 inline bool operator!=(const QualifiedName& q, const AtomicString& a) { return a != q.localName(); }
154 inline unsigned hashComponents(const QualifiedNameComponents& buf)
156 return StringHasher::hashMemory<sizeof(QualifiedNameComponents)>(&buf);
159 struct QualifiedNameHash {
160 static unsigned hash(const QualifiedName& name) { return hash(name.impl()); }
162 static unsigned hash(const QualifiedName::QualifiedNameImpl* name)
164 if (!name->m_existingHash)
165 name->m_existingHash = name->computeHash();
166 return name->m_existingHash;
169 static bool equal(const QualifiedName& a, const QualifiedName& b) { return a == b; }
170 static bool equal(const QualifiedName::QualifiedNameImpl* a, const QualifiedName::QualifiedNameImpl* b) { return a == b; }
172 static const bool safeToCompareToEmptyOrDeleted = false;
179 template<typename T> struct DefaultHash;
181 template<> struct DefaultHash<blink::QualifiedName> {
182 typedef blink::QualifiedNameHash Hash;
185 template<> struct HashTraits<blink::QualifiedName> : SimpleClassHashTraits<blink::QualifiedName> {
186 static const bool emptyValueIsZero = false;
187 static blink::QualifiedName emptyValue() { return blink::QualifiedName::null(); }