OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | |
dstockwell
2014/02/25 02:14:51
Use the new/shorter license header.
shans
2014/02/25 03:26:43
Done.
| |
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 InterpolableValue_h | |
32 #define InterpolableValue_h | |
33 | |
34 #include "wtf/OwnPtr.h" | |
35 | |
36 namespace WebCore { | |
37 | |
38 class InterpolableValue { | |
39 public: | |
40 virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) = 0; | |
41 virtual PassOwnPtr<InterpolableValue> clone() = 0; | |
42 | |
43 virtual bool isNumber() const { return false; } | |
44 virtual bool isBool() const { return false; } | |
45 virtual bool isList() const { return false; } | |
46 | |
47 virtual ~InterpolableValue() { } | |
48 }; | |
49 | |
50 class InterpolableNumber; | |
51 class InterpolableList; | |
52 | |
53 InterpolableNumber* toInterpolableNumber(InterpolableValue*); | |
54 InterpolableList* toInterpolableList(InterpolableValue*); | |
55 | |
56 class InterpolableNumber : public InterpolableValue { | |
57 public: | |
58 static PassOwnPtr<InterpolableNumber> create(double value) | |
59 { | |
60 return adoptPtr(new InterpolableNumber(value)); | |
61 } | |
62 | |
63 virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) OVERRIDE FINAL { | |
dstockwell
2014/02/25 02:14:51
percentage -> progress
dstockwell
2014/02/25 02:14:51
these non-trivial functions should move to cpp
shans
2014/02/25 03:26:43
Done.
shans
2014/02/25 03:26:43
Done.
| |
64 InterpolableNumber *otherNumber = toInterpolableNumber(&other); | |
65 if (!percentage) | |
66 return create(m_value); | |
67 if (percentage == 1) | |
68 return create(otherNumber->m_value); | |
69 return create(m_value * (1 - percentage) + otherNumber->m_value * percen tage); | |
70 } | |
71 | |
72 virtual PassOwnPtr<InterpolableValue> clone() OVERRIDE FINAL { return create (m_value); } | |
73 | |
74 virtual bool isNumber() const OVERRIDE FINAL { return true; } | |
75 | |
76 double value() { return m_value; } | |
77 | |
78 private: | |
79 double m_value; | |
80 | |
81 InterpolableNumber(double value) | |
82 : m_value(value) | |
83 { } | |
84 | |
85 }; | |
86 | |
87 class InterpolableBool : public InterpolableValue { | |
88 public: | |
89 PassOwnPtr<InterpolableBool> create(bool value) | |
90 { | |
91 return adoptPtr(new InterpolableBool(value)); | |
92 } | |
93 | |
94 virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) OVERRIDE FINAL { | |
95 if (percentage < 0.5) { | |
96 return clone(); | |
97 } | |
98 return (other.clone()); | |
dstockwell
2014/02/25 02:14:51
remove parens here
shans
2014/02/25 03:26:43
Done.
| |
99 } | |
100 | |
101 virtual PassOwnPtr<InterpolableValue> clone() OVERRIDE FINAL { return create (m_value); } | |
102 | |
103 virtual bool isBool() const OVERRIDE FINAL { return true; } | |
104 | |
105 bool value() { return m_value; } | |
106 | |
107 private: | |
108 bool m_value; | |
109 | |
110 InterpolableBool(bool value) | |
111 : m_value(value) | |
112 { } | |
113 }; | |
114 | |
115 class InterpolableList : public InterpolableValue { | |
116 public: | |
117 static PassOwnPtr<InterpolableList> create(InterpolableList &other) | |
118 { | |
119 return adoptPtr(new InterpolableList(other)); | |
120 } | |
121 | |
122 static PassOwnPtr<InterpolableList> create(size_t size) | |
123 { | |
124 return adoptPtr(new InterpolableList(size)); | |
125 } | |
126 | |
127 virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) OVERRIDE FINAL { | |
128 InterpolableList *otherList = toInterpolableList(&other); | |
dstockwell
2014/02/25 02:14:51
assert same size
shans
2014/02/25 03:26:43
Done.
| |
129 if (!percentage) { | |
130 return create(*this); | |
131 } | |
132 if (percentage == 1) { | |
133 return InterpolableList::create(*otherList); | |
134 } | |
135 | |
136 OwnPtr<InterpolableList> result = create(m_size); | |
137 for (size_t i = 0; i < m_size; i++) | |
138 result->set(m_values.get()[i]->interpolate(*(otherList->m_values.get ()[i]), percentage), i); | |
139 return result.release(); | |
140 } | |
141 | |
142 virtual PassOwnPtr<InterpolableValue> clone() OVERRIDE FINAL { return create (*this); } | |
143 | |
144 virtual bool isList() const OVERRIDE FINAL { return true; } | |
145 | |
146 void set(PassOwnPtr<InterpolableValue> value, size_t position) { m_values.ge t()[position] = value; } | |
dstockwell
2014/02/25 02:14:51
ASSERT position < m_size? Or is the size mutable?
shans
2014/02/25 03:26:43
Done.
| |
147 | |
148 private: | |
149 InterpolableList(size_t size) | |
150 : m_size(size) | |
151 { | |
152 m_values = adoptArrayPtr(new OwnPtr<InterpolableValue>[size]); | |
153 } | |
154 | |
155 InterpolableList(InterpolableList& other) | |
156 : m_size(other.m_size) | |
157 { | |
158 m_values = adoptArrayPtr(new OwnPtr<InterpolableValue>[m_size]); | |
159 for (size_t i = 0; i < m_size; i++) | |
160 set(other.m_values.get()[i]->clone(), i); | |
161 } | |
162 | |
163 size_t m_size; | |
164 OwnPtr<OwnPtr<InterpolableValue>[]> m_values; | |
165 }; | |
166 | |
167 DEFINE_TYPE_CASTS(InterpolableNumber, InterpolableValue, value, value->isNumber( ), value.isNumber()); | |
168 DEFINE_TYPE_CASTS(InterpolableBool, InterpolableValue, value, value->isBool(), v alue.isBool()); | |
169 DEFINE_TYPE_CASTS(InterpolableList, InterpolableValue, value, value->isList(), v alue.isList()); | |
170 | |
171 } | |
172 | |
173 #endif | |
OLD | NEW |