]> git.saurik.com Git - iphone-api.git/blob - WebCore/QualifiedName.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / QualifiedName.h
1 /*
2 * This file is part of the DOM implementation for KDE.
3 *
4 * Copyright (C) 2005 Apple Computer, Inc.
5 *
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.
10 *
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.
15 *
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.
20 *
21 */
22 #ifndef QualifiedName_h
23 #define QualifiedName_h
24
25 #include "AtomicString.h"
26 #include <wtf/HashFunctions.h>
27
28 namespace WebCore {
29
30 struct QualifiedNameComponents {
31 StringImpl* m_prefix;
32 StringImpl* m_localName;
33 StringImpl* m_namespace;
34 };
35
36 class QualifiedName {
37 public:
38 class QualifiedNameImpl : public RefCounted<QualifiedNameImpl> {
39 public:
40 static PassRefPtr<QualifiedNameImpl> create(const AtomicString& p, const AtomicString& l, const AtomicString& n)
41 {
42 return adoptRef(new QualifiedNameImpl(p, l, n));
43 }
44
45 AtomicString m_prefix;
46 AtomicString m_localName;
47 AtomicString m_namespace;
48
49 private:
50 QualifiedNameImpl(const AtomicString& p, const AtomicString& l, const AtomicString& n)
51 : m_prefix(p)
52 , m_localName(l)
53 , m_namespace(n)
54 {
55 }
56 };
57
58 QualifiedName(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI);
59 ~QualifiedName();
60 #ifdef QNAME_DEFAULT_CONSTRUCTOR
61 QualifiedName() : m_impl(0) { }
62 #endif
63
64 QualifiedName(const QualifiedName&);
65 const QualifiedName& operator=(const QualifiedName&);
66
67 bool operator==(const QualifiedName& other) const { return m_impl == other.m_impl; }
68 bool operator!=(const QualifiedName& other) const { return !(*this == other); }
69
70 bool matches(const QualifiedName& other) const { return m_impl == other.m_impl || (localName() == other.localName() && namespaceURI() == other.namespaceURI()); }
71
72 bool hasPrefix() const { return m_impl->m_prefix != nullAtom; }
73 void setPrefix(const AtomicString& prefix);
74
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; }
78
79 String toString() const;
80
81 QualifiedNameImpl* impl() const { return m_impl; }
82
83 // Init routine for globals
84 static void init();
85
86 private:
87 void ref() { m_impl->ref(); }
88 void deref();
89
90 QualifiedNameImpl* m_impl;
91 };
92
93 #ifndef WEBCORE_QUALIFIEDNAME_HIDE_GLOBALS
94 extern const QualifiedName anyName;
95 inline const QualifiedName& anyQName() { return anyName; }
96 #endif
97
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(); }
102
103
104 inline unsigned hashComponents(const QualifiedNameComponents& buf)
105 {
106 ASSERT(sizeof(QualifiedNameComponents) % (sizeof(uint16_t) * 2) == 0);
107
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;
111
112 // Main loop
113 for (; l > 0; l--) {
114 hash += s[0];
115 uint32_t tmp = (s[1] << 11) ^ hash;
116 hash = (hash << 16) ^ tmp;
117 s += 2;
118 hash += hash >> 11;
119 }
120
121 // Force "avalanching" of final 127 bits
122 hash ^= hash << 3;
123 hash += hash >> 5;
124 hash ^= hash << 2;
125 hash += hash >> 15;
126 hash ^= hash << 10;
127
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
131 if (hash == 0)
132 hash = 0x80000000;
133
134 return hash;
135 }
136
137 struct QualifiedNameHash {
138 static unsigned hash(const QualifiedName& name) { return hash(name.impl()); }
139
140 static unsigned hash(const QualifiedName::QualifiedNameImpl* name)
141 {
142 QualifiedNameComponents c = { name->m_prefix.impl(), name->m_localName.impl(), name->m_namespace.impl() };
143 return hashComponents(c);
144 }
145
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; }
148
149 static const bool safeToCompareToEmptyOrDeleted = false;
150 };
151
152 }
153
154 namespace WTF {
155
156 template<typename T> struct DefaultHash;
157 template<> struct DefaultHash<WebCore::QualifiedName> {
158 typedef WebCore::QualifiedNameHash Hash;
159 };
160
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(); }
166 };
167 }
168
169 #endif