2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 2008 Maksim Orlovich (maksim@kde.org)
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 _DOM_IDSTRING_h_
23 #define _DOM_IDSTRING_h_
25 #include "misc/shared.h"
26 #include "dom/dom_string.h"
27 #include "xml/dom_stringimpl.h"
28 #include "wtf/Vector.h"
35 // When we're working with a case-insensitive language, IDString can lookup
36 // IDs from strings ignoring the case itself; however it needs to be told
37 // what the canonical case is, so it knows what hash code to look for.
38 enum CaseNormalizeMode
{
45 An IDString is used to manage an identifier namespace that has some predefined constants,
46 but can be extended with new ones at runtime
48 The TableFactory template parameter must point to a class
49 that provides an idTable method returning a singleton IDTable<TableFactory>
51 template<typename TableFactory
>
57 // id 0xFFFF is handled specially in derefId so it's our default..
58 IDString(): m_id(0xFFFF) {}
60 IDString
<TableFactory
>& operator=(const IDString
& other
) {
61 TableFactory::idTable()->refId (other
.m_id
);
62 TableFactory::idTable()->derefId(m_id
);
67 IDString(const IDString
& other
) {
69 TableFactory::idTable()->refId(m_id
);
73 TableFactory::idTable()->derefId(m_id
);
80 DOMString
toString() const {
81 return TableFactory::idTable()->idToString(m_id
);
84 static IDString
<TableFactory
> fromId(unsigned short id
) {
85 IDString
<TableFactory
> nw
;
87 TableFactory::idTable()->refId(id
);
91 static IDString
<TableFactory
> fromString(const DOMString
& string
, CaseNormalizeMode cnm
= IDS_CaseSensitive
) {
92 IDString
<TableFactory
> nw
;
93 nw
.m_id
= TableFactory::idTable()->grabId(string
, cnm
); // Refs it already.
97 bool operator==(const IDString
<TableFactory
>& other
) const {
98 return m_id
== other
.m_id
;
101 QDebug
operator<<(QDebug stream
) const {
102 return id() ? (stream
<< id() << toString()) : "null idstring";
108 unsigned refCount
; // # of references, 0 if not in use.
111 Mapping(): refCount(0)
114 Mapping(const DOMString
& _name
): refCount(0), name(_name
)
120 // Wraps around DOMString and lets us trigger key sensitivity for lookup;
121 // that's done via a global fiddle --- warning, warning, warning.
125 bool operator==(const MappingKey
& other
) const;
128 MappingKey(const DOMString
& v
): str(v
) {}
130 static CaseNormalizeMode caseNormalizationMode
;
133 void refId(unsigned id
) {
136 ++m_mappings
[id
].refCount
;
139 void derefId(unsigned id
) {
142 --m_mappings
[id
].refCount
;
143 if (m_mappings
[id
].refCount
== 0)
147 const DOMString
& idToString(unsigned id
) {
148 return m_mappings
[id
].name
;
151 unsigned short grabId(const DOMString
& string
, CaseNormalizeMode cnm
);
154 // Registers a compile-type known ID constant with the name.
155 // This must be called before any other operations
156 void addStaticMapping(unsigned id
, const DOMString
& string
);
158 // Registers a hidden ID constant --- it has a name, but it
159 // can not be looked up by it.
160 void addHiddenMapping(unsigned id
, const DOMString
& string
);
162 void releaseId(unsigned id
);
164 WTF::Vector
<unsigned> m_idFreeList
;
165 WTF::Vector
<Mapping
> m_mappings
;
166 QHash
<MappingKey
, unsigned short> m_mappingLookup
;
169 template<typename TableFactory
>
170 class IDTable
: public IDTableBase
{
172 // Export methods to our version of IDString
173 friend class IDString
<TableFactory
>;
174 using IDTableBase::refId
;
175 using IDTableBase::derefId
;
182 Now these are the various ID types we used.. They are here to avoid circular
183 dependenies in headers
187 typedef khtml::IDString
<EventImpl
> EventName
;
192 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;