2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #ifndef DeprecatedSVGAnimatedTemplate_h
22 #define DeprecatedSVGAnimatedTemplate_h
25 #include "DeprecatedSVGAnimatedPropertyTraits.h"
26 #include "QualifiedName.h"
27 #include <wtf/Forward.h>
28 #include <wtf/HashMap.h>
33 class SVGTransformList;
35 struct DeprecatedSVGAnimatedTypeWrapperKey {
37 DeprecatedSVGAnimatedTypeWrapperKey()
43 DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
44 : element(reinterpret_cast<SVGElement*>(-1))
48 bool isHashTableDeletedValue() const
50 return element == reinterpret_cast<SVGElement*>(-1);
53 DeprecatedSVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName)
55 , attributeName(_attributeName.impl())
58 ASSERT(attributeName);
61 bool operator==(const DeprecatedSVGAnimatedTypeWrapperKey& other) const
63 return element == other.element && attributeName == other.attributeName;
66 const SVGElement* element;
67 AtomicStringImpl* attributeName;
70 struct DeprecatedSVGAnimatedTypeWrapperKeyHash {
71 static unsigned hash(const DeprecatedSVGAnimatedTypeWrapperKey& key)
73 return WTF::StringHasher::createBlobHash<sizeof(DeprecatedSVGAnimatedTypeWrapperKey)>(&key);
76 static bool equal(const DeprecatedSVGAnimatedTypeWrapperKey& a, const DeprecatedSVGAnimatedTypeWrapperKey& b)
81 static const bool safeToCompareToEmptyOrDeleted = true;
84 struct DeprecatedSVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<DeprecatedSVGAnimatedTypeWrapperKey> {
85 static const bool emptyValueIsZero = true;
87 static void constructDeletedValue(DeprecatedSVGAnimatedTypeWrapperKey& slot)
89 new (&slot) DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
92 static bool isDeletedValue(const DeprecatedSVGAnimatedTypeWrapperKey& value)
94 return value.isHashTableDeletedValue();
98 template<typename AnimatedType>
99 class DeprecatedSVGAnimatedTemplate : public RefCounted<DeprecatedSVGAnimatedTemplate<AnimatedType> > {
101 typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::PassType PassType;
102 typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::ReturnType ReturnType;
104 virtual ~DeprecatedSVGAnimatedTemplate() { forgetWrapper(this); }
106 virtual ReturnType baseVal() const = 0;
107 virtual void setBaseVal(PassType) = 0;
109 virtual ReturnType animVal() const = 0;
110 virtual void setAnimVal(PassType) = 0;
112 virtual const QualifiedName& associatedAttributeName() const = 0;
114 typedef HashMap<DeprecatedSVGAnimatedTypeWrapperKey, DeprecatedSVGAnimatedTemplate<AnimatedType>*, DeprecatedSVGAnimatedTypeWrapperKeyHash, DeprecatedSVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
115 typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
117 static ElementToWrapperMap* wrapperCache()
119 static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap;
120 return s_wrapperCache;
123 static void forgetWrapper(DeprecatedSVGAnimatedTemplate<AnimatedType>* wrapper)
125 ElementToWrapperMap* cache = wrapperCache();
126 ElementToWrapperMapIterator itr = cache->begin();
127 ElementToWrapperMapIterator end = cache->end();
128 for (; itr != end; ++itr) {
129 if (itr->second == wrapper) {
130 cache->remove(itr->first);
137 template<typename AnimatedType>
138 class DeprecatedSVGAnimatedProperty;
140 template<typename AnimatedType, typename AnimatedTearOff>
141 PassRefPtr<AnimatedTearOff> lookupOrCreateWrapper(SVGElement* element, DeprecatedSVGAnimatedProperty<AnimatedType>& creator, const QualifiedName& attrName)
143 DeprecatedSVGAnimatedTypeWrapperKey key(element, attrName.localName());
144 RefPtr<AnimatedTearOff> wrapper = static_cast<AnimatedTearOff*>(AnimatedTearOff::wrapperCache()->get(key));
147 wrapper = AnimatedTearOff::create(creator, element);
148 AnimatedTearOff::wrapperCache()->set(key, wrapper.get());
151 return wrapper.release();
154 // Common type definitions, to ease IDL generation.
155 typedef DeprecatedSVGAnimatedTemplate<SVGTransformList*> SVGAnimatedTransformList;