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>
35 class SVGPreserveAspectRatio;
36 class SVGTransformList;
39 struct DeprecatedSVGAnimatedTypeWrapperKey {
41 DeprecatedSVGAnimatedTypeWrapperKey()
47 DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
48 : element(reinterpret_cast<SVGElement*>(-1))
52 bool isHashTableDeletedValue() const
54 return element == reinterpret_cast<SVGElement*>(-1);
57 DeprecatedSVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName)
59 , attributeName(_attributeName.impl())
62 ASSERT(attributeName);
65 bool operator==(const DeprecatedSVGAnimatedTypeWrapperKey& other) const
67 return element == other.element && attributeName == other.attributeName;
70 const SVGElement* element;
71 AtomicStringImpl* attributeName;
74 struct DeprecatedSVGAnimatedTypeWrapperKeyHash {
75 static unsigned hash(const DeprecatedSVGAnimatedTypeWrapperKey& key)
77 return StringImpl::computeHash(reinterpret_cast<const UChar*>(&key), sizeof(DeprecatedSVGAnimatedTypeWrapperKey) / sizeof(UChar));
80 static bool equal(const DeprecatedSVGAnimatedTypeWrapperKey& a, const DeprecatedSVGAnimatedTypeWrapperKey& b)
85 static const bool safeToCompareToEmptyOrDeleted = true;
88 struct DeprecatedSVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<DeprecatedSVGAnimatedTypeWrapperKey> {
89 static const bool emptyValueIsZero = true;
91 static void constructDeletedValue(DeprecatedSVGAnimatedTypeWrapperKey& slot)
93 new (&slot) DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
96 static bool isDeletedValue(const DeprecatedSVGAnimatedTypeWrapperKey& value)
98 return value.isHashTableDeletedValue();
102 template<typename AnimatedType>
103 class DeprecatedSVGAnimatedTemplate : public RefCounted<DeprecatedSVGAnimatedTemplate<AnimatedType> > {
105 typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::PassType PassType;
106 typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::ReturnType ReturnType;
108 virtual ~DeprecatedSVGAnimatedTemplate() { forgetWrapper(this); }
110 virtual ReturnType baseVal() const = 0;
111 virtual void setBaseVal(PassType) = 0;
113 virtual ReturnType animVal() const = 0;
114 virtual void setAnimVal(PassType) = 0;
116 virtual const QualifiedName& associatedAttributeName() const = 0;
118 typedef HashMap<DeprecatedSVGAnimatedTypeWrapperKey, DeprecatedSVGAnimatedTemplate<AnimatedType>*, DeprecatedSVGAnimatedTypeWrapperKeyHash, DeprecatedSVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
119 typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
121 static ElementToWrapperMap* wrapperCache()
123 static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap;
124 return s_wrapperCache;
127 static void forgetWrapper(DeprecatedSVGAnimatedTemplate<AnimatedType>* wrapper)
129 ElementToWrapperMap* cache = wrapperCache();
130 ElementToWrapperMapIterator itr = cache->begin();
131 ElementToWrapperMapIterator end = cache->end();
132 for (; itr != end; ++itr) {
133 if (itr->second == wrapper) {
134 cache->remove(itr->first);
141 template<typename AnimatedType>
142 class DeprecatedSVGAnimatedProperty;
144 template<typename AnimatedType, typename AnimatedTearOff>
145 PassRefPtr<AnimatedTearOff> lookupOrCreateWrapper(SVGElement* element, DeprecatedSVGAnimatedProperty<AnimatedType>& creator, const QualifiedName& attrName)
147 DeprecatedSVGAnimatedTypeWrapperKey key(element, attrName.localName());
148 RefPtr<AnimatedTearOff> wrapper = static_cast<AnimatedTearOff*>(AnimatedTearOff::wrapperCache()->get(key));
151 wrapper = AnimatedTearOff::create(creator, element);
152 AnimatedTearOff::wrapperCache()->set(key, wrapper.get());
155 return wrapper.release();
158 // Common type definitions, to ease IDL generation.
159 typedef DeprecatedSVGAnimatedTemplate<SVGAngle> SVGAnimatedAngle;
160 typedef DeprecatedSVGAnimatedTemplate<bool> SVGAnimatedBoolean;
161 typedef DeprecatedSVGAnimatedTemplate<int> SVGAnimatedEnumeration;
162 typedef DeprecatedSVGAnimatedTemplate<long> SVGAnimatedInteger;
163 typedef DeprecatedSVGAnimatedTemplate<SVGLength> SVGAnimatedLength;
164 typedef DeprecatedSVGAnimatedTemplate<SVGLengthList*> SVGAnimatedLengthList;
165 typedef DeprecatedSVGAnimatedTemplate<float> SVGAnimatedNumber;
166 typedef DeprecatedSVGAnimatedTemplate<SVGNumberList*> SVGAnimatedNumberList;
167 typedef DeprecatedSVGAnimatedTemplate<SVGPreserveAspectRatio> SVGAnimatedPreserveAspectRatio;
168 typedef DeprecatedSVGAnimatedTemplate<FloatRect> SVGAnimatedRect;
169 typedef DeprecatedSVGAnimatedTemplate<String> SVGAnimatedString;
170 typedef DeprecatedSVGAnimatedTemplate<SVGTransformList*> SVGAnimatedTransformList;