Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(714)

Unified Diff: mojo/public/cpp/bindings/lib/map_serialization.h

Issue 632603002: Mojo C++ bindings: support Arrays that contain Maps. Base URL: https://chromium.googlesource.com/chromium/src.git@4_mojo_map
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_internal.h ('k') | mojo/public/cpp/bindings/tests/map_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_internal.h ('k') | mojo/public/cpp/bindings/tests/map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698