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