any.cc revision 9ed0cab99f18acb3570a35e9408f24355f6b8324
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 (GetType() != rhs.GetType())
38    return false;
39
40  if (IsEmpty())
41    return true;
42
43  return data_buffer_.GetDataPtr()->CompareEqual(rhs.data_buffer_.GetDataPtr());
44}
45
46const std::type_info& Any::GetType() const {
47  if (!IsEmpty())
48    return data_buffer_.GetDataPtr()->GetType();
49
50  struct NullType {};  // Special helper type representing an empty variant.
51  return typeid(NullType);
52}
53
54void Any::Swap(Any& other) {
55  std::swap(data_buffer_, other.data_buffer_);
56}
57
58bool Any::IsEmpty() const {
59  return data_buffer_.IsEmpty();
60}
61
62void Any::Clear() {
63  data_buffer_.Clear();
64}
65
66bool Any::IsConvertibleToInteger() const {
67  return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger();
68}
69
70intmax_t Any::GetAsInteger() const {
71  CHECK(!IsEmpty()) << "Must not be called on an empty Any";
72  return data_buffer_.GetDataPtr()->GetAsInteger();
73}
74
75void Any::AppendToDBusMessageWriter(dbus::MessageWriter* writer) const {
76  CHECK(!IsEmpty()) << "Must not be called on an empty Any";
77  data_buffer_.GetDataPtr()->AppendToDBusMessage(writer);
78}
79
80}  // namespace brillo
81