summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/underlying_value_owner.cc
blob: c0b6defa35601d2b8f64157f430fee6dbac11bda (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
// Copyright 2016 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.

#include "third_party/blink/renderer/core/animation/underlying_value_owner.h"

#include <memory>

namespace blink {

struct NullValueWrapper {
  NullValueWrapper() : value(nullptr) {}
  const InterpolationValue value;
};

InterpolableValue& UnderlyingValueOwner::MutableInterpolableValue() {
  return *MutableValue().interpolable_value;
}

void UnderlyingValueOwner::SetInterpolableValue(
    std::unique_ptr<InterpolableValue> interpolable_value) {
  DCHECK(type_);
  MutableValue().interpolable_value = std::move(interpolable_value);
}

const NonInterpolableValue* UnderlyingValueOwner::GetNonInterpolableValue()
    const {
  DCHECK(value_);
  return value_->non_interpolable_value.get();
}

void UnderlyingValueOwner::SetNonInterpolableValue(
    scoped_refptr<const NonInterpolableValue> non_interpolable_value) {
  MutableValue().non_interpolable_value = non_interpolable_value;
}

const InterpolationValue& UnderlyingValueOwner::Value() const {
  DEFINE_STATIC_LOCAL(NullValueWrapper, null_value_wrapper, ());
  return *this ? *value_ : null_value_wrapper.value;
}

void UnderlyingValueOwner::Set(std::nullptr_t) {
  type_ = nullptr;
  value_owner_.Clear();
  value_ = nullptr;
}

void UnderlyingValueOwner::Set(const InterpolationType& type,
                               const InterpolationValue& value) {
  DCHECK(value);
  type_ = &type;
  // By clearing |value_owner_| we will perform a copy before attempting to
  // mutate |value_|, thus upholding the const contract for this instance of
  // interpolationValue.
  value_owner_.Clear();
  value_ = &value;
}

void UnderlyingValueOwner::Set(const InterpolationType& type,
                               InterpolationValue&& value) {
  DCHECK(value);
  type_ = &type;
  value_owner_ = std::move(value);
  value_ = &value_owner_;
}

void UnderlyingValueOwner::Set(std::unique_ptr<TypedInterpolationValue> value) {
  if (value)
    Set(value->GetType(), std::move(value->MutableValue()));
  else
    Set(nullptr);
}

void UnderlyingValueOwner::Set(const TypedInterpolationValue* value) {
  if (value)
    Set(value->GetType(), value->Value());
  else
    Set(nullptr);
}

InterpolationValue& UnderlyingValueOwner::MutableValue() {
  DCHECK(type_ && value_);
  if (!value_owner_) {
    value_owner_ = value_->Clone();
    value_ = &value_owner_;
  }
  return value_owner_;
}

}  // namespace blink