Index: mojo/public/cpp/bindings/lib/map_serialization.h |
diff --git a/mojo/public/cpp/bindings/lib/map_serialization.h b/mojo/public/cpp/bindings/lib/map_serialization.h |
index fb84c93f3813bc44581ece374eba35379b179204..0b184f820a9f0e9bcdc7b88006014bbf8dc29c77 100644 |
--- a/mojo/public/cpp/bindings/lib/map_serialization.h |
+++ b/mojo/public/cpp/bindings/lib/map_serialization.h |
@@ -6,6 +6,7 @@ |
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
#include "mojo/public/cpp/bindings/lib/array_internal.h" |
+#include "mojo/public/cpp/bindings/lib/array_serialization.h" |
#include "mojo/public/cpp/bindings/lib/map_internal.h" |
namespace mojo { |
@@ -92,6 +93,23 @@ struct SizeAccumulator { |
} |
}; |
+ |
+template <typename MapKey> |
+struct MapKeyValidateParams { |
+ public: |
+ typedef NoValidateParams ElementValidateParams; |
+ static const uint32_t expected_num_elements = 0; |
+ static const bool element_is_nullable = false; |
+}; |
+ |
+ |
+template <> struct MapKeyValidateParams<String> { |
+ public: |
+ typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams; |
+ static const uint32_t expected_num_elements = 0; |
+ static const bool element_is_nullable = false; |
+}; |
+ |
} // namespace internal |
template <typename MapKey, typename MapValue> |
@@ -125,6 +143,54 @@ inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) { |
value_base_size + value_data_size; |
} |
+ |
+// We don't need a KeyValidateParams, because we konw exactly what params are |
+// needed. (Keys are primitive types or non-nullable strings.) |
+template <typename ValueValidateParams, |
+ typename KeyWrapperType, |
+ typename ValueWrapperType, |
+ typename KeySerializationType, |
+ typename ValueSerializationType> |
+inline void SerializeMap_( |
+ Map<KeyWrapperType, ValueWrapperType> input, |
+ internal::Buffer* buf, |
+ internal::MapPointerPair<KeySerializationType, |
+ ValueSerializationType>* output) { |
+ if (input) { |
+ Array<KeyWrapperType> keys; |
+ Array<ValueWrapperType> values; |
+ input.DecomposeMapTo(&keys, &values); |
+ SerializeArray_<internal::MapKeyValidateParams<KeyWrapperType>>( |
+ keys.Pass(), buf, &output->keys.ptr); |
+ SerializeArray_<ValueValidateParams>(values.Pass(), buf, |
+ &output->values.ptr); |
+ } else { |
+ output->keys.ptr = nullptr; |
+ output->values.ptr = nullptr; |
+ } |
+} |
+ |
+template <typename KeyWrapperType, |
+ typename ValueWrapperType, |
+ typename KeySerializationType, |
+ typename ValueSerializationType> |
+inline void Deserialize_( |
+ internal::MapPointerPair<KeySerializationType, |
+ ValueSerializationType>* input, |
+ Map<KeyWrapperType, ValueWrapperType>* output) { |
+ if (input) { |
+ Array<KeyWrapperType> keys; |
+ Array<ValueWrapperType> values; |
+ |
+ Deserialize_(input->keys.ptr, &keys); |
+ Deserialize_(input->values.ptr, &values); |
+ |
+ *output = Map<KeyWrapperType, ValueWrapperType>(keys.Pass(), values.Pass()); |
+ } else { |
+ output->reset(); |
+ } |
+} |
+ |
} // namespace mojo |
#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |