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

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

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gn build. 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
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
new file mode 100644
index 0000000000000000000000000000000000000000..0fe7b4531c2d11c87999d281cab07425f25d5e94
--- /dev/null
+++ b/mojo/public/cpp/bindings/lib/map_serialization.h
@@ -0,0 +1,121 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
+
+#include "mojo/public/cpp/bindings/lib/array_internal.h"
+#include "mojo/public/cpp/bindings/lib/map_internal.h"
+
+namespace mojo {
+namespace internal {
+
+template <typename MapType, typename DataType,
+ bool move_only = IsMoveOnlyType<MapType>::value>
yzshen1 2014/10/03 18:03:38 nit: the format kMoveOnly is used in map_internal.
+struct MapSerializer;
+
+template <typename MapType, typename DataType>
+struct MapSerializer<MapType, DataType, false> {
+ static size_t GetHeaderSize() {
+ return sizeof(Array_Data<DataType>);
+ }
+ static size_t GetBaseArraySize(size_t count) {
+ return Align(count * sizeof(DataType));
+ }
+ static size_t GetItemSize(const MapType& item) {
+ return 0;
+ }
+};
+
+template <>
+struct MapSerializer<bool, bool, false> {
+ static size_t GetHeaderSize() {
+ return sizeof(Array_Data<bool>);
+ }
+ static size_t GetBaseArraySize(size_t count) {
+ return Align((count + 7) / 8);
+ }
+ static size_t GetItemSize(bool item) {
+ return 0;
+ }
+};
+
+template <typename H>
+struct MapSerializer<ScopedHandleBase<H>, H, true> {
+ static size_t GetHeaderSize() {
+ return sizeof(Array_Data<H>);
+ }
+ static size_t GetBaseArraySize(size_t count) {
+ return Align(count * sizeof(H));
+ }
+ static size_t GetItemSize(const H& item) {
+ return 0;
+ }
+};
+
+template <typename S>
+struct MapSerializer<S, typename S::Data_*, true> {
+ static size_t GetHeaderSize() {
+ return sizeof(Array_Data<typename S::Data_*>);
+ }
+ static size_t GetBaseArraySize(size_t count) {
+ return count * sizeof(internal::StructPointer<typename S::Data_>);
+ }
+ static size_t GetItemSize(const S& item) {
+ return GetSerializedSize_(item);
+ }
+};
+
+template <>
+struct MapSerializer<String, String_Data*, false> {
+ static size_t GetHeaderSize() {
+ return sizeof(Array_Data<String_Data*>);
+ }
+ static size_t GetBaseArraySize(size_t count) {
+ return count * sizeof(internal::StringPointer);
+ }
+ static size_t GetItemSize(const String& item) {
+ return GetSerializedSize_(item);
+ }
+};
+
+template <typename MapKey, typename MapValue,
+ typename DataKey, typename DataValue>
+struct SizeAccumulator {
+ SizeAccumulator(size_t* data_size) : size(data_size) {}
+ size_t* size;
+
+ void operator()(const MapKey& key, const MapValue& value) {
+ *size += internal::MapSerializer<MapKey, DataKey>::GetItemSize(key);
+ *size += internal::MapSerializer<MapValue, DataValue>::GetItemSize(value);
+ }
+};
+
+} // namespace internal
+
+template <typename MapKey, typename MapValue>
+inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) {
yzshen1 2014/10/03 18:03:38 I probably haven't understood: I thought it is oka
Elliot Glaysher 2014/10/03 21:11:49 The problem is that there's no such thing as KeyAr
+ if (!input)
+ return 0;
+ typedef typename internal::WrapperTraits<MapKey>::DataType DataKey;
+ typedef typename internal::WrapperTraits<MapValue>::DataType DataValue;
+
+ size_t count = input.size();
+ size_t base_size =
+ internal::MapSerializer<MapKey, DataKey>::GetHeaderSize() +
+ internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count) +
+ internal::MapSerializer<MapValue, DataValue>::GetHeaderSize() +
+ internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count);
+
+ size_t data_size;
+ internal::SizeAccumulator<MapKey, MapValue, DataKey, DataValue> accumulator(
+ &data_size);
+ input.Iterate(accumulator);
+
+ return base_size + data_size;
+}
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_

Powered by Google App Engine
This is Rietveld 408576698