2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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.
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.
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.
20 #ifndef SVGAnimatedProperty_h
21 #define SVGAnimatedProperty_h
24 #include "SVGAnimatedPropertyDescription.h"
25 #include "SVGElement.h"
26 #include "SVGPropertyInfo.h"
27 #include <wtf/RefCounted.h>
33 class SVGAnimatedProperty : public RefCounted<SVGAnimatedProperty> {
35 SVGElement* contextElement() const { return m_contextElement.get(); }
36 const QualifiedName& attributeName() const { return m_attributeName; }
40 ASSERT(m_contextElement);
41 m_contextElement->invalidateSVGAttributes();
42 m_contextElement->svgAttributeChanged(m_attributeName);
45 virtual bool isAnimatedListTearOff() const { return false; }
46 virtual void updateAnimVal(void*) { ASSERT_NOT_REACHED(); }
48 // Caching facilities.
49 typedef HashMap<SVGAnimatedPropertyDescription, RefPtr<SVGAnimatedProperty>, SVGAnimatedPropertyDescriptionHash, SVGAnimatedPropertyDescriptionHashTraits> Cache;
51 virtual ~SVGAnimatedProperty()
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);
64 // lookupOrCreateWrapper & helper methods.
65 template<typename TearOffType, typename PropertyType, bool isDerivedFromSVGElement>
66 struct LookupOrCreateHelper;
68 template<typename TearOffType, typename PropertyType>
69 struct LookupOrCreateHelper<TearOffType, PropertyType, false> {
70 static PassRefPtr<TearOffType> lookupOrCreateWrapper(void*, const SVGPropertyInfo*, PropertyType&)
73 return PassRefPtr<TearOffType>();
77 template<typename TearOffType, typename PropertyType>
78 struct LookupOrCreateHelper<TearOffType, PropertyType, true> {
79 static PassRefPtr<TearOffType> lookupOrCreateWrapper(SVGElement* element, const SVGPropertyInfo* info, PropertyType& property)
82 SVGAnimatedPropertyDescription key(element, info->propertyIdentifier);
83 RefPtr<SVGAnimatedProperty> wrapper = animatedPropertyCache()->get(key);
85 wrapper = TearOffType::create(element, info->attributeName, property);
86 animatedPropertyCache()->set(key, wrapper);
88 return static_pointer_cast<TearOffType>(wrapper).release();
92 template<typename OwnerType, typename TearOffType, typename PropertyType, bool isDerivedFromSVGElement>
93 static PassRefPtr<TearOffType> lookupOrCreateWrapper(OwnerType* element, const SVGPropertyInfo* info, PropertyType& property)
95 return LookupOrCreateHelper<TearOffType, PropertyType, isDerivedFromSVGElement>::lookupOrCreateWrapper(element, info, property);
98 // lookupWrapper & helper methods.
99 template<typename TearOffType, bool isDerivedFromSVGElement>
102 template<typename TearOffType>
103 struct LookupHelper<TearOffType, false> {
104 static TearOffType* lookupWrapper(const void*, const SVGPropertyInfo*)
110 template<typename TearOffType>
111 struct LookupHelper<TearOffType, true> {
112 static TearOffType* lookupWrapper(const SVGElement* element, const SVGPropertyInfo* info)
115 SVGAnimatedPropertyDescription key(const_cast<SVGElement*>(element), info->propertyIdentifier);
116 return static_pointer_cast<TearOffType>(animatedPropertyCache()->get(key)).get();
120 template<typename OwnerType, typename TearOffType, bool isDerivedFromSVGElement>
121 static TearOffType* lookupWrapper(const OwnerType* element, const SVGPropertyInfo* info)
123 return LookupHelper<TearOffType, isDerivedFromSVGElement>::lookupWrapper(element, info);
127 SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName)
128 : m_contextElement(contextElement)
129 , m_attributeName(attributeName)
134 static Cache* animatedPropertyCache()
136 static Cache* s_cache = new Cache;
140 RefPtr<SVGElement> m_contextElement;
141 const QualifiedName& m_attributeName;
146 #endif // ENABLE(SVG)
147 #endif // SVGAnimatedProperty_h