1// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <brillo/any.h>
6
7#include <algorithm>
8
9namespace brillo {
10
11Any::Any() {
12}
13
14Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) {
15}
16
17// NOLINTNEXTLINE(build/c++11)
18Any::Any(Any&& rhs) : data_buffer_(std::move(rhs.data_buffer_)) {
19}
20
21Any::~Any() {
22}
23
24Any& Any::operator=(const Any& rhs) {
25  data_buffer_ = rhs.data_buffer_;
26  return *this;
27}
28
29// NOLINTNEXTLINE(build/c++11)
30Any& Any::operator=(Any&& rhs) {
31  data_buffer_ = std::move(rhs.data_buffer_);
32  return *this;
33}
34
35bool Any::operator==(const Any& rhs) const {
36  // Make sure both objects contain data of the same type.
37  if (strcmp(GetTypeTagInternal(), rhs.GetTypeTagInternal()) != 0)
38    return false;
39
40  if (IsEmpty())
41    return true;
42
43  return data_buffer_.GetDataPtr()->CompareEqual(rhs.data_buffer_.GetDataPtr());
44}
45
46const char* Any::GetTypeTagInternal() const {
47  if (!IsEmpty())
48    return data_buffer_.GetDataPtr()->GetTypeTag();
49
50  return "";
51}
52
53void Any::Swap(Any& other) {
54  std::swap(data_buffer_, other.data_buffer_);
55}
56
57bool Any::IsEmpty() const {
58  return data_buffer_.IsEmpty();
59}
60
61void Any::Clear() {
62  data_buffer_.Clear();
63}
64
65bool Any::IsConvertibleToInteger() const {
66  return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger();
67}
68
69intmax_t Any::GetAsInteger() const {
70  CHECK(!IsEmpty()) << "Must not be called on an empty Any";
71  return data_buffer_.GetDataPtr()->GetAsInteger();
72}
73
74void Any::AppendToDBusMessageWriter(dbus::MessageWriter* writer) const {
75  CHECK(!IsEmpty()) << "Must not be called on an empty Any";
76  data_buffer_.GetDataPtr()->AppendToDBusMessage(writer);
77}
78
79}  // namespace brillo
80