2
* Copyright (C) 2011 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 CalculationValue_h
32
#define CalculationValue_h
35
#include "LengthFunctions.h"
36
#include <wtf/OwnPtr.h>
37
#include <wtf/PassOwnPtr.h>
38
#include <wtf/RefCounted.h>
39
#include <wtf/Vector.h>
50
enum CalculationPermittedValueRange {
52
CalculationRangeNonNegative
55
enum CalcExpressionNodeType {
56
CalcExpressionNodeUndefined,
57
CalcExpressionNodeNumber,
58
CalcExpressionNodeLength,
59
CalcExpressionNodeBinaryOperation,
60
CalcExpressionNodeBlendLength,
63
class CalcExpressionNode {
64
WTF_MAKE_FAST_ALLOCATED;
67
: m_type(CalcExpressionNodeUndefined)
71
virtual ~CalcExpressionNode()
75
virtual float evaluate(float maxValue) const = 0;
76
virtual bool operator==(const CalcExpressionNode&) const = 0;
78
CalcExpressionNodeType type() const { return m_type; }
81
CalcExpressionNodeType m_type;
84
class CalculationValue : public RefCounted<CalculationValue> {
86
static PassRefPtr<CalculationValue> create(PassOwnPtr<CalcExpressionNode> value, CalculationPermittedValueRange);
87
float evaluate(float maxValue) const;
89
bool operator==(const CalculationValue& o) const
91
return *(m_value.get()) == *(o.m_value.get());
95
CalculationValue(PassOwnPtr<CalcExpressionNode> value, CalculationPermittedValueRange range)
97
, m_isNonNegative(range == CalculationRangeNonNegative)
101
OwnPtr<CalcExpressionNode> m_value;
102
bool m_isNonNegative;
105
class CalcExpressionNumber : public CalcExpressionNode {
107
explicit CalcExpressionNumber(float value)
110
m_type = CalcExpressionNodeNumber;
113
bool operator==(const CalcExpressionNumber& o) const
115
return m_value == o.m_value;
118
virtual bool operator==(const CalcExpressionNode& o) const
120
return type() == o.type() && *this == static_cast<const CalcExpressionNumber&>(o);
123
virtual float evaluate(float) const
132
class CalcExpressionLength : public CalcExpressionNode {
134
explicit CalcExpressionLength(Length length)
137
m_type = CalcExpressionNodeLength;
140
bool operator==(const CalcExpressionLength& o) const
142
return m_length == o.m_length;
145
virtual bool operator==(const CalcExpressionNode& o) const
147
return type() == o.type() && *this == static_cast<const CalcExpressionLength&>(o);
150
virtual float evaluate(float maxValue) const
152
return floatValueForLength(m_length, maxValue);
159
class CalcExpressionBinaryOperation : public CalcExpressionNode {
161
CalcExpressionBinaryOperation(PassOwnPtr<CalcExpressionNode> leftSide, PassOwnPtr<CalcExpressionNode> rightSide, CalcOperator op)
162
: m_leftSide(leftSide)
163
, m_rightSide(rightSide)
166
m_type = CalcExpressionNodeBinaryOperation;
169
bool operator==(const CalcExpressionBinaryOperation& o) const
171
return m_operator == o.m_operator && *m_leftSide == *o.m_leftSide && *m_rightSide == *o.m_rightSide;
174
virtual bool operator==(const CalcExpressionNode& o) const
176
return type() == o.type() && *this == static_cast<const CalcExpressionBinaryOperation&>(o);
180
virtual float evaluate(float) const;
183
OwnPtr<CalcExpressionNode> m_leftSide;
184
OwnPtr<CalcExpressionNode> m_rightSide;
185
CalcOperator m_operator;
188
class CalcExpressionBlendLength : public CalcExpressionNode {
190
CalcExpressionBlendLength(Length from, Length to, float progress)
193
, m_progress(progress)
195
m_type = CalcExpressionNodeBlendLength;
198
bool operator==(const CalcExpressionBlendLength& o) const
200
return m_progress == o.m_progress && m_from == o.m_from && m_to == o.m_to;
203
virtual bool operator==(const CalcExpressionNode& o) const
205
return type() == o.type() && *this == static_cast<const CalcExpressionBlendLength&>(o);
208
virtual float evaluate(float maxValue) const
210
return (1.0f - m_progress) * floatValueForLength(m_from, maxValue) + m_progress * floatValueForLength(m_to, maxValue);
219
} // namespace WebCore
221
#endif // CalculationValue_h