Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGEnumeration.h
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
36 namespace blink {
37
38 class SVGEnumerationBase : public SVGPropertyBase {
39 public:
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 <= maxExposedEnumValue() ? m_value : 0; }
50     void setValue(unsigned short, ExceptionState&);
51
52     // SVGPropertyBase:
53     virtual PassRefPtr<SVGEnumerationBase> clone() const = 0;
54     virtual PassRefPtr<SVGPropertyBase> cloneForAnimation(const String&) const OVERRIDE;
55
56     virtual String valueAsString() const OVERRIDE;
57     void setValueAsString(const String&, ExceptionState&);
58
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;
62
63     static AnimatedPropertyType classType() { return AnimatedEnumeration; }
64
65     // Ensure that |SVGAnimatedEnumerationBase::setBaseVal| is used instead of |SVGAnimatedProperty<SVGEnumerationBase>::setBaseVal|.
66     void setValue(unsigned short) { ASSERT_NOT_REACHED(); }
67
68     static unsigned short valueOfLastEnum(const StringEntries& entries) { return entries.last().first; }
69
70 protected:
71     SVGEnumerationBase(unsigned short value, const StringEntries& entries, unsigned short maxExposed)
72         : SVGPropertyBase(classType())
73         , m_value(value)
74         , m_maxExposed(maxExposed)
75         , m_entries(entries)
76     {
77     }
78
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); }
82
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; }
85
86     // Used by SVGMarkerOrientEnumeration.
87     virtual void notifyChange() { }
88
89     unsigned short m_value;
90     const unsigned short m_maxExposed;
91     const StringEntries& m_entries;
92 };
93 typedef SVGEnumerationBase::StringEntries SVGEnumerationStringEntries;
94
95 template<typename Enum> const SVGEnumerationStringEntries& getStaticStringEntries();
96 template<typename Enum> unsigned short getMaxExposedEnumValue()
97 {
98     return SVGEnumerationBase::valueOfLastEnum(getStaticStringEntries<Enum>());
99 }
100
101 template<typename Enum>
102 class SVGEnumeration : public SVGEnumerationBase {
103 public:
104     static PassRefPtr<SVGEnumeration<Enum> > create(Enum newValue)
105     {
106         return adoptRef(new SVGEnumeration<Enum>(newValue));
107     }
108
109     virtual ~SVGEnumeration()
110     {
111     }
112
113     virtual PassRefPtr<SVGEnumerationBase> clone() const OVERRIDE
114     {
115         return create(enumValue());
116     }
117
118     Enum enumValue() const
119     {
120         ASSERT(m_value <= maxInternalEnumValue());
121         return static_cast<Enum>(m_value);
122     }
123
124     void setEnumValue(Enum value)
125     {
126         m_value = value;
127         notifyChange();
128     }
129
130 protected:
131     explicit SVGEnumeration(Enum newValue)
132         : SVGEnumerationBase(newValue, getStaticStringEntries<Enum>(), getMaxExposedEnumValue<Enum>())
133     {
134     }
135 };
136
137 } // namespace blink
138
139 #endif // SVGEnumeration_h