2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 2005 Apple Computer, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #ifndef QualifiedName_h
23 #define QualifiedName_h
25 #include "AtomicString.h"
26 #include <wtf/HashFunctions.h>
30 struct QualifiedNameComponents
{
32 StringImpl
* m_localName
;
33 StringImpl
* m_namespace
;
38 class QualifiedNameImpl
: public RefCounted
<QualifiedNameImpl
> {
40 static PassRefPtr
<QualifiedNameImpl
> create(const AtomicString
& p
, const AtomicString
& l
, const AtomicString
& n
)
42 return adoptRef(new QualifiedNameImpl(p
, l
, n
));
45 AtomicString m_prefix
;
46 AtomicString m_localName
;
47 AtomicString m_namespace
;
50 QualifiedNameImpl(const AtomicString
& p
, const AtomicString
& l
, const AtomicString
& n
)
58 QualifiedName(const AtomicString
& prefix
, const AtomicString
& localName
, const AtomicString
& namespaceURI
);
60 #ifdef QNAME_DEFAULT_CONSTRUCTOR
61 QualifiedName() : m_impl(0) { }
64 QualifiedName(const QualifiedName
&);
65 const QualifiedName
& operator=(const QualifiedName
&);
67 bool operator==(const QualifiedName
& other
) const { return m_impl
== other
.m_impl
; }
68 bool operator!=(const QualifiedName
& other
) const { return !(*this == other
); }
70 bool matches(const QualifiedName
& other
) const { return m_impl
== other
.m_impl
|| (localName() == other
.localName() && namespaceURI() == other
.namespaceURI()); }
72 bool hasPrefix() const { return m_impl
->m_prefix
!= nullAtom
; }
73 void setPrefix(const AtomicString
& prefix
);
75 const AtomicString
& prefix() const { return m_impl
->m_prefix
; }
76 const AtomicString
& localName() const { return m_impl
->m_localName
; }
77 const AtomicString
& namespaceURI() const { return m_impl
->m_namespace
; }
79 String
toString() const;
81 QualifiedNameImpl
* impl() const { return m_impl
; }
83 // Init routine for globals
87 void ref() { m_impl
->ref(); }
90 QualifiedNameImpl
* m_impl
;
93 #ifndef WEBCORE_QUALIFIEDNAME_HIDE_GLOBALS
94 extern const QualifiedName anyName
;
95 inline const QualifiedName
& anyQName() { return anyName
; }
98 inline bool operator==(const AtomicString
& a
, const QualifiedName
& q
) { return a
== q
.localName(); }
99 inline bool operator!=(const AtomicString
& a
, const QualifiedName
& q
) { return a
!= q
.localName(); }
100 inline bool operator==(const QualifiedName
& q
, const AtomicString
& a
) { return a
== q
.localName(); }
101 inline bool operator!=(const QualifiedName
& q
, const AtomicString
& a
) { return a
!= q
.localName(); }
104 inline unsigned hashComponents(const QualifiedNameComponents
& buf
)
106 ASSERT(sizeof(QualifiedNameComponents
) % (sizeof(uint16_t) * 2) == 0);
108 unsigned l
= sizeof(QualifiedNameComponents
) / (sizeof(uint16_t) * 2);
109 const uint16_t* s
= reinterpret_cast<const uint16_t*>(&buf
);
110 uint32_t hash
= WTF::stringHashingStartValue
;
115 uint32_t tmp
= (s
[1] << 11) ^ hash
;
116 hash
= (hash
<< 16) ^ tmp
;
121 // Force "avalanching" of final 127 bits
128 // this avoids ever returning a hash code of 0, since that is used to
129 // signal "hash not computed yet", using a value that is likely to be
130 // effectively the same as 0 when the low bits are masked
137 struct QualifiedNameHash
{
138 static unsigned hash(const QualifiedName
& name
) { return hash(name
.impl()); }
140 static unsigned hash(const QualifiedName::QualifiedNameImpl
* name
)
142 QualifiedNameComponents c
= { name
->m_prefix
.impl(), name
->m_localName
.impl(), name
->m_namespace
.impl() };
143 return hashComponents(c
);
146 static bool equal(const QualifiedName
& a
, const QualifiedName
& b
) { return a
== b
; }
147 static bool equal(const QualifiedName::QualifiedNameImpl
* a
, const QualifiedName::QualifiedNameImpl
* b
) { return a
== b
; }
149 static const bool safeToCompareToEmptyOrDeleted
= false;
156 template<typename T
> struct DefaultHash
;
157 template<> struct DefaultHash
<WebCore::QualifiedName
> {
158 typedef WebCore::QualifiedNameHash Hash
;
161 template<> struct HashTraits
<WebCore::QualifiedName
> : GenericHashTraits
<WebCore::QualifiedName
> {
162 static const bool emptyValueIsZero
= false;
163 static WebCore::QualifiedName
emptyValue() { return WebCore::QualifiedName(WebCore::nullAtom
, WebCore::nullAtom
, WebCore::nullAtom
); }
164 static void constructDeletedValue(WebCore::QualifiedName
& slot
) { new (&slot
) WebCore::QualifiedName(WebCore::nullAtom
, WebCore::AtomicString(HashTableDeletedValue
), WebCore::nullAtom
); }
165 static bool isDeletedValue(const WebCore::QualifiedName
& slot
) { return slot
.localName().isHashTableDeletedValue(); }