summaryrefslogtreecommitdiff
path: root/chromium/media/base/status.cc
blob: 003b65753302812f1fb235bd8e1aa2a77c0e5122 (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
// Copyright 2020 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 "media/base/status.h"

#include <memory>
#include "media/base/media_serializers.h"

namespace media {

Status::Status() = default;

Status::Status(StatusCode code,
               base::StringPiece message,
               const base::Location& location) {
  DCHECK(code != StatusCode::kOk);
  data_ = std::make_unique<StatusInternal>(code, message.as_string());
  AddFrame(location);
}

// Copy Constructor
Status::Status(const Status& copy) {
  *this = copy;
}

Status& Status::operator=(const Status& copy) {
  if (copy.is_ok()) {
    data_.reset();
    return *this;
  }

  data_ = std::make_unique<StatusInternal>(copy.code(), copy.message());
  for (const base::Value& frame : copy.data_->frames)
    data_->frames.push_back(frame.Clone());
  for (const Status& err : copy.data_->causes)
    data_->causes.push_back(err);
  data_->data = copy.data_->data.Clone();
  return *this;
}

// Allow move.
Status::Status(Status&&) = default;
Status& Status::operator=(Status&&) = default;

Status::~Status() = default;

Status::StatusInternal::StatusInternal(StatusCode code, std::string message)
    : code(code),
      message(std::move(message)),
      data(base::Value(base::Value::Type::DICTIONARY)) {}

Status::StatusInternal::~StatusInternal() = default;

Status&& Status::AddHere(const base::Location& location) && {
  DCHECK(data_);
  AddFrame(location);
  return std::move(*this);
}

Status&& Status::AddCause(Status&& cause) && {
  DCHECK(data_ && cause.data_);
  data_->causes.push_back(std::move(cause));
  return std::move(*this);
}

void Status::AddFrame(const base::Location& location) {
  DCHECK(data_);
  data_->frames.push_back(MediaSerialize(location));
}

Status OkStatus() {
  return Status();
}

}  // namespace media