summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/WebKit/Source/core/animation/InterpolableValue.h
blob: 75484cf12b1e884c3f3062ed9c7e0577d949055c (plain)
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
// 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/animation/AnimatableValue.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/Vector.h"

namespace WebCore {

class InterpolableValue : public NoBaseWillBeGarbageCollected<InterpolableValue> {
    DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(InterpolableValue);
public:
    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 PassOwnPtrWillBeRawPtr<InterpolableValue> clone() const = 0;

    virtual void trace(Visitor*) { }

private:
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> interpolate(const InterpolableValue &to, const double progress) const = 0;

    friend class Interpolation;

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

class InterpolableNumber : public InterpolableValue {
public:
    static PassOwnPtrWillBeRawPtr<InterpolableNumber> create(double value)
    {
        return adoptPtrWillBeNoop(new InterpolableNumber(value));
    }

    virtual bool isNumber() const OVERRIDE FINAL { return true; }
    double value() const { return m_value; }
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> clone() const OVERRIDE FINAL { return create(m_value); }

    virtual void trace(Visitor* visitor) OVERRIDE { InterpolableValue::trace(visitor); }

private:
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> interpolate(const InterpolableValue &to, const double progress) const OVERRIDE FINAL;
    double m_value;

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

};

class InterpolableBool : public InterpolableValue {
public:
    static PassOwnPtrWillBeRawPtr<InterpolableBool> create(bool value)
    {
        return adoptPtrWillBeNoop(new InterpolableBool(value));
    }

    virtual bool isBool() const OVERRIDE FINAL { return true; }
    bool value() const { return m_value; }
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> clone() const OVERRIDE FINAL { return create(m_value); }

    virtual void trace(Visitor* visitor) OVERRIDE { InterpolableValue::trace(visitor); }

private:
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> interpolate(const InterpolableValue &to, const double progress) const OVERRIDE FINAL;
    bool m_value;

    explicit InterpolableBool(bool value)
        : m_value(value)
    {
    }

};

class InterpolableList : public InterpolableValue {
public:
    static PassOwnPtrWillBeRawPtr<InterpolableList> create(const InterpolableList &other)
    {
        return adoptPtrWillBeNoop(new InterpolableList(other));
    }

    static PassOwnPtrWillBeRawPtr<InterpolableList> create(size_t size)
    {
        return adoptPtrWillBeNoop(new InterpolableList(size));
    }

    virtual bool isList() const OVERRIDE FINAL { return true; }
    void set(size_t position, PassOwnPtrWillBeRawPtr<InterpolableValue> value)
    {
        ASSERT(position < m_size);
        m_values[position] = value;
    }
    const InterpolableValue* get(size_t position) const
    {
        ASSERT(position < m_size);
        return m_values[position].get();
    }
    size_t length() const { return m_size; }
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> clone() const OVERRIDE FINAL { return create(*this); }

    virtual void trace(Visitor*) OVERRIDE;

private:
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> interpolate(const InterpolableValue &other, const double progress) const OVERRIDE FINAL;
    explicit InterpolableList(size_t size)
        : m_size(size)
        , m_values(m_size)
    {
    }

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

    size_t m_size;
    WillBeHeapVector<OwnPtrWillBeMember<InterpolableValue> > m_values;
};

// FIXME: Remove this when we can.
class InterpolableAnimatableValue : public InterpolableValue {
public:
    static PassOwnPtrWillBeRawPtr<InterpolableAnimatableValue> create(PassRefPtrWillBeRawPtr<AnimatableValue> value)
    {
        return adoptPtrWillBeNoop(new InterpolableAnimatableValue(value));
    }

    virtual bool isAnimatableValue() const OVERRIDE FINAL { return true; }
    AnimatableValue* value() const { return m_value.get(); }
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> clone() const OVERRIDE FINAL { return create(m_value); }

    virtual void trace(Visitor*) OVERRIDE;

private:
    virtual PassOwnPtrWillBeRawPtr<InterpolableValue> interpolate(const InterpolableValue &other, const double progress) const OVERRIDE FINAL;
    RefPtrWillBeMember<AnimatableValue> m_value;

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

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

}

#endif