1/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#ifndef SVGEnumeration_h
32#define SVGEnumeration_h
33
34#include "core/svg/properties/SVGProperty.h"
35
36namespace WebCore {
37
38class SVGEnumerationBase : public SVGPropertyBase {
39public:
40    typedef std::pair<unsigned short, String> StringEntry;
41    typedef Vector<StringEntry> StringEntries;
42
43    // SVGEnumeration does not have a tear-off type.
44    typedef void TearOffType;
45    typedef unsigned short PrimitiveType;
46
47    virtual ~SVGEnumerationBase();
48
49    unsigned short value() const { return m_value; }
50    void setValue(unsigned short, ExceptionState&);
51
52    // This assumes that |m_entries| are sorted.
53    unsigned short maxEnumValue() const { return m_entries.last().first; }
54
55    // SVGPropertyBase:
56    virtual PassRefPtr<SVGEnumerationBase> clone() const = 0;
57    virtual PassRefPtr<SVGPropertyBase> cloneForAnimation(const String&) const OVERRIDE;
58
59    virtual String valueAsString() const OVERRIDE;
60    void setValueAsString(const String&, ExceptionState&);
61
62    virtual void add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*) OVERRIDE;
63    virtual void calculateAnimatedValue(SVGAnimationElement*, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*) OVERRIDE;
64    virtual float calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*) OVERRIDE;
65
66    static AnimatedPropertyType classType() { return AnimatedEnumeration; }
67
68    // Ensure that |SVGAnimatedEnumerationBase::setBaseVal| is used instead of |SVGAnimatedProperty<SVGEnumerationBase>::setBaseVal|.
69    void setValue(unsigned short) { ASSERT_NOT_REACHED(); }
70
71protected:
72    SVGEnumerationBase(unsigned short value, const StringEntries& entries)
73        : SVGPropertyBase(classType())
74        , m_value(value)
75        , m_entries(entries)
76    {
77    }
78
79    // Used by SVGMarkerOrientEnumeration.
80    virtual void notifyChange() { }
81
82    unsigned short m_value;
83    const StringEntries& m_entries;
84};
85typedef SVGEnumerationBase::StringEntries SVGEnumerationStringEntries;
86
87template<typename Enum> const SVGEnumerationStringEntries& getStaticStringEntries();
88
89template<typename Enum>
90class SVGEnumeration : public SVGEnumerationBase {
91public:
92    static PassRefPtr<SVGEnumeration<Enum> > create(Enum newValue)
93    {
94        return adoptRef(new SVGEnumeration<Enum>(newValue));
95    }
96
97    virtual ~SVGEnumeration()
98    {
99    }
100
101    virtual PassRefPtr<SVGEnumerationBase> clone() const OVERRIDE
102    {
103        return create(enumValue());
104    }
105
106    Enum enumValue() const
107    {
108        ASSERT(m_value <= maxEnumValue());
109        return static_cast<Enum>(m_value);
110    }
111
112    void setEnumValue(Enum value)
113    {
114        m_value = value;
115        notifyChange();
116    }
117
118protected:
119    explicit SVGEnumeration(Enum newValue)
120        : SVGEnumerationBase(newValue, getStaticStringEntries<Enum>())
121    {
122    }
123};
124
125} // namespace WebCore
126
127#endif // SVGEnumeration_h
128