2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005 Rob Buis <buis@kde.org>
5 This file is part of the KDE project
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #ifndef SVGAnimatedTemplate_h
24 #define SVGAnimatedTemplate_h
27 #include <wtf/RefCounted.h>
28 //#include "AtomicString.h"
29 //#include "Attribute.h"
30 #include <wtf/HashTraits.h>
31 #include <wtf/HashMap.h>
42 class SVGPreserveAspectRatio
;
43 class SVGTransformList
;
45 //class QualifiedName;
47 struct SVGAnimatedTypeWrapperKey
{
49 SVGAnimatedTypeWrapperKey()
55 SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType
)
56 : element(reinterpret_cast<SVGElement
*>(-1))
59 bool isHashTableDeletedValue() const
61 return element
== reinterpret_cast<SVGElement
*>(-1);
64 SVGAnimatedTypeWrapperKey(const SVGElement
* _element
, const AtomicString
& _attributeName
)
66 , attributeName(_attributeName
.impl())
69 ASSERT(attributeName
);
72 bool operator==(const SVGAnimatedTypeWrapperKey
& other
) const
74 return element
== other
.element
&& attributeName
== other
.attributeName
;
77 const SVGElement
* element
;
78 AtomicStringImpl
* attributeName
;
81 struct SVGAnimatedTypeWrapperKeyHash
{
82 static unsigned hash(const SVGAnimatedTypeWrapperKey
& key
)
84 return StringImpl::computeHash(reinterpret_cast<const UChar
*>(&key
), sizeof(SVGAnimatedTypeWrapperKey
) / sizeof(UChar
));
87 static bool equal(const SVGAnimatedTypeWrapperKey
& a
, const SVGAnimatedTypeWrapperKey
& b
)
92 static const bool safeToCompareToEmptyOrDeleted
= true;
95 struct SVGAnimatedTypeWrapperKeyHashTraits
: WTF::GenericHashTraits
<SVGAnimatedTypeWrapperKey
> {
96 static const bool emptyValueIsZero
= true;
98 static void constructDeletedValue(SVGAnimatedTypeWrapperKey
* slot
)
100 new (slot
) SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue
);
102 static bool isDeletedValue(const SVGAnimatedTypeWrapperKey
& value
)
104 return value
.isHashTableDeletedValue();
108 template<typename BareType
>
109 class SVGAnimatedTemplate
: public RefCounted
<SVGAnimatedTemplate
<BareType
> > {
111 SVGAnimatedTemplate(const QualifiedName
& attributeName
)
112 : RefCounted
<SVGAnimatedTemplate
<BareType
> >(0)
113 , m_associatedAttributeName(attributeName
)
117 virtual ~SVGAnimatedTemplate() { forgetWrapper(this); }
119 virtual BareType
baseVal() const = 0;
120 virtual void setBaseVal(BareType newBaseVal
) = 0;
122 virtual BareType
animVal() const = 0;
123 virtual void setAnimVal(BareType newAnimVal
) = 0;
125 typedef HashMap
<SVGAnimatedTypeWrapperKey
, SVGAnimatedTemplate
<BareType
>*, SVGAnimatedTypeWrapperKeyHash
, SVGAnimatedTypeWrapperKeyHashTraits
> ElementToWrapperMap
;
126 typedef typename
ElementToWrapperMap::const_iterator ElementToWrapperMapIterator
;
128 static ElementToWrapperMap
* wrapperCache()
130 static ElementToWrapperMap
* s_wrapperCache
= new ElementToWrapperMap
;
131 return s_wrapperCache
;
134 static void forgetWrapper(SVGAnimatedTemplate
<BareType
>* wrapper
)
136 ElementToWrapperMap
* cache
= wrapperCache();
137 ElementToWrapperMapIterator itr
= cache
->begin();
138 ElementToWrapperMapIterator end
= cache
->end();
139 for (; itr
!= end
; ++itr
) {
140 if (itr
->second
== wrapper
) {
141 cache
->remove(itr
->first
);
147 const QualifiedName
& associatedAttributeName() const { return m_associatedAttributeName
; }
150 const QualifiedName
& m_associatedAttributeName
;
153 template <class Type
, class SVGElementSubClass
>
154 Type
* lookupOrCreateWrapper(const SVGElementSubClass
* element
, const QualifiedName
& domAttrName
, const AtomicString
& attrIdentifier
) {
155 SVGAnimatedTypeWrapperKey
key(element
, attrIdentifier
);
156 Type
* wrapper
= static_cast<Type
*>(Type::wrapperCache()->get(key
));
158 wrapper
= new Type(element
, domAttrName
);
159 Type::wrapperCache()->set(key
, wrapper
);
164 // Common type definitions, to ease IDL generation...
165 typedef SVGAnimatedTemplate
<SVGAngle
*> SVGAnimatedAngle
;
166 typedef SVGAnimatedTemplate
<bool> SVGAnimatedBoolean
;
167 typedef SVGAnimatedTemplate
<int> SVGAnimatedEnumeration
;
168 typedef SVGAnimatedTemplate
<long> SVGAnimatedInteger
;
169 typedef SVGAnimatedTemplate
<SVGLength
> SVGAnimatedLength
;
170 typedef SVGAnimatedTemplate
<SVGLengthList
*> SVGAnimatedLengthList
;
171 typedef SVGAnimatedTemplate
<float> SVGAnimatedNumber
;
172 typedef SVGAnimatedTemplate
<SVGNumberList
*> SVGAnimatedNumberList
;
173 typedef SVGAnimatedTemplate
<SVGPreserveAspectRatio
*> SVGAnimatedPreserveAspectRatio
;
174 typedef SVGAnimatedTemplate
<FloatRect
> SVGAnimatedRect
;
175 typedef SVGAnimatedTemplate
<String
> SVGAnimatedString
;
176 typedef SVGAnimatedTemplate
<SVGTransformList
*> SVGAnimatedTransformList
;
179 #endif // ENABLE(SVG)
180 #endif // SVGAnimatedTemplate_h