OSDN Git Service

Merge WebKit at r71558: 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 "QualifiedName.h"
27 #include <wtf/Forward.h>
28 #include <wtf/HashMap.h>
29
30 namespace WebCore {
31    
32     class SVGElement;
33     class SVGTransformList;
34
35     struct DeprecatedSVGAnimatedTypeWrapperKey {            
36         // Empty value
37         DeprecatedSVGAnimatedTypeWrapperKey()
38             : element(0)
39             , attributeName(0)
40         { }
41
42         // Deleted value
43         DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
44             : element(reinterpret_cast<SVGElement*>(-1))
45         {
46         }
47
48         bool isHashTableDeletedValue() const
49         {
50             return element == reinterpret_cast<SVGElement*>(-1);
51         }
52
53         DeprecatedSVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName)
54             : element(_element)
55             , attributeName(_attributeName.impl())
56         {
57             ASSERT(element);
58             ASSERT(attributeName);
59         }
60
61         bool operator==(const DeprecatedSVGAnimatedTypeWrapperKey& other) const
62         {
63             return element == other.element && attributeName == other.attributeName;
64         }
65
66         const SVGElement* element;
67         AtomicStringImpl* attributeName;
68     };
69     
70     struct DeprecatedSVGAnimatedTypeWrapperKeyHash {
71         static unsigned hash(const DeprecatedSVGAnimatedTypeWrapperKey& key)
72         {
73             return WTF::StringHasher::createBlobHash<sizeof(DeprecatedSVGAnimatedTypeWrapperKey)>(&key);
74         }
75
76         static bool equal(const DeprecatedSVGAnimatedTypeWrapperKey& a, const DeprecatedSVGAnimatedTypeWrapperKey& b)
77         {
78             return a == b;
79         }
80
81         static const bool safeToCompareToEmptyOrDeleted = true;
82     };
83
84     struct DeprecatedSVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<DeprecatedSVGAnimatedTypeWrapperKey> {
85         static const bool emptyValueIsZero = true;
86
87         static void constructDeletedValue(DeprecatedSVGAnimatedTypeWrapperKey& slot)
88         {
89             new (&slot) DeprecatedSVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
90         }
91
92         static bool isDeletedValue(const DeprecatedSVGAnimatedTypeWrapperKey& value)
93         {
94             return value.isHashTableDeletedValue();
95         }
96     };
97  
98     template<typename AnimatedType>
99     class DeprecatedSVGAnimatedTemplate : public RefCounted<DeprecatedSVGAnimatedTemplate<AnimatedType> > {
100     public:
101         typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::PassType PassType;
102         typedef typename DeprecatedSVGAnimatedPropertyTraits<AnimatedType>::ReturnType ReturnType;
103
104         virtual ~DeprecatedSVGAnimatedTemplate() { forgetWrapper(this); }
105
106         virtual ReturnType baseVal() const = 0;
107         virtual void setBaseVal(PassType) = 0;
108
109         virtual ReturnType animVal() const = 0;
110         virtual void setAnimVal(PassType) = 0;
111
112         virtual const QualifiedName& associatedAttributeName() const = 0;
113
114         typedef HashMap<DeprecatedSVGAnimatedTypeWrapperKey, DeprecatedSVGAnimatedTemplate<AnimatedType>*, DeprecatedSVGAnimatedTypeWrapperKeyHash, DeprecatedSVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
115         typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
116
117         static ElementToWrapperMap* wrapperCache()
118         {
119             static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap;                
120             return s_wrapperCache;
121         }
122
123         static void forgetWrapper(DeprecatedSVGAnimatedTemplate<AnimatedType>* wrapper)
124         {
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);
131                     break;
132                 }
133             }
134         }
135     };
136
137     template<typename AnimatedType>
138     class DeprecatedSVGAnimatedProperty;
139
140     template<typename AnimatedType, typename AnimatedTearOff>
141     PassRefPtr<AnimatedTearOff> lookupOrCreateWrapper(SVGElement* element, DeprecatedSVGAnimatedProperty<AnimatedType>& creator, const QualifiedName& attrName)
142     {
143         DeprecatedSVGAnimatedTypeWrapperKey key(element, attrName.localName());
144         RefPtr<AnimatedTearOff> wrapper = static_cast<AnimatedTearOff*>(AnimatedTearOff::wrapperCache()->get(key));
145
146         if (!wrapper) {
147             wrapper = AnimatedTearOff::create(creator, element);
148             AnimatedTearOff::wrapperCache()->set(key, wrapper.get());
149         }
150
151         return wrapper.release();
152     }
153
154     // Common type definitions, to ease IDL generation.
155     typedef DeprecatedSVGAnimatedTemplate<SVGTransformList*> SVGAnimatedTransformList;
156
157 }
158
159 #endif
160 #endif