don't discard iframe children.
[kdelibs.git] / khtml / misc / idstring.h
blobfab154e1a25623dcbb486fc8e577ec42a43eb839
1 /*
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"
29 #include <QHash>
31 using DOM::DOMString;
33 namespace khtml {
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 {
39 IDS_CaseSensitive,
40 IDS_NormalizeUpper,
41 IDS_NormalizeLower
44 /**
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>
52 class IDString {
53 private:
54 unsigned short m_id;
56 public:
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);
63 m_id = other.m_id;
64 return *this;
67 IDString(const IDString& other) {
68 m_id = other.m_id;
69 TableFactory::idTable()->refId(m_id);
72 ~IDString() {
73 TableFactory::idTable()->derefId(m_id);
76 unsigned id() const {
77 return 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;
86 nw.m_id = id;
87 TableFactory::idTable()->refId(id);
88 return nw;
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.
94 return nw;
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";
106 class IDTableBase {
107 struct Mapping {
108 unsigned refCount; // # of references, 0 if not in use.
109 DOMString name;
111 Mapping(): refCount(0)
114 Mapping(const DOMString& _name): refCount(0), name(_name)
119 public:
120 // Wraps around DOMString and lets us trigger key sensitivity for lookup;
121 // that's done via a global fiddle --- warning, warning, warning.
122 struct MappingKey
124 DOMString str;
125 bool operator==(const MappingKey& other) const;
127 MappingKey() {}
128 MappingKey(const DOMString& v): str(v) {}
130 static CaseNormalizeMode caseNormalizationMode;
132 protected:
133 void refId(unsigned id) {
134 if (id == 0xFFFF)
135 return;
136 ++m_mappings[id].refCount;
139 void derefId(unsigned id) {
140 if (id == 0xFFFF)
141 return;
142 --m_mappings[id].refCount;
143 if (m_mappings[id].refCount == 0)
144 releaseId(id);
147 const DOMString& idToString(unsigned id) {
148 return m_mappings[id].name;
151 unsigned short grabId(const DOMString& string, CaseNormalizeMode cnm);
153 public:
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);
161 private:
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 {
171 public:
172 // Export methods to our version of IDString
173 friend class IDString<TableFactory>;
174 using IDTableBase::refId;
175 using IDTableBase::derefId;
176 IDTable() {}
182 Now these are the various ID types we used.. They are here to avoid circular
183 dependenies in headers
185 namespace DOM {
186 class EventImpl;
187 typedef khtml::IDString<EventImpl> EventName;
191 #endif
192 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;