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 <wtf/Forward.h>
27 #include <wtf/HashMap.h>
33 class SVGPreserveAspectRatio;
34 class SVGTransformList;
37 struct DeprecatedSVGAnimatedTypeWrapperKey {
39 DeprecatedSVGAnimatedTypeWrapperKey()
45 DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
46 : element(reinterpret_cast<SVGElement*>(-1))
50 bool isHashTableDeletedValue() const
52 return element == reinterpret_cast<SVGElement*>(-1);
55 DeprecatedSVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName)
57 , attributeName(_attributeName.impl())
60 ASSERT(attributeName);
63 bool operator==(const DeprecatedSVGAnimatedTypeWrapperKey& other) const
65 return element == other.element && attributeName == other.attributeName;
68 const SVGElement* element;
69 AtomicStringImpl* attributeName;
72 struct DeprecatedSVGAnimatedTypeWrapperKeyHash {
73 static unsigned hash(const DeprecatedSVGAnimatedTypeWrapperKey& key)
75 return WTF::StringHasher::createBlobHash<sizeof(DeprecatedSVGAnimatedTypeWrapperKey)>(&key);
78 static bool equal(const DeprecatedSVGAnimatedTypeWrapperKey& a, const DeprecatedSVGAnimatedTypeWrapperKey& b)
83 static const bool safeToCompareToEmptyOrDeleted = true;
86 struct DeprecatedSVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<DeprecatedSVGAnimatedTypeWrapperKey> {
87 static const bool emptyValueIsZero = true;
89 static void constructDeletedValue(DeprecatedSVGAnimatedTypeWrapperKey& slot)
91 new (&slot) DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
94 static bool isDeletedValue(const DeprecatedSVGAnimatedTypeWrapperKey& value)
96 return value.isHashTableDeletedValue();
100 template<typename AnimatedType>
101 class DeprecatedSVGAnimatedTemplate : public RefCounted<DeprecatedSVGAnimatedTemplate<AnimatedType> > {
103 typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::PassType PassType;
104 typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::ReturnType ReturnType;
106 virtual ~DeprecatedSVGAnimatedTemplate() { forgetWrapper(this); }
108 virtual ReturnType baseVal() const = 0;
109 virtual void setBaseVal(PassType) = 0;
111 virtual ReturnType animVal() const = 0;
112 virtual void setAnimVal(PassType) = 0;
114 virtual const QualifiedName& associatedAttributeName() const = 0;
116 typedef HashMap<DeprecatedSVGAnimatedTypeWrapperKey, DeprecatedSVGAnimatedTemplate<AnimatedType>*, DeprecatedSVGAnimatedTypeWrapperKeyHash, DeprecatedSVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
117 typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
119 static ElementToWrapperMap* wrapperCache()
121 static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap;
122 return s_wrapperCache;
125 static void forgetWrapper(DeprecatedSVGAnimatedTemplate<AnimatedType>* wrapper)
127 ElementToWrapperMap* cache = wrapperCache();
128 ElementToWrapperMapIterator itr = cache->begin();
129 ElementToWrapperMapIterator end = cache->end();
130 for (; itr != end; ++itr) {
131 if (itr->second == wrapper) {
132 cache->remove(itr->first);
139 template<typename AnimatedType>
140 class DeprecatedSVGAnimatedProperty;
142 template<typename AnimatedType, typename AnimatedTearOff>
143 PassRefPtr<AnimatedTearOff> lookupOrCreateWrapper(SVGElement* element, DeprecatedSVGAnimatedProperty<AnimatedType>& creator, const QualifiedName& attrName)
145 DeprecatedSVGAnimatedTypeWrapperKey key(element, attrName.localName());
146 RefPtr<AnimatedTearOff> wrapper = static_cast<AnimatedTearOff*>(AnimatedTearOff::wrapperCache()->get(key));
149 wrapper = AnimatedTearOff::create(creator, element);
150 AnimatedTearOff::wrapperCache()->set(key, wrapper.get());
153 return wrapper.release();
156 // Common type definitions, to ease IDL generation.
157 typedef DeprecatedSVGAnimatedTemplate<float> SVGAnimatedNumber;
158 typedef DeprecatedSVGAnimatedTemplate<SVGNumberList*> SVGAnimatedNumberList;
159 typedef DeprecatedSVGAnimatedTemplate<SVGPreserveAspectRatio> SVGAnimatedPreserveAspectRatio;
160 typedef DeprecatedSVGAnimatedTemplate<String> SVGAnimatedString;
161 typedef DeprecatedSVGAnimatedTemplate<SVGTransformList*> SVGAnimatedTransformList;