summaryrefslogtreecommitdiff
path: root/brillo/any.cc
blob: 30e875bbe56c38fe0066850f7a7f5feeebc0fad6 (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
// Copyright 2014 The Chromium OS 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 <brillo/any.h>

#include <algorithm>

namespace brillo {

Any::Any() {
}

Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) {
}

// NOLINTNEXTLINE(build/c++11)
Any::Any(Any&& rhs) : data_buffer_(std::move(rhs.data_buffer_)) {
}

Any::~Any() {
}

Any& Any::operator=(const Any& rhs) {
  data_buffer_ = rhs.data_buffer_;
  return *this;
}

// NOLINTNEXTLINE(build/c++11)
Any& Any::operator=(Any&& rhs) {
  data_buffer_ = std::move(rhs.data_buffer_);
  return *this;
}

bool Any::operator==(const Any& rhs) const {
  // Make sure both objects contain data of the same type.
  if (GetType() != rhs.GetType())
    return false;

  if (IsEmpty())
    return true;

  return data_buffer_.GetDataPtr()->CompareEqual(rhs.data_buffer_.GetDataPtr());
}

const std::type_info& Any::GetType() const {
  if (!IsEmpty())
    return data_buffer_.GetDataPtr()->GetType();

  struct NullType {};  // Special helper type representing an empty variant.
  return typeid(NullType);
}

void Any::Swap(Any& other) {
  std::swap(data_buffer_, other.data_buffer_);
}

bool Any::IsEmpty() const {
  return data_buffer_.IsEmpty();
}

void Any::Clear() {
  data_buffer_.Clear();
}

bool Any::IsConvertibleToInteger() const {
  return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger();
}

intmax_t Any::GetAsInteger() const {
  CHECK(!IsEmpty()) << "Must not be called on an empty Any";
  return data_buffer_.GetDataPtr()->GetAsInteger();
}

void Any::AppendToDBusMessageWriter(dbus::MessageWriter* writer) const {
  CHECK(!IsEmpty()) << "Must not be called on an empty Any";
  data_buffer_.GetDataPtr()->AppendToDBusMessage(writer);
}

}  // namespace brillo