KHTML
SVGAnimatedTemplate.h
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org> 00003 2004, 2005 Rob Buis <buis@kde.org> 00004 00005 This file is part of the KDE project 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #ifndef SVGAnimatedTemplate_h 00024 #define SVGAnimatedTemplate_h 00025 00026 #if ENABLE(SVG) 00027 #include <wtf/RefCounted.h> 00028 //#include "AtomicString.h" 00029 //#include "Attribute.h" 00030 #include <wtf/HashTraits.h> 00031 #include <wtf/HashMap.h> 00032 00033 00034 namespace WebCore { 00035 00036 class FloatRect; 00037 class SVGAngle; 00038 class SVGElement; 00039 class SVGLength; 00040 class SVGLengthList; 00041 class SVGNumberList; 00042 class SVGPreserveAspectRatio; 00043 class SVGTransformList; 00044 //class String; 00045 //class QualifiedName; 00046 00047 struct SVGAnimatedTypeWrapperKey { 00048 // Empty value 00049 SVGAnimatedTypeWrapperKey() 00050 : element(0) 00051 , attributeName(0) 00052 { } 00053 00054 // Deleted value 00055 SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType) 00056 : element(reinterpret_cast<SVGElement*>(-1)) 00057 { 00058 } 00059 bool isHashTableDeletedValue() const 00060 { 00061 return element == reinterpret_cast<SVGElement*>(-1); 00062 } 00063 00064 SVGAnimatedTypeWrapperKey(const SVGElement* _element, const AtomicString& _attributeName) 00065 : element(_element) 00066 , attributeName(_attributeName.impl()) 00067 { 00068 ASSERT(element); 00069 ASSERT(attributeName); 00070 } 00071 00072 bool operator==(const SVGAnimatedTypeWrapperKey& other) const 00073 { 00074 return element == other.element && attributeName == other.attributeName; 00075 } 00076 00077 const SVGElement* element; 00078 AtomicStringImpl* attributeName; 00079 }; 00080 00081 struct SVGAnimatedTypeWrapperKeyHash { 00082 static unsigned hash(const SVGAnimatedTypeWrapperKey& key) 00083 { 00084 return StringImpl::computeHash(reinterpret_cast<const UChar*>(&key), sizeof(SVGAnimatedTypeWrapperKey) / sizeof(UChar)); 00085 } 00086 00087 static bool equal(const SVGAnimatedTypeWrapperKey& a, const SVGAnimatedTypeWrapperKey& b) 00088 { 00089 return a == b; 00090 } 00091 00092 static const bool safeToCompareToEmptyOrDeleted = true; 00093 }; 00094 00095 struct SVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<SVGAnimatedTypeWrapperKey> { 00096 static const bool emptyValueIsZero = true; 00097 00098 static void constructDeletedValue(SVGAnimatedTypeWrapperKey* slot) 00099 { 00100 new (slot) SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue); 00101 } 00102 static bool isDeletedValue(const SVGAnimatedTypeWrapperKey& value) 00103 { 00104 return value.isHashTableDeletedValue(); 00105 } 00106 }; 00107 00108 template<typename BareType> 00109 class SVGAnimatedTemplate : public RefCounted<SVGAnimatedTemplate<BareType> > { 00110 public: 00111 SVGAnimatedTemplate(const QualifiedName& attributeName) 00112 : RefCounted<SVGAnimatedTemplate<BareType> >(0) 00113 , m_associatedAttributeName(attributeName) 00114 { 00115 } 00116 00117 virtual ~SVGAnimatedTemplate() { forgetWrapper(this); } 00118 00119 virtual BareType baseVal() const = 0; 00120 virtual void setBaseVal(BareType newBaseVal) = 0; 00121 00122 virtual BareType animVal() const = 0; 00123 virtual void setAnimVal(BareType newAnimVal) = 0; 00124 00125 typedef HashMap<SVGAnimatedTypeWrapperKey, SVGAnimatedTemplate<BareType>*, SVGAnimatedTypeWrapperKeyHash, SVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap; 00126 typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator; 00127 00128 static ElementToWrapperMap* wrapperCache() 00129 { 00130 static ElementToWrapperMap* s_wrapperCache = new ElementToWrapperMap; 00131 return s_wrapperCache; 00132 } 00133 00134 static void forgetWrapper(SVGAnimatedTemplate<BareType>* wrapper) 00135 { 00136 ElementToWrapperMap* cache = wrapperCache(); 00137 ElementToWrapperMapIterator itr = cache->begin(); 00138 ElementToWrapperMapIterator end = cache->end(); 00139 for (; itr != end; ++itr) { 00140 if (itr->second == wrapper) { 00141 cache->remove(itr->first); 00142 break; 00143 } 00144 } 00145 } 00146 00147 const QualifiedName& associatedAttributeName() const { return m_associatedAttributeName; } 00148 00149 private: 00150 const QualifiedName& m_associatedAttributeName; 00151 }; 00152 00153 template <class Type, class SVGElementSubClass> 00154 Type* lookupOrCreateWrapper(const SVGElementSubClass* element, const QualifiedName& domAttrName, const AtomicString& attrIdentifier) { 00155 SVGAnimatedTypeWrapperKey key(element, attrIdentifier); 00156 Type* wrapper = static_cast<Type*>(Type::wrapperCache()->get(key)); 00157 if (!wrapper) { 00158 wrapper = new Type(element, domAttrName); 00159 Type::wrapperCache()->set(key, wrapper); 00160 } 00161 return wrapper; 00162 } 00163 00164 // Common type definitions, to ease IDL generation... 00165 typedef SVGAnimatedTemplate<SVGAngle*> SVGAnimatedAngle; 00166 typedef SVGAnimatedTemplate<bool> SVGAnimatedBoolean; 00167 typedef SVGAnimatedTemplate<int> SVGAnimatedEnumeration; 00168 typedef SVGAnimatedTemplate<long> SVGAnimatedInteger; 00169 typedef SVGAnimatedTemplate<SVGLength> SVGAnimatedLength; 00170 typedef SVGAnimatedTemplate<SVGLengthList*> SVGAnimatedLengthList; 00171 typedef SVGAnimatedTemplate<float> SVGAnimatedNumber; 00172 typedef SVGAnimatedTemplate<SVGNumberList*> SVGAnimatedNumberList; 00173 typedef SVGAnimatedTemplate<SVGPreserveAspectRatio*> SVGAnimatedPreserveAspectRatio; 00174 typedef SVGAnimatedTemplate<FloatRect> SVGAnimatedRect; 00175 typedef SVGAnimatedTemplate<String> SVGAnimatedString; 00176 typedef SVGAnimatedTemplate<SVGTransformList*> SVGAnimatedTransformList; 00177 } 00178 00179 #endif // ENABLE(SVG) 00180 #endif // SVGAnimatedTemplate_h
KDE 4.6 API Reference