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 Any()11Any::Any() { 12 } 13 Any(const Any & rhs)14Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) { 15 } 16 17 // NOLINTNEXTLINE(build/c++11) Any(Any && rhs)18Any::Any(Any&& rhs) : data_buffer_(std::move(rhs.data_buffer_)) { 19 } 20 ~Any()21Any::~Any() { 22 } 23 operator =(const Any & rhs)24Any& Any::operator=(const Any& rhs) { 25 data_buffer_ = rhs.data_buffer_; 26 return *this; 27 } 28 29 // NOLINTNEXTLINE(build/c++11) operator =(Any && rhs)30Any& Any::operator=(Any&& rhs) { 31 data_buffer_ = std::move(rhs.data_buffer_); 32 return *this; 33 } 34 operator ==(const Any & rhs) const35bool 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 GetTypeTagInternal() const46const char* Any::GetTypeTagInternal() const { 47 if (!IsEmpty()) 48 return data_buffer_.GetDataPtr()->GetTypeTag(); 49 50 return ""; 51 } 52 Swap(Any & other)53void Any::Swap(Any& other) { 54 std::swap(data_buffer_, other.data_buffer_); 55 } 56 IsEmpty() const57bool Any::IsEmpty() const { 58 return data_buffer_.IsEmpty(); 59 } 60 Clear()61void Any::Clear() { 62 data_buffer_.Clear(); 63 } 64 IsConvertibleToInteger() const65bool Any::IsConvertibleToInteger() const { 66 return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger(); 67 } 68 GetAsInteger() const69intmax_t Any::GetAsInteger() const { 70 CHECK(!IsEmpty()) << "Must not be called on an empty Any"; 71 return data_buffer_.GetDataPtr()->GetAsInteger(); 72 } 73 AppendToDBusMessageWriter(dbus::MessageWriter * writer) const74void 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