• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "config.h"
21 
22 #ifdef SKIP_STATIC_CONSTRUCTORS_ON_GCC
23 #define WEBCORE_QUALIFIEDNAME_HIDE_GLOBALS 1
24 #else
25 #define QNAME_DEFAULT_CONSTRUCTOR
26 #endif
27 
28 #include "QualifiedName.h"
29 #include "StaticConstructors.h"
30 #include <wtf/Assertions.h>
31 #include <wtf/HashSet.h>
32 
33 namespace WebCore {
34 
35 typedef HashSet<QualifiedName::QualifiedNameImpl*, QualifiedNameHash> QNameSet;
36 
37 struct QNameComponentsTranslator {
hashWebCore::QNameComponentsTranslator38     static unsigned hash(const QualifiedNameComponents& components)
39     {
40         return hashComponents(components);
41     }
equalWebCore::QNameComponentsTranslator42     static bool equal(QualifiedName::QualifiedNameImpl* name, const QualifiedNameComponents& c)
43     {
44         return c.m_prefix == name->m_prefix.impl() && c.m_localName == name->m_localName.impl() && c.m_namespace == name->m_namespace.impl();
45     }
translateWebCore::QNameComponentsTranslator46     static void translate(QualifiedName::QualifiedNameImpl*& location, const QualifiedNameComponents& components, unsigned)
47     {
48         location = QualifiedName::QualifiedNameImpl::create(components.m_prefix, components.m_localName, components.m_namespace).releaseRef();
49     }
50 };
51 
52 static QNameSet* gNameCache;
53 
QualifiedName(const AtomicString & p,const AtomicString & l,const AtomicString & n)54 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
55 {
56     if (!gNameCache)
57         gNameCache = new QNameSet;
58     QualifiedNameComponents components = { p.impl(), l.impl(), n.isEmpty() ? nullAtom.impl() : n.impl() };
59     pair<QNameSet::iterator, bool> addResult = gNameCache->add<QualifiedNameComponents, QNameComponentsTranslator>(components);
60     m_impl = *addResult.first;
61     if (!addResult.second)
62         m_impl->ref();
63 }
64 
deref()65 void QualifiedName::deref()
66 {
67 #ifdef QNAME_DEFAULT_CONSTRUCTOR
68     if (!m_impl)
69         return;
70 #endif
71 
72     if (m_impl->hasOneRef())
73         gNameCache->remove(m_impl);
74     m_impl->deref();
75 }
76 
toString() const77 String QualifiedName::toString() const
78 {
79     String local = localName();
80     if (hasPrefix())
81         return prefix() + ":" + local;
82     return local;
83 }
84 
85 // Global init routines
DEFINE_GLOBAL(QualifiedName,anyName,nullAtom,starAtom,starAtom)86 DEFINE_GLOBAL(QualifiedName, anyName, nullAtom, starAtom, starAtom)
87 
88 void QualifiedName::init()
89 {
90     static bool initialized;
91     if (!initialized) {
92         // Use placement new to initialize the globals.
93 
94         AtomicString::init();
95         new ((void*)&anyName) QualifiedName(nullAtom, starAtom, starAtom);
96         initialized = true;
97     }
98 }
99 
100 }
101