initial import
[vuplus_webkit] / Source / WebCore / svg / properties / SVGAnimatedProperty.h
1 /*
2  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef SVGAnimatedProperty_h
21 #define SVGAnimatedProperty_h
22
23 #if ENABLE(SVG)
24 #include "SVGAnimatedPropertyDescription.h"
25 #include "SVGElement.h"
26 #include "SVGPropertyInfo.h"
27 #include <wtf/RefCounted.h>
28
29 namespace WebCore {
30
31 class SVGElement;
32
33 class SVGAnimatedProperty : public RefCounted<SVGAnimatedProperty> {
34 public:
35     SVGElement* contextElement() const { return m_contextElement.get(); }
36     const QualifiedName& attributeName() const { return m_attributeName; }
37
38     void commitChange()
39     {
40         ASSERT(m_contextElement);
41         m_contextElement->invalidateSVGAttributes();
42         m_contextElement->svgAttributeChanged(m_attributeName);
43     }
44
45     virtual bool isAnimatedListTearOff() const { return false; }
46     virtual void updateAnimVal(void*) { ASSERT_NOT_REACHED(); }
47
48     // Caching facilities.
49     typedef HashMap<SVGAnimatedPropertyDescription, RefPtr<SVGAnimatedProperty>, SVGAnimatedPropertyDescriptionHash, SVGAnimatedPropertyDescriptionHashTraits> Cache;
50
51     virtual ~SVGAnimatedProperty()
52     {
53         // Remove wrapper from cache.
54         Cache* cache = animatedPropertyCache();
55         const Cache::const_iterator end = cache->end();
56         for (Cache::const_iterator it = cache->begin(); it != end; ++it) {
57             if (it->second == this) {
58                 cache->remove(it->first);
59                 break;
60             }
61         }
62     }
63
64     // lookupOrCreateWrapper & helper methods.
65     template<typename TearOffType, typename PropertyType, bool isDerivedFromSVGElement>
66     struct LookupOrCreateHelper;
67
68     template<typename TearOffType, typename PropertyType>
69     struct LookupOrCreateHelper<TearOffType, PropertyType, false> {
70         static PassRefPtr<TearOffType> lookupOrCreateWrapper(void*, const SVGPropertyInfo*, PropertyType&)
71         {
72             ASSERT_NOT_REACHED();
73             return PassRefPtr<TearOffType>();
74         }
75     };
76
77     template<typename TearOffType, typename PropertyType>
78     struct LookupOrCreateHelper<TearOffType, PropertyType, true> {
79         static PassRefPtr<TearOffType> lookupOrCreateWrapper(SVGElement* element, const SVGPropertyInfo* info, PropertyType& property)
80         {
81             ASSERT(info);
82             SVGAnimatedPropertyDescription key(element, info->propertyIdentifier);
83             RefPtr<SVGAnimatedProperty> wrapper = animatedPropertyCache()->get(key);
84             if (!wrapper) {
85                 wrapper = TearOffType::create(element, info->attributeName, property);
86                 animatedPropertyCache()->set(key, wrapper);
87             }
88             return static_pointer_cast<TearOffType>(wrapper).release();
89         }
90     };
91
92     template<typename OwnerType, typename TearOffType, typename PropertyType, bool isDerivedFromSVGElement>
93     static PassRefPtr<TearOffType> lookupOrCreateWrapper(OwnerType* element, const SVGPropertyInfo* info, PropertyType& property)
94     {
95         return LookupOrCreateHelper<TearOffType, PropertyType, isDerivedFromSVGElement>::lookupOrCreateWrapper(element, info, property);
96     }
97
98     // lookupWrapper & helper methods.
99     template<typename TearOffType, bool isDerivedFromSVGElement>
100     struct LookupHelper;
101
102     template<typename TearOffType>
103     struct LookupHelper<TearOffType, false> {
104         static TearOffType* lookupWrapper(const void*, const SVGPropertyInfo*)
105         {
106             return 0;
107         }
108     };
109
110     template<typename TearOffType>
111     struct LookupHelper<TearOffType, true> {
112         static TearOffType* lookupWrapper(const SVGElement* element, const SVGPropertyInfo* info)
113         {
114             ASSERT(info);
115             SVGAnimatedPropertyDescription key(const_cast<SVGElement*>(element), info->propertyIdentifier);
116             return static_pointer_cast<TearOffType>(animatedPropertyCache()->get(key)).get();
117         }
118     };
119
120     template<typename OwnerType, typename TearOffType, bool isDerivedFromSVGElement>
121     static TearOffType* lookupWrapper(const OwnerType* element, const SVGPropertyInfo* info)
122     {
123         return LookupHelper<TearOffType, isDerivedFromSVGElement>::lookupWrapper(element, info);
124     }
125
126 protected:
127     SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName)
128         : m_contextElement(contextElement)
129         , m_attributeName(attributeName)
130     {
131     }
132
133 private:
134     static Cache* animatedPropertyCache()
135     {
136         static Cache* s_cache = new Cache;                
137         return s_cache;
138     }
139
140     RefPtr<SVGElement> m_contextElement;
141     const QualifiedName& m_attributeName;
142 };
143
144 }
145
146 #endif // ENABLE(SVG)
147 #endif // SVGAnimatedProperty_h