1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/core/dom/qualified_name.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/mathml_names.h"
#include "third_party/blink/renderer/core/svg_names.h"
#include "third_party/blink/renderer/core/xlink_names.h"
#include "third_party/blink/renderer/core/xml_names.h"
#include "third_party/blink/renderer/core/xmlns_names.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/size_assertions.h"
#include "third_party/blink/renderer/platform/wtf/static_constructors.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"
namespace blink {
struct SameSizeAsQualifiedNameImpl
: public RefCounted<SameSizeAsQualifiedNameImpl> {
unsigned bitfield;
void* pointers[4];
};
ASSERT_SIZE(QualifiedName::QualifiedNameImpl, SameSizeAsQualifiedNameImpl);
using QualifiedNameCache = HashSet<QualifiedName::QualifiedNameImpl*>;
static QualifiedNameCache& GetQualifiedNameCache() {
// This code is lockless and thus assumes it all runs on one thread!
DCHECK(IsMainThread());
static QualifiedNameCache* g_name_cache = new QualifiedNameCache;
return *g_name_cache;
}
struct QNameComponentsTranslator {
static unsigned GetHash(const QualifiedNameData& data) {
return HashComponents(data.components_);
}
static bool Equal(QualifiedName::QualifiedNameImpl* name,
const QualifiedNameData& data) {
return data.components_.prefix_ == name->prefix_.Impl() &&
data.components_.local_name_ == name->local_name_.Impl() &&
data.components_.namespace_ == name->namespace_.Impl();
}
static void Store(QualifiedName::QualifiedNameImpl*& location,
const QualifiedNameData& data,
unsigned) {
const QualifiedNameComponents& components = data.components_;
auto name = QualifiedName::QualifiedNameImpl::Create(
components.prefix_, components.local_name_, components.namespace_,
data.is_static_);
name->AddRef();
location = name.get();
}
};
QualifiedName::QualifiedName(const AtomicString& p,
const AtomicString& l,
const AtomicString& n) {
QualifiedNameData data = {
{p.Impl(), l.Impl(), n.empty() ? g_null_atom.Impl() : n.Impl()}, false};
QualifiedNameCache::AddResult add_result =
GetQualifiedNameCache().AddWithTranslator<QNameComponentsTranslator>(
data);
impl_ = *add_result.stored_value;
if (add_result.is_new_entry)
impl_->Release();
}
QualifiedName::QualifiedName(const AtomicString& local_name)
: QualifiedName(g_null_atom, local_name, g_null_atom) {}
QualifiedName::QualifiedName(const AtomicString& p,
const AtomicString& l,
const AtomicString& n,
bool is_static) {
QualifiedNameData data = {{p.Impl(), l.Impl(), n.Impl()}, is_static};
QualifiedNameCache::AddResult add_result =
GetQualifiedNameCache().AddWithTranslator<QNameComponentsTranslator>(
data);
impl_ = *add_result.stored_value;
if (add_result.is_new_entry)
impl_->Release();
}
QualifiedName::~QualifiedName() = default;
QualifiedName::QualifiedNameImpl::~QualifiedNameImpl() {
GetQualifiedNameCache().erase(this);
}
String QualifiedName::ToString() const {
String local = LocalName();
if (HasPrefix())
return Prefix().GetString() + ":" + local;
return local;
}
// Global init routines
DEFINE_GLOBAL(, QualifiedName, g_any_name);
DEFINE_GLOBAL(, QualifiedName, g_null_name);
void QualifiedName::InitAndReserveCapacityForSize(unsigned size) {
DCHECK(g_star_atom.Impl());
GetQualifiedNameCache().ReserveCapacityForSize(
size + 2 /*g_star_atom and g_null_atom */);
new ((void*)&g_any_name)
QualifiedName(g_null_atom, g_null_atom, g_star_atom, true);
new ((void*)&g_null_name)
QualifiedName(g_null_atom, g_null_atom, g_null_atom, true);
}
const AtomicString& QualifiedName::LocalNameUpperSlow() const {
impl_->local_name_upper_ = impl_->local_name_.UpperASCII();
return impl_->local_name_upper_;
}
unsigned QualifiedName::QualifiedNameImpl::ComputeHash() const {
QualifiedNameComponents components = {prefix_.Impl(), local_name_.Impl(),
namespace_.Impl()};
return HashComponents(components);
}
void QualifiedName::CreateStatic(void* target_address,
StringImpl* name,
const AtomicString& name_namespace) {
new (target_address)
QualifiedName(g_null_atom, AtomicString(name), name_namespace, true);
}
void QualifiedName::CreateStatic(void* target_address, StringImpl* name) {
new (target_address)
QualifiedName(g_null_atom, AtomicString(name), g_null_atom, true);
}
std::ostream& operator<<(std::ostream& ostream, const QualifiedName& qname) {
ostream << "QualifiedName(local=" << qname.LocalName()
<< " ns=" << qname.NamespaceURI() << " prefix=" << qname.Prefix()
<< ")";
return ostream;
}
} // namespace blink
|