2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef SVGEnumeration_h
32 #define SVGEnumeration_h
34 #include "core/svg/properties/SVGProperty.h"
38 class SVGEnumerationBase : public SVGPropertyBase {
40 typedef std::pair<unsigned short, String> StringEntry;
41 typedef Vector<StringEntry> StringEntries;
43 // SVGEnumeration does not have a tear-off type.
44 typedef void TearOffType;
45 typedef unsigned short PrimitiveType;
47 virtual ~SVGEnumerationBase();
49 unsigned short value() const { return m_value <= maxExposedEnumValue() ? m_value : 0; }
50 void setValue(unsigned short, ExceptionState&);
53 virtual PassRefPtr<SVGEnumerationBase> clone() const = 0;
54 virtual PassRefPtr<SVGPropertyBase> cloneForAnimation(const String&) const OVERRIDE;
56 virtual String valueAsString() const OVERRIDE;
57 void setValueAsString(const String&, ExceptionState&);
59 virtual void add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*) OVERRIDE;
60 virtual void calculateAnimatedValue(SVGAnimationElement*, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*) OVERRIDE;
61 virtual float calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*) OVERRIDE;
63 static AnimatedPropertyType classType() { return AnimatedEnumeration; }
65 // Ensure that |SVGAnimatedEnumerationBase::setBaseVal| is used instead of |SVGAnimatedProperty<SVGEnumerationBase>::setBaseVal|.
66 void setValue(unsigned short) { ASSERT_NOT_REACHED(); }
68 static unsigned short valueOfLastEnum(const StringEntries& entries) { return entries.last().first; }
71 SVGEnumerationBase(unsigned short value, const StringEntries& entries, unsigned short maxExposed)
72 : SVGPropertyBase(classType())
74 , m_maxExposed(maxExposed)
79 // This is the maximum value of all the internal enumeration values.
80 // This assumes that |m_entries| are sorted.
81 unsigned short maxInternalEnumValue() const { return valueOfLastEnum(m_entries); }
83 // This is the maximum value that is exposed as an IDL constant on the relevant interface.
84 unsigned short maxExposedEnumValue() const { return m_maxExposed; }
86 // Used by SVGMarkerOrientEnumeration.
87 virtual void notifyChange() { }
89 unsigned short m_value;
90 const unsigned short m_maxExposed;
91 const StringEntries& m_entries;
93 typedef SVGEnumerationBase::StringEntries SVGEnumerationStringEntries;
95 template<typename Enum> const SVGEnumerationStringEntries& getStaticStringEntries();
96 template<typename Enum> unsigned short getMaxExposedEnumValue()
98 return SVGEnumerationBase::valueOfLastEnum(getStaticStringEntries<Enum>());
101 template<typename Enum>
102 class SVGEnumeration : public SVGEnumerationBase {
104 static PassRefPtr<SVGEnumeration<Enum> > create(Enum newValue)
106 return adoptRef(new SVGEnumeration<Enum>(newValue));
109 virtual ~SVGEnumeration()
113 virtual PassRefPtr<SVGEnumerationBase> clone() const OVERRIDE
115 return create(enumValue());
118 Enum enumValue() const
120 ASSERT(m_value <= maxInternalEnumValue());
121 return static_cast<Enum>(m_value);
124 void setEnumValue(Enum value)
131 explicit SVGEnumeration(Enum newValue)
132 : SVGEnumerationBase(newValue, getStaticStringEntries<Enum>(), getMaxExposedEnumValue<Enum>())
139 #endif // SVGEnumeration_h