OSDN Git Service

Merge Webkit at r70949: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / svg / DeprecatedSVGAnimatedTemplate.h
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifndef DeprecatedSVGAnimatedTemplate_h
22 #define DeprecatedSVGAnimatedTemplate_h
23
24 #if ENABLE(SVG)
25 #include "DeprecatedSVGAnimatedPropertyTraits.h"
26 #include <wtf/Forward.h>
27 #include <wtf/HashMap.h>
28
29 namespace WebCore {
30    
31     class SVGElement;
32     class SVGNumberList;
33     class SVGPreserveAspectRatio;
34     class SVGTransformList;
35     class QualifiedName;
36
37     struct DeprecatedSVGAnimatedTypeWrapperKey {            
38         // Empty value
39         DeprecatedSVGAnimatedTypeWrapperKey()
40             : element(0)
41             , attributeName(0)
42         { }
43
44         // Deleted value
45         DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
46             : element(reinterpret_cast<SVGElement*>(-1))
47         {
48         }
49
50         bool isHashTableDeletedValue() const
51         {
52             return element == reinterpret_cast<SVGElement*>(-1);
53         }
54
55         DeprecatedSVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName)
56             : element(_element)
57             , attributeName(_attributeName.impl())
58         {
59             ASSERT(element);
60             ASSERT(attributeName);
61         }
62
63         bool operator==(const DeprecatedSVGAnimatedTypeWrapperKey& other) const
64         {
65             return element == other.element && attributeName == other.attributeName;
66         }
67
68         const SVGElement* element;
69         AtomicStringImpl* attributeName;
70     };
71     
72     struct DeprecatedSVGAnimatedTypeWrapperKeyHash {
73         static unsigned hash(const DeprecatedSVGAnimatedTypeWrapperKey& key)
74         {
75             return WTF::StringHasher::createBlobHash<sizeof(DeprecatedSVGAnimatedTypeWrapperKey)>(&key);
76         }
77
78         static bool equal(const DeprecatedSVGAnimatedTypeWrapperKey& a, const DeprecatedSVGAnimatedTypeWrapperKey& b)
79         {
80             return a == b;
81         }
82
83         static const bool safeToCompareToEmptyOrDeleted = true;
84     };
85
86     struct DeprecatedSVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<DeprecatedSVGAnimatedTypeWrapperKey> {
87         static const bool emptyValueIsZero = true;
88
89         static void constructDeletedValue(DeprecatedSVGAnimatedTypeWrapperKey& slot)
90         {
91             new (&slot) DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
92         }
93
94         static bool isDeletedValue(const DeprecatedSVGAnimatedTypeWrapperKey& value)
95         {
96             return value.isHashTableDeletedValue();
97         }
98     };
99  
100     template<typename AnimatedType>
101     class DeprecatedSVGAnimatedTemplate : public RefCounted<DeprecatedSVGAnimatedTemplate<AnimatedType> > {
102     public:
103         typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::PassType PassType;
104         typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::ReturnType ReturnType;
105
106         virtual ~DeprecatedSVGAnimatedTemplate() { forgetWrapper(this); }
107
108         virtual ReturnType baseVal() const = 0;
109         virtual void setBaseVal(PassType) = 0;
110
111         virtual ReturnType animVal() const = 0;
112         virtual void setAnimVal(PassType) = 0;
113
114         virtual const QualifiedName& associatedAttributeName() const = 0;
115
116         typedef HashMap<DeprecatedSVGAnimatedTypeWrapperKey, DeprecatedSVGAnimatedTemplate<AnimatedType>*, DeprecatedSVGAnimatedTypeWrapperKeyHash, DeprecatedSVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
117         typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
118
119         static ElementToWrapperMap* wrapperCache()
120         {
121             static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap;                
122             return s_wrapperCache;
123         }
124
125         static void forgetWrapper(DeprecatedSVGAnimatedTemplate<AnimatedType>* wrapper)
126         {
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);
133                     break;
134                 }
135             }
136         }
137     };
138
139     template<typename AnimatedType>
140     class DeprecatedSVGAnimatedProperty;
141
142     template<typename AnimatedType, typename AnimatedTearOff>
143     PassRefPtr<AnimatedTearOff> lookupOrCreateWrapper(SVGElement* element, DeprecatedSVGAnimatedProperty<AnimatedType>& creator, const QualifiedName& attrName)
144     {
145         DeprecatedSVGAnimatedTypeWrapperKey key(element, attrName.localName());
146         RefPtr<AnimatedTearOff> wrapper = static_cast<AnimatedTearOff*>(AnimatedTearOff::wrapperCache()->get(key));
147
148         if (!wrapper) {
149             wrapper = AnimatedTearOff::create(creator, element);
150             AnimatedTearOff::wrapperCache()->set(key, wrapper.get());
151         }
152
153         return wrapper.release();
154     }
155
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;
162
163 }
164
165 #endif
166 #endif