Home | History | Annotate | Download | only in brillo
      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 
      9 namespace brillo {
     10 
     11 Any::Any() {
     12 }
     13 
     14 Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) {
     15 }
     16 
     17 // NOLINTNEXTLINE(build/c++11)
     18 Any::Any(Any&& rhs) : data_buffer_(std::move(rhs.data_buffer_)) {
     19 }
     20 
     21 Any::~Any() {
     22 }
     23 
     24 Any& Any::operator=(const Any& rhs) {
     25   data_buffer_ = rhs.data_buffer_;
     26   return *this;
     27 }
     28 
     29 // NOLINTNEXTLINE(build/c++11)
     30 Any& Any::operator=(Any&& rhs) {
     31   data_buffer_ = std::move(rhs.data_buffer_);
     32   return *this;
     33 }
     34 
     35 bool 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 
     46 const char* Any::GetTypeTagInternal() const {
     47   if (!IsEmpty())
     48     return data_buffer_.GetDataPtr()->GetTypeTag();
     49 
     50   return "";
     51 }
     52 
     53 void Any::Swap(Any& other) {
     54   std::swap(data_buffer_, other.data_buffer_);
     55 }
     56 
     57 bool Any::IsEmpty() const {
     58   return data_buffer_.IsEmpty();
     59 }
     60 
     61 void Any::Clear() {
     62   data_buffer_.Clear();
     63 }
     64 
     65 bool Any::IsConvertibleToInteger() const {
     66   return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger();
     67 }
     68 
     69 intmax_t Any::GetAsInteger() const {
     70   CHECK(!IsEmpty()) << "Must not be called on an empty Any";
     71   return data_buffer_.GetDataPtr()->GetAsInteger();
     72 }
     73 
     74 void 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