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"
40 class SVGEnumerationBase : public SVGPropertyBase {
42 typedef std::pair<unsigned short, String> StringEntry;
43 typedef Vector<StringEntry> StringEntries;
45 // SVGEnumeration does not have a tear-off type.
46 typedef void TearOffType;
47 typedef unsigned short PrimitiveType;
49 virtual ~SVGEnumerationBase();
51 unsigned short value() const { return m_value <= maxExposedEnumValue() ? m_value : 0; }
52 void setValue(unsigned short, ExceptionState&);
55 virtual PassRefPtr<SVGEnumerationBase> clone() const = 0;
56 virtual PassRefPtr<SVGPropertyBase> cloneForAnimation(const String&) const override;
58 virtual String valueAsString() const override;
59 void setValueAsString(const String&, ExceptionState&);
61 virtual void add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*) override;
62 virtual void calculateAnimatedValue(SVGAnimationElement*, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*) override;
63 virtual float calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*) override;
65 static AnimatedPropertyType classType() { return AnimatedEnumeration; }
67 // Ensure that |SVGAnimatedEnumerationBase::setBaseVal| is used instead of |SVGAnimatedProperty<SVGEnumerationBase>::setBaseVal|.
68 void setValue(unsigned short) { ASSERT_NOT_REACHED(); }
70 static unsigned short valueOfLastEnum(const StringEntries& entries) { return entries.last().first; }
73 SVGEnumerationBase(unsigned short value, const StringEntries& entries, unsigned short maxExposed)
74 : SVGPropertyBase(classType())
76 , m_maxExposed(maxExposed)
81 // This is the maximum value of all the internal enumeration values.
82 // This assumes that |m_entries| are sorted.
83 unsigned short maxInternalEnumValue() const { return valueOfLastEnum(m_entries); }
85 // This is the maximum value that is exposed as an IDL constant on the relevant interface.
86 unsigned short maxExposedEnumValue() const { return m_maxExposed; }
88 // Used by SVGMarkerOrientEnumeration.
89 virtual void notifyChange() { }
91 unsigned short m_value;
92 const unsigned short m_maxExposed;
93 const StringEntries& m_entries;
95 typedef SVGEnumerationBase::StringEntries SVGEnumerationStringEntries;
97 template<typename Enum> const SVGEnumerationStringEntries& getStaticStringEntries();
98 template<typename Enum> unsigned short getMaxExposedEnumValue()
100 return SVGEnumerationBase::valueOfLastEnum(getStaticStringEntries<Enum>());
103 template<typename Enum>
104 class SVGEnumeration : public SVGEnumerationBase {
106 static PassRefPtr<SVGEnumeration<Enum> > create(Enum newValue)
108 return adoptRef(new SVGEnumeration<Enum>(newValue));
111 virtual ~SVGEnumeration()
115 virtual PassRefPtr<SVGEnumerationBase> clone() const override
117 return create(enumValue());
120 Enum enumValue() const
122 ASSERT(m_value <= maxInternalEnumValue());
123 return static_cast<Enum>(m_value);
126 void setEnumValue(Enum value)
133 explicit SVGEnumeration(Enum newValue)
134 : SVGEnumerationBase(newValue, getStaticStringEntries<Enum>(), getMaxExposedEnumValue<Enum>())
141 #endif // SVGEnumeration_h