Alex Vakulenko | b6513a1 | 2014-05-05 17:23:40 -0700 | [diff] [blame] | 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 "buffet/any.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | namespace buffet { |
| 10 | |
| 11 | Any::Any(const Any& rhs) : data_buffer_(rhs.data_buffer_) { |
| 12 | } |
| 13 | |
| 14 | Any::~Any() { |
| 15 | } |
| 16 | |
| 17 | Any& Any::operator=(const Any& rhs) { |
| 18 | data_buffer_ = rhs.data_buffer_; |
| 19 | return *this; |
| 20 | } |
| 21 | |
| 22 | const std::type_info& Any::GetType() const { |
| 23 | if (!IsEmpty()) |
| 24 | return data_buffer_.GetDataPtr()->GetType(); |
| 25 | |
| 26 | struct NullType {}; // Special helper type representing an empty variant. |
| 27 | return typeid(NullType); |
| 28 | } |
| 29 | |
| 30 | void Any::Swap(Any& other) { |
| 31 | std::swap(data_buffer_, other.data_buffer_); |
| 32 | } |
| 33 | |
| 34 | bool Any::IsEmpty() const { |
| 35 | return data_buffer_.IsEmpty(); |
| 36 | } |
| 37 | |
| 38 | void Any::Clear() { |
| 39 | data_buffer_.Clear(); |
| 40 | } |
| 41 | |
| 42 | bool Any::IsConvertibleToInteger() const { |
| 43 | return !IsEmpty() && data_buffer_.GetDataPtr()->IsConvertibleToInteger(); |
| 44 | } |
| 45 | |
| 46 | intmax_t Any::GetAsInteger() const { |
| 47 | CHECK(!IsEmpty()) << "Must not be called on an empty Any"; |
| 48 | return data_buffer_.GetDataPtr()->GetAsInteger(); |
| 49 | } |
| 50 | |
| 51 | } // namespace buffet |