diff options
Diffstat (limited to 'brillo/any.cc')
-rw-r--r-- | brillo/any.cc | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/brillo/any.cc b/brillo/any.cc new file mode 100644 index 0000000..30e875b --- /dev/null +++ b/brillo/any.cc @@ -0,0 +1,80 @@ +// Copyright 2014 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <brillo/any.h> + +#include <algorithm> + +namespace brillo { + +Any::Any() { +} + +Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) { +} + +// NOLINTNEXTLINE(build/c++11) +Any::Any(Any&& rhs) : data_buffer_(std::move(rhs.data_buffer_)) { +} + +Any::~Any() { +} + +Any& Any::operator=(const Any& rhs) { + data_buffer_ = rhs.data_buffer_; + return *this; +} + +// NOLINTNEXTLINE(build/c++11) +Any& Any::operator=(Any&& rhs) { + data_buffer_ = std::move(rhs.data_buffer_); + return *this; +} + +bool Any::operator==(const Any& rhs) const { + // Make sure both objects contain data of the same type. + if (GetType() != rhs.GetType()) + return false; + + if (IsEmpty()) + return true; + + return data_buffer_.GetDataPtr()->CompareEqual(rhs.data_buffer_.GetDataPtr()); +} + +const std::type_info& Any::GetType() const { + if (!IsEmpty()) + return data_buffer_.GetDataPtr()->GetType(); + + struct NullType {}; // Special helper type representing an empty variant. + return typeid(NullType); +} + +void Any::Swap(Any& other) { + std::swap(data_buffer_, other.data_buffer_); +} + +bool Any::IsEmpty() const { + return data_buffer_.IsEmpty(); +} + +void Any::Clear() { + data_buffer_.Clear(); +} + +bool Any::IsConvertibleToInteger() const { + return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger(); +} + +intmax_t Any::GetAsInteger() const { + CHECK(!IsEmpty()) << "Must not be called on an empty Any"; + return data_buffer_.GetDataPtr()->GetAsInteger(); +} + +void Any::AppendToDBusMessageWriter(dbus::MessageWriter* writer) const { + CHECK(!IsEmpty()) << "Must not be called on an empty Any"; + data_buffer_.GetDataPtr()->AppendToDBusMessage(writer); +} + +} // namespace brillo |