KHtml

JSSVGPODTypeWrapper.h
1 /*
2  * Copyright (C) 2006, 2008 Nikolas Zimmermann <[email protected]>
3  * Copyright (C) 2008 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef JSSVGPODTypeWrapper_h
28 #define JSSVGPODTypeWrapper_h
29 
30 #if ENABLE(SVG)
31 
32 #include "Frame.h"
33 #include <wtf/RefCounted.h>
34 #include "SVGElement.h"
35 
36 #include <wtf/Assertions.h>
37 #include <wtf/HashMap.h>
38 
39 namespace WebCore
40 {
41 template<typename PODType>
42 class SVGPODListItem;
43 }
44 
45 using namespace WebCore;
46 
47 namespace khtml
48 {
49 
50 template<typename PODType>
51 class JSSVGPODTypeWrapper : public RefCounted<JSSVGPODTypeWrapper<PODType> >
52 {
53 public:
54  JSSVGPODTypeWrapper() : RefCounted<JSSVGPODTypeWrapper<PODType> >(0) { }
55  virtual ~JSSVGPODTypeWrapper() { }
56 
57  // Getter wrapper
58  virtual operator PODType() = 0;
59 
60  // Setter wrapper
61  virtual void commitChange(PODType, SVGElement *) = 0;
62 };
63 
64 template<typename PODType, typename PODTypeCreator>
65 class JSSVGPODTypeWrapperCreatorReadWrite : public JSSVGPODTypeWrapper<PODType>
66 {
67 public:
68  typedef PODType(PODTypeCreator::*GetterMethod)() const;
69  typedef void (PODTypeCreator::*SetterMethod)(PODType);
70 
71  JSSVGPODTypeWrapperCreatorReadWrite(PODTypeCreator *creator, GetterMethod getter, SetterMethod setter)
72  : m_creator(creator)
73  , m_getter(getter)
74  , m_setter(setter)
75  {
76  ASSERT(creator);
77  ASSERT(getter);
78  ASSERT(setter);
79  }
80 
81  virtual ~JSSVGPODTypeWrapperCreatorReadWrite() { }
82 
83  // Getter wrapper
84  virtual operator PODType()
85  {
86  return (m_creator.get()->*m_getter)();
87  }
88 
89  // Setter wrapper
90  virtual void commitChange(PODType type, SVGElement *context)
91  {
92  if (!m_setter) {
93  return;
94  }
95 
96  (m_creator.get()->*m_setter)(type);
97 
98  if (context) {
99  context->svgAttributeChanged(m_creator->associatedAttributeName());
100  }
101  }
102 
103 private:
104  // Update callbacks
105  RefPtr<PODTypeCreator> m_creator;
106  GetterMethod m_getter;
107  SetterMethod m_setter;
108 };
109 
110 template<typename PODType>
111 class JSSVGPODTypeWrapperCreatorReadOnly : public JSSVGPODTypeWrapper<PODType>
112 {
113 public:
114  JSSVGPODTypeWrapperCreatorReadOnly(PODType type)
115  : m_podType(type)
116  { }
117 
118  virtual ~JSSVGPODTypeWrapperCreatorReadOnly() { }
119 
120  // Getter wrapper
121  virtual operator PODType()
122  {
123  return m_podType;
124  }
125 
126  // Setter wrapper
127  virtual void commitChange(PODType type, SVGElement *)
128  {
129  m_podType = type;
130  }
131 
132 private:
133  PODType m_podType;
134 };
135 
136 template<typename PODType>
137 class JSSVGPODTypeWrapperCreatorForList : public JSSVGPODTypeWrapper<PODType>
138 {
139 public:
140  typedef PODType(SVGPODListItem<PODType>::*GetterMethod)() const;
141  typedef void (SVGPODListItem<PODType>::*SetterMethod)(PODType);
142 
143  JSSVGPODTypeWrapperCreatorForList(SVGPODListItem<PODType> *creator, const QualifiedName &attributeName)
144  : m_creator(creator)
145  , m_getter(&SVGPODListItem<PODType>::value)
146  , m_setter(&SVGPODListItem<PODType>::setValue)
147  , m_associatedAttributeName(attributeName)
148  {
149  ASSERT(m_creator);
150  ASSERT(m_getter);
151  ASSERT(m_setter);
152  }
153 
154  virtual ~JSSVGPODTypeWrapperCreatorForList() { }
155 
156  // Getter wrapper
157  virtual operator PODType()
158  {
159  return (m_creator.get()->*m_getter)();
160  }
161 
162  // Setter wrapper
163  virtual void commitChange(PODType type, SVGElement *context)
164  {
165  if (!m_setter) {
166  return;
167  }
168 
169  (m_creator.get()->*m_setter)(type);
170 
171  if (context) {
172  context->svgAttributeChanged(m_associatedAttributeName);
173  }
174  }
175 
176 private:
177  // Update callbacks
178  RefPtr<SVGPODListItem<PODType> > m_creator;
179  GetterMethod m_getter;
180  SetterMethod m_setter;
181  const QualifiedName &m_associatedAttributeName;
182 };
183 
184 // Caching facilities
185 template<typename PODType, typename PODTypeCreator>
186 struct PODTypeReadWriteHashInfo {
187  typedef PODType(PODTypeCreator::*GetterMethod)() const;
188  typedef void (PODTypeCreator::*SetterMethod)(PODType);
189 
190  // Empty value
191  PODTypeReadWriteHashInfo()
192  : creator(0)
193  , getter(0)
194  , setter(0)
195  {
196  }
197 
198  // Deleted value
199  PODTypeReadWriteHashInfo(WTF::HashTableDeletedValueType)
200  : creator(reinterpret_cast<PODTypeCreator *>(-1))
201  {
202  }
203  bool isHashTableDeletedValue() const
204  {
205  return creator == reinterpret_cast<PODTypeCreator *>(-1);
206  }
207 
208  PODTypeReadWriteHashInfo(PODTypeCreator *_creator, GetterMethod _getter, SetterMethod _setter)
209  : creator(_creator)
210  , getter(_getter)
211  , setter(_setter)
212  {
213  ASSERT(creator);
214  ASSERT(getter);
215  }
216 
217  bool operator==(const PODTypeReadWriteHashInfo &other) const
218  {
219  return creator == other.creator && getter == other.getter && setter == other.setter;
220  }
221 
222  PODTypeCreator *creator;
223  GetterMethod getter;
224  SetterMethod setter;
225 };
226 
227 template<typename PODType, typename PODTypeCreator>
228 struct PODTypeReadWriteHashInfoHash {
229  static unsigned hash(const PODTypeReadWriteHashInfo<PODType, PODTypeCreator> &info)
230  {
231  return StringImpl::computeHash(reinterpret_cast<const WebCore::UChar *>(&info), sizeof(PODTypeReadWriteHashInfo<PODType, PODTypeCreator>) / sizeof(WebCore::UChar));
232  }
233 
234  static bool equal(const PODTypeReadWriteHashInfo<PODType, PODTypeCreator> &a, const PODTypeReadWriteHashInfo<PODType, PODTypeCreator> &b)
235  {
236  return a == b;
237  }
238 
239  static const bool safeToCompareToEmptyOrDeleted = true;
240 };
241 
242 template<typename PODType, typename PODTypeCreator>
243 struct PODTypeReadWriteHashInfoTraits : WTF::GenericHashTraits<PODTypeReadWriteHashInfo<PODType, PODTypeCreator> > {
244  static const bool emptyValueIsZero = true;
245  static const bool needsDestruction = false;
246 
247  static const PODTypeReadWriteHashInfo<PODType, PODTypeCreator> &emptyValue()
248  {
249  static PODTypeReadWriteHashInfo<PODType, PODTypeCreator> key;
250  return key;
251  }
252 
253  static void constructDeletedValue(PODTypeReadWriteHashInfo<PODType, PODTypeCreator> *slot)
254  {
255  new(slot) PODTypeReadWriteHashInfo<PODType, PODTypeCreator>(WTF::HashTableDeletedValue);
256  }
257  static bool isDeletedValue(const PODTypeReadWriteHashInfo<PODType, PODTypeCreator> &value)
258  {
259  return value.isHashTableDeletedValue();
260  }
261 };
262 
263 template<typename PODType, typename PODTypeCreator>
264 class JSSVGPODTypeWrapperCache
265 {
266 public:
267  typedef PODType(PODTypeCreator::*GetterMethod)() const;
268  typedef void (PODTypeCreator::*SetterMethod)(PODType);
269 
270  typedef HashMap<PODTypeReadWriteHashInfo<PODType, PODTypeCreator>, JSSVGPODTypeWrapperCreatorReadWrite<PODType, PODTypeCreator>*, PODTypeReadWriteHashInfoHash<PODType, PODTypeCreator>, PODTypeReadWriteHashInfoTraits<PODType, PODTypeCreator> > ReadWriteHashMap;
271  typedef typename ReadWriteHashMap::const_iterator ReadWriteHashMapIterator;
272 
273  static ReadWriteHashMap &readWriteHashMap()
274  {
275  static ReadWriteHashMap _readWriteHashMap;
276  return _readWriteHashMap;
277  }
278 
279  // Used for readwrite attributes only
280  static JSSVGPODTypeWrapper<PODType> *lookupOrCreateWrapper(PODTypeCreator *creator, GetterMethod getter, SetterMethod setter)
281  {
282  ReadWriteHashMap &map(readWriteHashMap());
283  PODTypeReadWriteHashInfo<PODType, PODTypeCreator> info(creator, getter, setter);
284 
285  if (map.contains(info)) {
286  return map.get(info);
287  }
288 
289  JSSVGPODTypeWrapperCreatorReadWrite<PODType, PODTypeCreator> *wrapper = new JSSVGPODTypeWrapperCreatorReadWrite<PODType, PODTypeCreator>(creator, getter, setter);
290  map.set(info, wrapper);
291  return wrapper;
292  }
293 
294  static void forgetWrapper(JSSVGPODTypeWrapper<PODType> *wrapper)
295  {
296  ReadWriteHashMap &map(readWriteHashMap());
297 
298  ReadWriteHashMapIterator it = map.begin();
299  ReadWriteHashMapIterator end = map.end();
300 
301  for (; it != end; ++it) {
302  if (it->second != wrapper) {
303  continue;
304  }
305 
306  // It's guaruanteed that there's just one object we need to take care of.
307  map.remove(it->first);
308  break;
309  }
310  }
311 };
312 
313 };
314 
315 #endif // ENABLE(SVG)
316 #endif // JSSVGPODTypeWrapper_h
This file is part of the HTML rendering engine for KDE.
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
const QList< QKeySequence > & end()
QFuture< void > map(Sequence &sequence, MapFunctor function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.