File: InterpolableValue.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (187 lines) | stat: -rw-r--r-- 6,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef InterpolableValue_h
#define InterpolableValue_h

#include "core/CoreExport.h"
#include "core/animation/animatable/AnimatableValue.h"
#include "platform/heap/Handle.h"
#include "wtf/PtrUtil.h"
#include "wtf/Vector.h"
#include <memory>

namespace blink {

// Represents the components of a PropertySpecificKeyframe's value that change
// smoothly as it interpolates to an adjacent value.
class CORE_EXPORT InterpolableValue {
  USING_FAST_MALLOC(InterpolableValue);

 public:
  virtual ~InterpolableValue() {}

  virtual bool isNumber() const { return false; }
  virtual bool isBool() const { return false; }
  virtual bool isList() const { return false; }
  virtual bool isAnimatableValue() const { return false; }

  virtual bool equals(const InterpolableValue&) const = 0;
  virtual std::unique_ptr<InterpolableValue> clone() const = 0;
  virtual std::unique_ptr<InterpolableValue> cloneAndZero() const = 0;
  virtual void scale(double scale) = 0;
  virtual void scaleAndAdd(double scale, const InterpolableValue& other) = 0;

 private:
  virtual void interpolate(const InterpolableValue& to,
                           const double progress,
                           InterpolableValue& result) const = 0;

  friend class LegacyStyleInterpolation;
  friend class PairwisePrimitiveInterpolation;

  // Keep interpolate private, but allow calls within the hierarchy without
  // knowledge of type.
  friend class InterpolableNumber;
  friend class InterpolableBool;
  friend class InterpolableList;

  friend class AnimationInterpolableValueTest;
};

class CORE_EXPORT InterpolableNumber final : public InterpolableValue {
 public:
  static std::unique_ptr<InterpolableNumber> create(double value) {
    return WTF::wrapUnique(new InterpolableNumber(value));
  }

  bool isNumber() const final { return true; }
  double value() const { return m_value; }
  bool equals(const InterpolableValue& other) const final;
  std::unique_ptr<InterpolableValue> clone() const final {
    return create(m_value);
  }
  std::unique_ptr<InterpolableValue> cloneAndZero() const final {
    return create(0);
  }
  void scale(double scale) final;
  void scaleAndAdd(double scale, const InterpolableValue& other) final;
  void set(double value) { m_value = value; }

 private:
  void interpolate(const InterpolableValue& to,
                   const double progress,
                   InterpolableValue& result) const final;
  double m_value;

  explicit InterpolableNumber(double value) : m_value(value) {}
};

class CORE_EXPORT InterpolableList : public InterpolableValue {
 public:
  // Explicitly delete operator= because MSVC automatically generate
  // copy constructors and operator= for dll-exported classes.
  // Since InterpolableList is not copyable, automatically generated
  // operator= causes MSVC compiler error.
  // However, we cannot use WTF_MAKE_NONCOPYABLE because InterpolableList
  // has its own copy constructor. So just delete operator= here.
  InterpolableList& operator=(const InterpolableList&) = delete;

  static std::unique_ptr<InterpolableList> create(
      const InterpolableList& other) {
    return WTF::wrapUnique(new InterpolableList(other));
  }

  static std::unique_ptr<InterpolableList> create(size_t size) {
    return WTF::wrapUnique(new InterpolableList(size));
  }

  bool isList() const final { return true; }
  void set(size_t position, std::unique_ptr<InterpolableValue> value) {
    m_values[position] = std::move(value);
  }
  const InterpolableValue* get(size_t position) const {
    return m_values[position].get();
  }
  std::unique_ptr<InterpolableValue>& getMutable(size_t position) {
    return m_values[position];
  }
  size_t length() const { return m_values.size(); }
  bool equals(const InterpolableValue& other) const final;
  std::unique_ptr<InterpolableValue> clone() const final {
    return create(*this);
  }
  std::unique_ptr<InterpolableValue> cloneAndZero() const final;
  void scale(double scale) final;
  void scaleAndAdd(double scale, const InterpolableValue& other) final;

 private:
  void interpolate(const InterpolableValue& to,
                   const double progress,
                   InterpolableValue& result) const final;
  explicit InterpolableList(size_t size) : m_values(size) {}

  InterpolableList(const InterpolableList& other) : m_values(other.length()) {
    for (size_t i = 0; i < length(); i++)
      set(i, other.m_values[i]->clone());
  }

  Vector<std::unique_ptr<InterpolableValue>> m_values;
};

// FIXME: Remove this when we can.
class InterpolableAnimatableValue : public InterpolableValue {
 public:
  static std::unique_ptr<InterpolableAnimatableValue> create(
      PassRefPtr<AnimatableValue> value) {
    return WTF::wrapUnique(new InterpolableAnimatableValue(std::move(value)));
  }

  bool isAnimatableValue() const final { return true; }
  AnimatableValue* value() const { return m_value.get(); }
  bool equals(const InterpolableValue&) const final {
    NOTREACHED();
    return false;
  }
  std::unique_ptr<InterpolableValue> clone() const final {
    return create(m_value);
  }
  std::unique_ptr<InterpolableValue> cloneAndZero() const final {
    NOTREACHED();
    return nullptr;
  }
  void scale(double scale) final { NOTREACHED(); }
  void scaleAndAdd(double scale, const InterpolableValue& other) final {
    NOTREACHED();
  }

 private:
  void interpolate(const InterpolableValue& to,
                   const double progress,
                   InterpolableValue& result) const final;
  RefPtr<AnimatableValue> m_value;

  InterpolableAnimatableValue(PassRefPtr<AnimatableValue> value)
      : m_value(value) {}
};

DEFINE_TYPE_CASTS(InterpolableNumber,
                  InterpolableValue,
                  value,
                  value->isNumber(),
                  value.isNumber());
DEFINE_TYPE_CASTS(InterpolableList,
                  InterpolableValue,
                  value,
                  value->isList(),
                  value.isList());
DEFINE_TYPE_CASTS(InterpolableAnimatableValue,
                  InterpolableValue,
                  value,
                  value->isAnimatableValue(),
                  value.isAnimatableValue());

}  // namespace blink

#endif