CalculationValue.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2011 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. #ifndef CalculationValue_h
  31. #define CalculationValue_h
  32. #include "Length.h"
  33. #include "LengthFunctions.h"
  34. #include <wtf/OwnPtr.h>
  35. #include <wtf/PassOwnPtr.h>
  36. #include <wtf/RefCounted.h>
  37. #include <wtf/Vector.h>
  38. namespace WebCore {
  39. enum CalcOperator {
  40. CalcAdd = '+',
  41. CalcSubtract = '-',
  42. CalcMultiply = '*',
  43. CalcDivide = '/'
  44. };
  45. enum CalculationPermittedValueRange {
  46. CalculationRangeAll,
  47. CalculationRangeNonNegative
  48. };
  49. enum CalcExpressionNodeType {
  50. CalcExpressionNodeUndefined,
  51. CalcExpressionNodeNumber,
  52. CalcExpressionNodeLength,
  53. CalcExpressionNodeBinaryOperation,
  54. CalcExpressionNodeBlendLength,
  55. };
  56. class CalcExpressionNode {
  57. WTF_MAKE_FAST_ALLOCATED;
  58. public:
  59. CalcExpressionNode()
  60. : m_type(CalcExpressionNodeUndefined)
  61. {
  62. }
  63. virtual ~CalcExpressionNode()
  64. {
  65. }
  66. virtual float evaluate(float maxValue) const = 0;
  67. virtual bool operator==(const CalcExpressionNode&) const = 0;
  68. CalcExpressionNodeType type() const { return m_type; }
  69. protected:
  70. CalcExpressionNodeType m_type;
  71. };
  72. class CalculationValue : public RefCounted<CalculationValue> {
  73. public:
  74. static PassRefPtr<CalculationValue> create(PassOwnPtr<CalcExpressionNode> value, CalculationPermittedValueRange);
  75. float evaluate(float maxValue) const;
  76. bool operator==(const CalculationValue& o) const
  77. {
  78. return *(m_value.get()) == *(o.m_value.get());
  79. }
  80. private:
  81. CalculationValue(PassOwnPtr<CalcExpressionNode> value, CalculationPermittedValueRange range)
  82. : m_value(value)
  83. , m_isNonNegative(range == CalculationRangeNonNegative)
  84. {
  85. }
  86. OwnPtr<CalcExpressionNode> m_value;
  87. bool m_isNonNegative;
  88. };
  89. class CalcExpressionNumber : public CalcExpressionNode {
  90. public:
  91. explicit CalcExpressionNumber(float value)
  92. : m_value(value)
  93. {
  94. m_type = CalcExpressionNodeNumber;
  95. }
  96. bool operator==(const CalcExpressionNumber& o) const
  97. {
  98. return m_value == o.m_value;
  99. }
  100. virtual bool operator==(const CalcExpressionNode& o) const
  101. {
  102. return type() == o.type() && *this == static_cast<const CalcExpressionNumber&>(o);
  103. }
  104. virtual float evaluate(float) const
  105. {
  106. return m_value;
  107. }
  108. private:
  109. float m_value;
  110. };
  111. class CalcExpressionLength : public CalcExpressionNode {
  112. public:
  113. explicit CalcExpressionLength(Length length)
  114. : m_length(length)
  115. {
  116. m_type = CalcExpressionNodeLength;
  117. }
  118. bool operator==(const CalcExpressionLength& o) const
  119. {
  120. return m_length == o.m_length;
  121. }
  122. virtual bool operator==(const CalcExpressionNode& o) const
  123. {
  124. return type() == o.type() && *this == static_cast<const CalcExpressionLength&>(o);
  125. }
  126. virtual float evaluate(float maxValue) const
  127. {
  128. return floatValueForLength(m_length, maxValue);
  129. }
  130. private:
  131. Length m_length;
  132. };
  133. class CalcExpressionBinaryOperation : public CalcExpressionNode {
  134. public:
  135. CalcExpressionBinaryOperation(PassOwnPtr<CalcExpressionNode> leftSide, PassOwnPtr<CalcExpressionNode> rightSide, CalcOperator op)
  136. : m_leftSide(leftSide)
  137. , m_rightSide(rightSide)
  138. , m_operator(op)
  139. {
  140. m_type = CalcExpressionNodeBinaryOperation;
  141. }
  142. bool operator==(const CalcExpressionBinaryOperation& o) const
  143. {
  144. return m_operator == o.m_operator && *m_leftSide == *o.m_leftSide && *m_rightSide == *o.m_rightSide;
  145. }
  146. virtual bool operator==(const CalcExpressionNode& o) const
  147. {
  148. return type() == o.type() && *this == static_cast<const CalcExpressionBinaryOperation&>(o);
  149. }
  150. virtual float evaluate(float) const;
  151. private:
  152. OwnPtr<CalcExpressionNode> m_leftSide;
  153. OwnPtr<CalcExpressionNode> m_rightSide;
  154. CalcOperator m_operator;
  155. };
  156. class CalcExpressionBlendLength : public CalcExpressionNode {
  157. public:
  158. CalcExpressionBlendLength(Length from, Length to, float progress)
  159. : m_from(from)
  160. , m_to(to)
  161. , m_progress(progress)
  162. {
  163. m_type = CalcExpressionNodeBlendLength;
  164. }
  165. bool operator==(const CalcExpressionBlendLength& o) const
  166. {
  167. return m_progress == o.m_progress && m_from == o.m_from && m_to == o.m_to;
  168. }
  169. virtual bool operator==(const CalcExpressionNode& o) const
  170. {
  171. return type() == o.type() && *this == static_cast<const CalcExpressionBlendLength&>(o);
  172. }
  173. virtual float evaluate(float maxValue) const
  174. {
  175. return (1.0f - m_progress) * floatValueForLength(m_from, maxValue) + m_progress * floatValueForLength(m_to, maxValue);
  176. }
  177. private:
  178. Length m_from;
  179. Length m_to;
  180. float m_progress;
  181. };
  182. } // namespace WebCore
  183. #endif // CalculationValue_h