1/*
2 * Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21#ifndef QualifiedName_h
22#define QualifiedName_h
23
24#include <wtf/Forward.h>
25#include <wtf/HashTraits.h>
26#include <wtf/RefCounted.h>
27#include <wtf/text/AtomicString.h>
28
29namespace WebCore {
30
31struct QualifiedNameComponents {
32    StringImpl* m_prefix;
33    StringImpl* m_localName;
34    StringImpl* m_namespace;
35};
36
37class QualifiedName {
38    WTF_MAKE_FAST_ALLOCATED;
39public:
40    class QualifiedNameImpl : public RefCounted<QualifiedNameImpl> {
41    public:
42        static PassRefPtr<QualifiedNameImpl> create(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI)
43        {
44            return adoptRef(new QualifiedNameImpl(prefix, localName, namespaceURI));
45        }
46
47        ~QualifiedNameImpl();
48
49        unsigned computeHash() const;
50
51        mutable unsigned m_existingHash;
52        const AtomicString m_prefix;
53        const AtomicString m_localName;
54        const AtomicString m_namespace;
55        mutable AtomicString m_localNameUpper;
56
57    private:
58        QualifiedNameImpl(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI)
59            : m_existingHash(0)
60            , m_prefix(prefix)
61            , m_localName(localName)
62            , m_namespace(namespaceURI)
63        {
64            ASSERT(!namespaceURI.isEmpty() || namespaceURI.isNull());
65        }
66    };
67
68    QualifiedName(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI);
69    QualifiedName(WTF::HashTableDeletedValueType) : m_impl(hashTableDeletedValue()) { }
70    bool isHashTableDeletedValue() const { return m_impl == hashTableDeletedValue(); }
71    ~QualifiedName();
72#ifdef QNAME_DEFAULT_CONSTRUCTOR
73    QualifiedName() : m_impl(0) { }
74#endif
75
76    QualifiedName(const QualifiedName& other) : m_impl(other.m_impl) { ref(); }
77    const QualifiedName& operator=(const QualifiedName& other) { other.ref(); deref(); m_impl = other.m_impl; return *this; }
78
79    bool operator==(const QualifiedName& other) const { return m_impl == other.m_impl; }
80    bool operator!=(const QualifiedName& other) const { return !(*this == other); }
81
82    bool matches(const QualifiedName& other) const { return m_impl == other.m_impl || (localName() == other.localName() && namespaceURI() == other.namespaceURI()); }
83
84    bool hasPrefix() const { return m_impl->m_prefix != nullAtom; }
85    void setPrefix(const AtomicString& prefix) { *this = QualifiedName(prefix, localName(), namespaceURI()); }
86
87    const AtomicString& prefix() const { return m_impl->m_prefix; }
88    const AtomicString& localName() const { return m_impl->m_localName; }
89    const AtomicString& namespaceURI() const { return m_impl->m_namespace; }
90
91    // Uppercased localName, cached for efficiency
92    const AtomicString& localNameUpper() const;
93
94    String toString() const;
95
96    QualifiedNameImpl* impl() const { return m_impl; }
97
98    // Init routine for globals
99    static void init();
100
101private:
102    void ref() const { m_impl->ref(); }
103    void deref();
104
105    static QualifiedNameImpl* hashTableDeletedValue() { return RefPtr<QualifiedNameImpl>::hashTableDeletedValue(); }
106
107    QualifiedNameImpl* m_impl;
108};
109
110#ifndef WEBCORE_QUALIFIEDNAME_HIDE_GLOBALS
111extern const QualifiedName anyName;
112inline const QualifiedName& anyQName() { return anyName; }
113#endif
114
115const QualifiedName& nullQName();
116
117inline bool operator==(const AtomicString& a, const QualifiedName& q) { return a == q.localName(); }
118inline bool operator!=(const AtomicString& a, const QualifiedName& q) { return a != q.localName(); }
119inline bool operator==(const QualifiedName& q, const AtomicString& a) { return a == q.localName(); }
120inline bool operator!=(const QualifiedName& q, const AtomicString& a) { return a != q.localName(); }
121
122inline unsigned hashComponents(const QualifiedNameComponents& buf)
123{
124    return StringHasher::hashMemory<sizeof(QualifiedNameComponents)>(&buf);
125}
126
127struct QualifiedNameHash {
128    static unsigned hash(const QualifiedName& name) { return hash(name.impl()); }
129
130    static unsigned hash(const QualifiedName::QualifiedNameImpl* name)
131    {
132        if (!name->m_existingHash)
133            name->m_existingHash = name->computeHash();
134        return name->m_existingHash;
135    }
136
137    static bool equal(const QualifiedName& a, const QualifiedName& b) { return a == b; }
138    static bool equal(const QualifiedName::QualifiedNameImpl* a, const QualifiedName::QualifiedNameImpl* b) { return a == b; }
139
140    static const bool safeToCompareToEmptyOrDeleted = false;
141};
142
143void createQualifiedName(void* targetAddress, StringImpl* name);
144void createQualifiedName(void* targetAddress, StringImpl* name, const AtomicString& nameNamespace);
145
146}
147
148namespace WTF {
149
150    template<typename T> struct DefaultHash;
151
152    template<> struct DefaultHash<WebCore::QualifiedName> {
153        typedef WebCore::QualifiedNameHash Hash;
154    };
155
156    template<> struct HashTraits<WebCore::QualifiedName> : SimpleClassHashTraits<WebCore::QualifiedName> {
157        static const bool emptyValueIsZero = false;
158        static WebCore::QualifiedName emptyValue() { return WebCore::nullQName(); }
159    };
160}
161
162#endif
163