yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 1 | // Copyright 2016 The Chromium 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 | #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_DATA_VIEW_H_ |
| 6 | #define MOJO_PUBLIC_CPP_BINDINGS_MAP_DATA_VIEW_H_ |
| 7 | |
| 8 | #include "base/logging.h" |
yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 9 | #include "mojo/public/cpp/bindings/array_data_view.h" |
| 10 | #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
yzshen | 6dc9035 | 2016-08-24 09:12:57 | [diff] [blame] | 11 | #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 12 | #include "mojo/public/cpp/bindings/lib/serialization_context.h" |
| 13 | #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 14 | |
| 15 | namespace mojo { |
| 16 | |
| 17 | template <typename K, typename V> |
| 18 | class MapDataView { |
| 19 | public: |
yzshen | 6dc9035 | 2016-08-24 09:12:57 | [diff] [blame] | 20 | using Data_ = typename internal::MojomTypeTraits<MapDataView<K, V>>::Data; |
yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 21 | |
| 22 | MapDataView() {} |
| 23 | |
| 24 | MapDataView(Data_* data, internal::SerializationContext* context) |
| 25 | : keys_(data ? data->keys.Get() : nullptr, context), |
| 26 | values_(data ? data->values.Get() : nullptr, context) {} |
| 27 | |
| 28 | bool is_null() const { |
| 29 | DCHECK_EQ(keys_.is_null(), values_.is_null()); |
| 30 | return keys_.is_null(); |
| 31 | } |
| 32 | |
| 33 | size_t size() const { |
| 34 | DCHECK_EQ(keys_.size(), values_.size()); |
| 35 | return keys_.size(); |
| 36 | } |
| 37 | |
| 38 | ArrayDataView<K>& keys() { return keys_; } |
| 39 | const ArrayDataView<K>& keys() const { return keys_; } |
| 40 | |
| 41 | template <typename U> |
| 42 | bool ReadKeys(U* output) { |
yzshen | 6dc9035 | 2016-08-24 09:12:57 | [diff] [blame] | 43 | return internal::Deserialize<ArrayDataView<K>>(keys_.data_, output, |
| 44 | keys_.context_); |
yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ArrayDataView<V>& values() { return values_; } |
| 48 | const ArrayDataView<V>& values() const { return values_; } |
| 49 | |
| 50 | template <typename U> |
| 51 | bool ReadValues(U* output) { |
yzshen | 6dc9035 | 2016-08-24 09:12:57 | [diff] [blame] | 52 | return internal::Deserialize<ArrayDataView<V>>(values_.data_, output, |
| 53 | values_.context_); |
yzshen | 3854898 | 2016-07-23 03:38:53 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
| 57 | ArrayDataView<K> keys_; |
| 58 | ArrayDataView<V> values_; |
| 59 | }; |
| 60 | |
| 61 | } // namespace mojo |
| 62 | |
| 63 | #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_DATA_VIEW_H_ |