Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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_LIB_MAP_SERIALIZATION_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | |
| 7 | |
| 8 #include "mojo/public/cpp/bindings/lib/array_internal.h" | |
| 9 #include "mojo/public/cpp/bindings/lib/map_internal.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 namespace internal { | |
| 13 | |
| 14 template <typename MapType, typename DataType, | |
| 15 bool move_only = IsMoveOnlyType<MapType>::value> | |
|
yzshen1
2014/10/03 18:03:38
nit: the format kMoveOnly is used in map_internal.
| |
| 16 struct MapSerializer; | |
| 17 | |
| 18 template <typename MapType, typename DataType> | |
| 19 struct MapSerializer<MapType, DataType, false> { | |
| 20 static size_t GetHeaderSize() { | |
| 21 return sizeof(Array_Data<DataType>); | |
| 22 } | |
| 23 static size_t GetBaseArraySize(size_t count) { | |
| 24 return Align(count * sizeof(DataType)); | |
| 25 } | |
| 26 static size_t GetItemSize(const MapType& item) { | |
| 27 return 0; | |
| 28 } | |
| 29 }; | |
| 30 | |
| 31 template <> | |
| 32 struct MapSerializer<bool, bool, false> { | |
| 33 static size_t GetHeaderSize() { | |
| 34 return sizeof(Array_Data<bool>); | |
| 35 } | |
| 36 static size_t GetBaseArraySize(size_t count) { | |
| 37 return Align((count + 7) / 8); | |
| 38 } | |
| 39 static size_t GetItemSize(bool item) { | |
| 40 return 0; | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 template <typename H> | |
| 45 struct MapSerializer<ScopedHandleBase<H>, H, true> { | |
| 46 static size_t GetHeaderSize() { | |
| 47 return sizeof(Array_Data<H>); | |
| 48 } | |
| 49 static size_t GetBaseArraySize(size_t count) { | |
| 50 return Align(count * sizeof(H)); | |
| 51 } | |
| 52 static size_t GetItemSize(const H& item) { | |
| 53 return 0; | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 template <typename S> | |
| 58 struct MapSerializer<S, typename S::Data_*, true> { | |
| 59 static size_t GetHeaderSize() { | |
| 60 return sizeof(Array_Data<typename S::Data_*>); | |
| 61 } | |
| 62 static size_t GetBaseArraySize(size_t count) { | |
| 63 return count * sizeof(internal::StructPointer<typename S::Data_>); | |
| 64 } | |
| 65 static size_t GetItemSize(const S& item) { | |
| 66 return GetSerializedSize_(item); | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 template <> | |
| 71 struct MapSerializer<String, String_Data*, false> { | |
| 72 static size_t GetHeaderSize() { | |
| 73 return sizeof(Array_Data<String_Data*>); | |
| 74 } | |
| 75 static size_t GetBaseArraySize(size_t count) { | |
| 76 return count * sizeof(internal::StringPointer); | |
| 77 } | |
| 78 static size_t GetItemSize(const String& item) { | |
| 79 return GetSerializedSize_(item); | |
| 80 } | |
| 81 }; | |
| 82 | |
| 83 template <typename MapKey, typename MapValue, | |
| 84 typename DataKey, typename DataValue> | |
| 85 struct SizeAccumulator { | |
| 86 SizeAccumulator(size_t* data_size) : size(data_size) {} | |
| 87 size_t* size; | |
| 88 | |
| 89 void operator()(const MapKey& key, const MapValue& value) { | |
| 90 *size += internal::MapSerializer<MapKey, DataKey>::GetItemSize(key); | |
| 91 *size += internal::MapSerializer<MapValue, DataValue>::GetItemSize(value); | |
| 92 } | |
| 93 }; | |
| 94 | |
| 95 } // namespace internal | |
| 96 | |
| 97 template <typename MapKey, typename MapValue> | |
| 98 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
| |
| 99 if (!input) | |
| 100 return 0; | |
| 101 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey; | |
| 102 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue; | |
| 103 | |
| 104 size_t count = input.size(); | |
| 105 size_t base_size = | |
| 106 internal::MapSerializer<MapKey, DataKey>::GetHeaderSize() + | |
| 107 internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count) + | |
| 108 internal::MapSerializer<MapValue, DataValue>::GetHeaderSize() + | |
| 109 internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count); | |
| 110 | |
| 111 size_t data_size; | |
| 112 internal::SizeAccumulator<MapKey, MapValue, DataKey, DataValue> accumulator( | |
| 113 &data_size); | |
| 114 input.Iterate(accumulator); | |
| 115 | |
| 116 return base_size + data_size; | |
| 117 } | |
| 118 | |
| 119 } // namespace mojo | |
| 120 | |
| 121 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | |
| OLD | NEW |