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" 9 #include "mojo/public/cpp/bindings/array_data_view.h" 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 11 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" 12 #include "mojo/public/cpp/bindings/lib/serialization_context.h" 13 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" 14 15 namespace mojo { 16 17 template <typename K, typename V> 18 class MapDataView { 19 public: 20 using Data_ = typename internal::MojomTypeTraits<MapDataView<K, V>>::Data; 21 MapDataView()22 MapDataView() {} 23 MapDataView(Data_ * data,internal::SerializationContext * context)24 MapDataView(Data_* data, internal::SerializationContext* context) 25 : keys_(data ? data->keys.Get() : nullptr, context), 26 values_(data ? data->values.Get() : nullptr, context) {} 27 is_null()28 bool is_null() const { 29 DCHECK_EQ(keys_.is_null(), values_.is_null()); 30 return keys_.is_null(); 31 } 32 size()33 size_t size() const { 34 DCHECK_EQ(keys_.size(), values_.size()); 35 return keys_.size(); 36 } 37 keys()38 ArrayDataView<K>& keys() { return keys_; } keys()39 const ArrayDataView<K>& keys() const { return keys_; } 40 41 template <typename U> ReadKeys(U * output)42 bool ReadKeys(U* output) { 43 return internal::Deserialize<ArrayDataView<K>>(keys_.data_, output, 44 keys_.context_); 45 } 46 values()47 ArrayDataView<V>& values() { return values_; } values()48 const ArrayDataView<V>& values() const { return values_; } 49 50 template <typename U> ReadValues(U * output)51 bool ReadValues(U* output) { 52 return internal::Deserialize<ArrayDataView<V>>(values_.data_, output, 53 values_.context_); 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_ 64