File indexing completed on 2024-04-28 15:24:27

0001 /*
0002     Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
0003                   2004, 2005 Rob Buis <buis@kde.org>
0004 
0005     This file is part of the KDE project
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef SVGAnimatedTemplate_h
0024 #define SVGAnimatedTemplate_h
0025 
0026 #if ENABLE(SVG)
0027 #include <wtf/RefCounted.h>
0028 //#include "AtomicString.h"
0029 //#include "Attribute.h"
0030 #include <wtf/HashTraits.h>
0031 #include <wtf/HashMap.h>
0032 
0033 namespace WebCore
0034 {
0035 
0036 class FloatRect;
0037 class SVGAngle;
0038 class SVGElement;
0039 class SVGLength;
0040 class SVGLengthList;
0041 class SVGNumberList;
0042 class SVGPreserveAspectRatio;
0043 class SVGTransformList;
0044 //class String;
0045 //class QualifiedName;
0046 
0047 struct SVGAnimatedTypeWrapperKey {
0048     // Empty value
0049     SVGAnimatedTypeWrapperKey()
0050         : element(nullptr)
0051         , attributeName(nullptr)
0052     { }
0053 
0054     // Deleted value
0055     SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValueType)
0056         : element(reinterpret_cast<SVGElement *>(-1))
0057     {
0058     }
0059     bool isHashTableDeletedValue() const
0060     {
0061         return element == reinterpret_cast<SVGElement *>(-1);
0062     }
0063 
0064     SVGAnimatedTypeWrapperKey(const SVGElement *_element, const AtomicString &_attributeName)
0065         : element(_element)
0066         , attributeName(_attributeName.impl())
0067     {
0068         ASSERT(element);
0069         ASSERT(attributeName);
0070     }
0071 
0072     bool operator==(const SVGAnimatedTypeWrapperKey &other) const
0073     {
0074         return element == other.element && attributeName == other.attributeName;
0075     }
0076 
0077     const SVGElement *element;
0078     AtomicStringImpl *attributeName;
0079 };
0080 
0081 struct SVGAnimatedTypeWrapperKeyHash {
0082     static unsigned hash(const SVGAnimatedTypeWrapperKey &key)
0083     {
0084         return StringImpl::computeHash(reinterpret_cast<const UChar *>(&key), sizeof(SVGAnimatedTypeWrapperKey) / sizeof(UChar));
0085     }
0086 
0087     static bool equal(const SVGAnimatedTypeWrapperKey &a, const SVGAnimatedTypeWrapperKey &b)
0088     {
0089         return a == b;
0090     }
0091 
0092     static const bool safeToCompareToEmptyOrDeleted = true;
0093 };
0094 
0095 struct SVGAnimatedTypeWrapperKeyHashTraits : WTF::GenericHashTraits<SVGAnimatedTypeWrapperKey> {
0096     static const bool emptyValueIsZero = true;
0097 
0098     static void constructDeletedValue(SVGAnimatedTypeWrapperKey *slot)
0099     {
0100         new(slot) SVGAnimatedTypeWrapperKey(WTF::HashTableDeletedValue);
0101     }
0102     static bool isDeletedValue(const SVGAnimatedTypeWrapperKey &value)
0103     {
0104         return value.isHashTableDeletedValue();
0105     }
0106 };
0107 
0108 template<typename BareType>
0109 class SVGAnimatedTemplate : public RefCounted<SVGAnimatedTemplate<BareType> >
0110 {
0111 public:
0112     SVGAnimatedTemplate(const QualifiedName &attributeName)
0113         : RefCounted<SVGAnimatedTemplate<BareType> >(0)
0114         , m_associatedAttributeName(attributeName)
0115     {
0116     }
0117 
0118     virtual ~SVGAnimatedTemplate()
0119     {
0120         forgetWrapper(this);
0121     }
0122 
0123     virtual BareType baseVal() const = 0;
0124     virtual void setBaseVal(BareType newBaseVal) = 0;
0125 
0126     virtual BareType animVal() const = 0;
0127     virtual void setAnimVal(BareType newAnimVal) = 0;
0128 
0129     typedef HashMap<SVGAnimatedTypeWrapperKey, SVGAnimatedTemplate<BareType>*, SVGAnimatedTypeWrapperKeyHash, SVGAnimatedTypeWrapperKeyHashTraits > ElementToWrapperMap;
0130     typedef typename ElementToWrapperMap::const_iterator ElementToWrapperMapIterator;
0131 
0132     static ElementToWrapperMap *wrapperCache()
0133     {
0134         static ElementToWrapperMap *s_wrapperCache = new ElementToWrapperMap;
0135         return s_wrapperCache;
0136     }
0137 
0138     static void forgetWrapper(SVGAnimatedTemplate<BareType> *wrapper)
0139     {
0140         ElementToWrapperMap *cache = wrapperCache();
0141         ElementToWrapperMapIterator itr = cache->begin();
0142         ElementToWrapperMapIterator end = cache->end();
0143         for (; itr != end; ++itr) {
0144             if (itr->second == wrapper) {
0145                 cache->remove(itr->first);
0146                 break;
0147             }
0148         }
0149     }
0150 
0151     const QualifiedName &associatedAttributeName() const
0152     {
0153         return m_associatedAttributeName;
0154     }
0155 
0156 private:
0157     const QualifiedName &m_associatedAttributeName;
0158 };
0159 
0160 template <class Type, class SVGElementSubClass>
0161 Type *lookupOrCreateWrapper(const SVGElementSubClass *element, const QualifiedName &domAttrName, const AtomicString &attrIdentifier)
0162 {
0163     SVGAnimatedTypeWrapperKey key(element, attrIdentifier);
0164     Type *wrapper = static_cast<Type *>(Type::wrapperCache()->get(key));
0165     if (!wrapper) {
0166         wrapper = new Type(element, domAttrName);
0167         Type::wrapperCache()->set(key, wrapper);
0168     }
0169     return wrapper;
0170 }
0171 
0172 // Common type definitions, to ease IDL generation...
0173 typedef SVGAnimatedTemplate<SVGAngle *> SVGAnimatedAngle;
0174 typedef SVGAnimatedTemplate<bool> SVGAnimatedBoolean;
0175 typedef SVGAnimatedTemplate<int> SVGAnimatedEnumeration;
0176 typedef SVGAnimatedTemplate<long> SVGAnimatedInteger;
0177 typedef SVGAnimatedTemplate<SVGLength> SVGAnimatedLength;
0178 typedef SVGAnimatedTemplate<SVGLengthList *> SVGAnimatedLengthList;
0179 typedef SVGAnimatedTemplate<float> SVGAnimatedNumber;
0180 typedef SVGAnimatedTemplate<SVGNumberList *> SVGAnimatedNumberList;
0181 typedef SVGAnimatedTemplate<SVGPreserveAspectRatio *> SVGAnimatedPreserveAspectRatio;
0182 typedef SVGAnimatedTemplate<FloatRect> SVGAnimatedRect;
0183 typedef SVGAnimatedTemplate<String> SVGAnimatedString;
0184 typedef SVGAnimatedTemplate<SVGTransformList *> SVGAnimatedTransformList;
0185 }
0186 
0187 #endif // ENABLE(SVG)
0188 #endif // SVGAnimatedTemplate_h