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 #include <utility>
9 
10 namespace brillo {
11 
Any()12 Any::Any() {
13 }
14 
Any(const Any & rhs)15 Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) {
16 }
17 
18 // NOLINTNEXTLINE(build/c++11)
Any(Any && rhs)19 Any::Any(Any&& rhs) : data_buffer_(std::move(rhs.data_buffer_)) {
20 }
21 
~Any()22 Any::~Any() {
23 }
24 
operator =(const Any & rhs)25 Any& Any::operator=(const Any& rhs) {
26   data_buffer_ = rhs.data_buffer_;
27   return *this;
28 }
29 
30 // NOLINTNEXTLINE(build/c++11)
operator =(Any && rhs)31 Any& Any::operator=(Any&& rhs) {
32   data_buffer_ = std::move(rhs.data_buffer_);
33   return *this;
34 }
35 
operator ==(const Any & rhs) const36 bool Any::operator==(const Any& rhs) const {
37   // Make sure both objects contain data of the same type.
38   if (strcmp(GetTypeTagInternal(), rhs.GetTypeTagInternal()) != 0)
39     return false;
40 
41   if (IsEmpty())
42     return true;
43 
44   return data_buffer_.GetDataPtr()->CompareEqual(rhs.data_buffer_.GetDataPtr());
45 }
46 
GetTypeTagInternal() const47 const char* Any::GetTypeTagInternal() const {
48   if (!IsEmpty())
49     return data_buffer_.GetDataPtr()->GetTypeTag();
50 
51   return "";
52 }
53 
Swap(Any & other)54 void Any::Swap(Any& other) {
55   std::swap(data_buffer_, other.data_buffer_);
56 }
57 
IsEmpty() const58 bool Any::IsEmpty() const {
59   return data_buffer_.IsEmpty();
60 }
61 
Clear()62 void Any::Clear() {
63   data_buffer_.Clear();
64 }
65 
IsConvertibleToInteger() const66 bool Any::IsConvertibleToInteger() const {
67   return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger();
68 }
69 
GetAsInteger() const70 intmax_t Any::GetAsInteger() const {
71   CHECK(!IsEmpty()) << "Must not be called on an empty Any";
72   return data_buffer_.GetDataPtr()->GetAsInteger();
73 }
74 
AppendToDBusMessageWriter(dbus::MessageWriter * writer) const75 void 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