KHtml

idstring.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 2008,2009 Maksim Orlovich ([email protected])
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22 #ifndef _DOM_IDSTRING_h_
23 #define _DOM_IDSTRING_h_
24 
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>
30 
31 using DOM::DOMString;
32 using DOM::DOMStringImpl;
33 
34 namespace khtml
35 {
36 
37 // When we're working with a case-insensitive language, IDString can lookup
38 // IDs from strings ignoring the case itself; however it needs to be told
39 // what the canonical case is, so it knows what hash code to look for.
40 enum CaseNormalizeMode {
41  IDS_CaseSensitive,
42  IDS_NormalizeUpper,
43  IDS_NormalizeLower
44 };
45 
46 /**
47  An IDString is used to manage an identifier namespace that has some predefined constants,
48  but can be extended with new ones at runtime
49 
50  The TableFactory template parameter must point to a class
51  that provides an idTable method returning a singleton IDTable<TableFactory>
52 */
53 template<typename TableFactory>
54 class IDString
55 {
56 private:
57  unsigned short m_id;
58 
59 public:
60  // id 0xFFFF is handled specially in derefId so it's our default..
61  IDString(): m_id(0xFFFF) {}
62 
63  IDString<TableFactory> &operator=(const IDString &other)
64  {
65  TableFactory::idTable()->refId(other.m_id);
66  TableFactory::idTable()->derefId(m_id);
67  m_id = other.m_id;
68  return *this;
69  }
70 
71  IDString(const IDString &other)
72  {
73  m_id = other.m_id;
74  TableFactory::idTable()->refId(m_id);
75  }
76 
77  ~IDString()
78  {
79  TableFactory::idTable()->derefId(m_id);
80  }
81 
82  unsigned id() const
83  {
84  return m_id;
85  }
86 
87  DOMString toString() const
88  {
89  return TableFactory::idTable()->idToString(m_id);
90  }
91 
92  static IDString<TableFactory> fromId(unsigned short id)
93  {
95  nw.m_id = id;
96  TableFactory::idTable()->refId(id);
97  return nw;
98  }
99 
100  static IDString<TableFactory> fromString(const DOMString &string, CaseNormalizeMode cnm = IDS_CaseSensitive)
101  {
102  return fromString(string.implementation(), cnm);
103  }
104 
105  static IDString<TableFactory> fromString(DOMStringImpl *string, CaseNormalizeMode cnm = IDS_CaseSensitive)
106  {
108  nw.m_id = TableFactory::idTable()->grabId(string, cnm); // Refs it already.
109  return nw;
110  }
111 
112  bool operator==(const IDString<TableFactory> &other) const
113  {
114  return m_id == other.m_id;
115  }
116 
117  QDebug operator<<(QDebug stream) const
118  {
119  return id() ? (stream << id() << toString()) : "null idstring";
120  }
121 };
122 
123 class IDTableBase
124 {
125  struct Mapping {
126  unsigned refCount; // # of references, 0 if not in use.
127  DOMStringImpl *name; // same as our key, so no separate
128  // refcount (except for hidden mappings)
129 
130  Mapping(): refCount(0)
131  {}
132 
133  Mapping(DOMStringImpl *_name): refCount(0), name(_name)
134  {}
135  };
136 
137 public:
138  // Wraps around DOMString and lets us trigger key sensitivity for lookup;
139  // that's done via a global fiddle --- warning, warning, warning.
140  struct MappingKey {
141  DOMStringImpl *str;
142  bool operator==(const MappingKey &other) const;
143 
144  MappingKey() {}
145  MappingKey(DOMStringImpl *v): str(v) {}
146 
147  static CaseNormalizeMode caseNormalizationMode;
148  };
149 protected:
150  void refId(unsigned id)
151  {
152  if (id == 0xFFFF) {
153  return;
154  }
155  ++m_mappings[id].refCount;
156  }
157 
158  void derefId(unsigned id)
159  {
160  if (id == 0xFFFF) {
161  return;
162  }
163  --m_mappings[id].refCount;
164  if (m_mappings[id].refCount == 0) {
165  releaseId(id);
166  }
167  }
168 
169  const DOMString idToString(unsigned id)
170  {
171  return DOMString(m_mappings[id].name);
172  }
173 
174  unsigned short grabId(DOMStringImpl *string, CaseNormalizeMode cnm);
175 
176 public:
177  // Registers a compile-type known ID constant with the name.
178  // This must be called before any other operations
179  void addStaticMapping(unsigned id, const DOMString &string);
180 
181  // Registers a hidden ID constant --- it has a name, but it
182  // can not be looked up by it.
183  void addHiddenMapping(unsigned id, const DOMString &string);
184 private:
185  void releaseId(unsigned id);
186 
187  WTF::Vector <unsigned> m_idFreeList;
188  WTF::Vector <Mapping> m_mappings;
189  QHash <MappingKey, unsigned short> m_mappingLookup;
190 };
191 
192 template<typename TableFactory>
193 class IDTable: public IDTableBase
194 {
195 public:
196  // Export methods to our version of IDString
197  friend class IDString<TableFactory>;
198  using IDTableBase::refId;
199  using IDTableBase::derefId;
200  IDTable() {}
201 };
202 
203 }
204 
205 /**
206  Now these are the various ID types we used.. They are here to avoid circular
207  dependenies in headers
208 */
209 namespace DOM
210 {
211 class EventImpl;
213 }
214 
215 #endif
An IDString is used to manage an identifier namespace that has some predefined constants, but can be extended with new ones at runtime.
Definition: idstring.h:54
This file is part of the HTML rendering engine for KDE.
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
This library provides a full-featured HTML parser and widget.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.