Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / svg / SVGAnimatedTemplate.h
blobd65fe0bdb6317e173b35436b1705fe4aa087ee85
1 /*
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 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 SVGAnimatedTemplate_h
22 #define SVGAnimatedTemplate_h
24 #if ENABLE(SVG)
25 #include "AtomicString.h"
26 #include "FloatRect.h"
27 #include "SVGLength.h"
28 #include <wtf/HashMap.h>
30 namespace WebCore {
32 class SVGAngle;
33 class SVGElement;
34 class SVGLengthList;
35 class SVGNumberList;
36 class SVGPreserveAspectRatio;
37 class SVGTransformList;
38 class String;
39 class QualifiedName;
41 struct SVGAnimatedTypeWrapperKey {
42 // Empty value
43 SVGAnimatedTypeWrapperKey()
44 : element(0)
45 , attributeName(0)
46 { }
48 // Deleted value
49 SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
50 : element(reinterpret_cast<SVGElement*>(-1))
54 bool isHashTableDeletedValue() const
56 return element == reinterpret_cast<SVGElement*>(-1);
59 SVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName)
60 : element(_element)
61 , attributeName(_attributeName.impl())
63 ASSERT(element);
64 ASSERT(attributeName);
67 bool operator==(const SVGAnimatedTypeWrapperKey& other) const
69 return element == other.element && attributeName == other.attributeName;
72 const SVGElement* element;
73 AtomicStringImpl* attributeName;
76 struct SVGAnimatedTypeWrapperKeyHash {
77 static unsigned hash(const SVGAnimatedTypeWrapperKey& key)
79 return StringImpl::computeHash(reinterpret_cast<const UChar*>(&key), sizeof(SVGAnimatedTypeWrapperKey) / sizeof(UChar));
82 static bool equal(const SVGAnimatedTypeWrapperKey& a, const SVGAnimatedTypeWrapperKey& b)
84 return a == b;
87 static const bool safeToCompareToEmptyOrDeleted = true;
90 struct SVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<SVGAnimatedTypeWrapperKey> {
91 static const bool emptyValueIsZero = true;
93 static void constructDeletedValue(SVGAnimatedTypeWrapperKey& slot)
95 new (&slot) SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
98 static bool isDeletedValue(const SVGAnimatedTypeWrapperKey& value)
100 return value.isHashTableDeletedValue();
104 template<typename BareType>
105 class SVGAnimatedTemplate : public RefCounted<SVGAnimatedTemplate<BareType> > {
106 public:
107 virtual ~SVGAnimatedTemplate() { forgetWrapper(this); }
109 virtual BareType baseVal() const = 0;
110 virtual void setBaseVal(BareType) = 0;
112 virtual BareType animVal() const = 0;
113 virtual void setAnimVal(BareType) = 0;
115 typedef HashMap<SVGAnimatedTypeWrapperKey, SVGAnimatedTemplate<BareType>*, SVGAnimatedTypeWrapperKeyHash, SVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
116 typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
118 static ElementToWrapperMap* wrapperCache()
120 static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap;
121 return s_wrapperCache;
124 static void forgetWrapper(SVGAnimatedTemplate<BareType>* wrapper)
126 ElementToWrapperMap* cache = wrapperCache();
127 ElementToWrapperMapIterator itr = cache->begin();
128 ElementToWrapperMapIterator end = cache->end();
129 for (; itr != end; ++itr) {
130 if (itr->second == wrapper) {
131 cache->remove(itr->first);
132 break;
137 const QualifiedName& associatedAttributeName() const { return m_associatedAttributeName; }
139 protected:
140 SVGAnimatedTemplate(const QualifiedName& attributeName)
141 : m_associatedAttributeName(attributeName)
145 private:
146 const QualifiedName& m_associatedAttributeName;
149 template<typename OwnerTypeArg, typename AnimatedTypeArg, const char* TagName, const char* PropertyName>
150 class SVGAnimatedProperty;
152 template<typename OwnerType, typename AnimatedType, const char* TagName, const char* PropertyName, typename Type, typename OwnerElement>
153 PassRefPtr<Type> lookupOrCreateWrapper(const SVGAnimatedProperty<OwnerType, AnimatedType, TagName, PropertyName>& creator,
154 const OwnerElement* element, const QualifiedName& attrName, const AtomicString& attrIdentifier)
156 SVGAnimatedTypeWrapperKey key(element, attrIdentifier);
157 RefPtr<Type> wrapper = static_cast<Type*>(Type::wrapperCache()->get(key));
159 if (!wrapper) {
160 wrapper = Type::create(creator, element, attrName);
161 element->propertyController().setPropertyNeedsSynchronization(attrName);
162 Type::wrapperCache()->set(key, wrapper.get());
165 return wrapper.release();
168 // Default implementation for pointer types
169 template<typename Type>
170 struct SVGAnimatedTypeValue : Noncopyable {
171 typedef RefPtr<Type> StorableType;
172 typedef Type* DecoratedType;
174 static Type null() { return 0; }
175 static String toString(Type type) { return type ? type->valueAsString() : String(); }
178 template<>
179 struct SVGAnimatedTypeValue<bool> : Noncopyable {
180 typedef bool StorableType;
181 typedef bool DecoratedType;
183 static bool null() { return false; }
184 static String toString(bool type) { return type ? "true" : "false"; }
187 template<>
188 struct SVGAnimatedTypeValue<int> : Noncopyable {
189 typedef int StorableType;
190 typedef int DecoratedType;
192 static int null() { return 0; }
193 static String toString(int type) { return String::number(type); }
196 template<>
197 struct SVGAnimatedTypeValue<long> : Noncopyable {
198 typedef long StorableType;
199 typedef long DecoratedType;
201 static long null() { return 0l; }
202 static String toString(long type) { return String::number(type); }
205 template<>
206 struct SVGAnimatedTypeValue<SVGLength> : Noncopyable {
207 typedef SVGLength StorableType;
208 typedef SVGLength DecoratedType;
210 static SVGLength null() { return SVGLength(); }
211 static String toString(const SVGLength& type) { return type.valueAsString(); }
214 template<>
215 struct SVGAnimatedTypeValue<float> : Noncopyable {
216 typedef float StorableType;
217 typedef float DecoratedType;
219 static float null() { return 0.0f; }
220 static String toString(float type) { return String::number(type); }
223 template<>
224 struct SVGAnimatedTypeValue<FloatRect> : Noncopyable {
225 typedef FloatRect StorableType;
226 typedef FloatRect DecoratedType;
228 static FloatRect null() { return FloatRect(); }
229 static String toString(const FloatRect& type) { return String::format("%f %f %f %f", type.x(), type.y(), type.width(), type.height()); }
232 template<>
233 struct SVGAnimatedTypeValue<String> : Noncopyable {
234 typedef String StorableType;
235 typedef String DecoratedType;
237 static String null() { return String(); }
238 static String toString(const String& type) { return type; }
241 // Common type definitions, to ease IDL generation.
242 typedef SVGAnimatedTemplate<SVGAngle*> SVGAnimatedAngle;
243 typedef SVGAnimatedTemplate<bool> SVGAnimatedBoolean;
244 typedef SVGAnimatedTemplate<int> SVGAnimatedEnumeration;
245 typedef SVGAnimatedTemplate<long> SVGAnimatedInteger;
246 typedef SVGAnimatedTemplate<SVGLength> SVGAnimatedLength;
247 typedef SVGAnimatedTemplate<SVGLengthList*> SVGAnimatedLengthList;
248 typedef SVGAnimatedTemplate<float> SVGAnimatedNumber;
249 typedef SVGAnimatedTemplate<SVGNumberList*> SVGAnimatedNumberList;
250 typedef SVGAnimatedTemplate<SVGPreserveAspectRatio*> SVGAnimatedPreserveAspectRatio;
251 typedef SVGAnimatedTemplate<FloatRect> SVGAnimatedRect;
252 typedef SVGAnimatedTemplate<String> SVGAnimatedString;
253 typedef SVGAnimatedTemplate<SVGTransformList*> SVGAnimatedTransformList;
257 #endif // ENABLE(SVG)
258 #endif // SVGAnimatedTemplate_h