| 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_data_internal.h" | |
| 10 #include "mojo/public/cpp/bindings/lib/map_internal.h" | |
| 11 #include "mojo/public/cpp/bindings/lib/string_serialization.h" | |
| 12 #include "mojo/public/cpp/bindings/map.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 | |
| 16 template <typename Key, typename Value> | |
| 17 inline size_t GetSerializedSize_(const Map<Key, Value>& input); | |
| 18 | |
| 19 template <typename ValidateParams, typename E, typename F> | |
| 20 inline void SerializeArray_( | |
| 21 Array<E> input, | |
| 22 internal::Buffer* buf, | |
| 23 internal::Array_Data<F>** output, | |
| 24 const internal::ArrayValidateParams* validate_params); | |
| 25 | |
| 26 namespace internal { | |
| 27 | |
| 28 template <typename MapType, | |
| 29 typename DataType, | |
| 30 bool value_is_move_only_type = IsMoveOnlyType<MapType>::value, | |
| 31 bool is_union = | |
| 32 IsUnionDataType<typename RemovePointer<DataType>::type>::value> | |
| 33 struct MapSerializer; | |
| 34 | |
| 35 template <typename MapType, typename DataType> | |
| 36 struct MapSerializer<MapType, DataType, false, false> { | |
| 37 static size_t GetBaseArraySize(size_t count) { | |
| 38 return Align(count * sizeof(DataType)); | |
| 39 } | |
| 40 static size_t GetItemSize(const MapType& item) { return 0; } | |
| 41 }; | |
| 42 | |
| 43 template <> | |
| 44 struct MapSerializer<bool, bool, false, false> { | |
| 45 static size_t GetBaseArraySize(size_t count) { | |
| 46 return Align((count + 7) / 8); | |
| 47 } | |
| 48 static size_t GetItemSize(bool item) { return 0; } | |
| 49 }; | |
| 50 | |
| 51 template <typename H> | |
| 52 struct MapSerializer<ScopedHandleBase<H>, H, true, false> { | |
| 53 static size_t GetBaseArraySize(size_t count) { | |
| 54 return Align(count * sizeof(H)); | |
| 55 } | |
| 56 static size_t GetItemSize(const ScopedHandleBase<H>& item) { return 0; } | |
| 57 }; | |
| 58 | |
| 59 // This template must only apply to pointer mojo entity (structs and arrays). | |
| 60 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. | |
| 61 template <typename S> | |
| 62 struct MapSerializer< | |
| 63 S, | |
| 64 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, | |
| 65 typename WrapperTraits<S>::DataType>::type, | |
| 66 true, | |
| 67 false> { | |
| 68 typedef | |
| 69 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; | |
| 70 static size_t GetBaseArraySize(size_t count) { | |
| 71 return count * sizeof(StructPointer<S_Data>); | |
| 72 } | |
| 73 static size_t GetItemSize(const S& item) { return GetSerializedSize_(item); } | |
| 74 }; | |
| 75 | |
| 76 template <typename U, typename U_Data> | |
| 77 struct MapSerializer<U, U_Data*, true, true> { | |
| 78 static size_t GetBaseArraySize(size_t count) { | |
| 79 return count * sizeof(U_Data); | |
| 80 } | |
| 81 static size_t GetItemSize(const U& item) { | |
| 82 return GetSerializedSize_(item, true); | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 template <> | |
| 87 struct MapSerializer<String, String_Data*, false, false> { | |
| 88 static size_t GetBaseArraySize(size_t count) { | |
| 89 return count * sizeof(StringPointer); | |
| 90 } | |
| 91 static size_t GetItemSize(const String& item) { | |
| 92 return GetSerializedSize_(item); | |
| 93 } | |
| 94 }; | |
| 95 | |
| 96 } // namespace internal | |
| 97 | |
| 98 // TODO(erg): This can't go away yet. We still need to calculate out the size | |
| 99 // of a struct header, and two arrays. | |
| 100 template <typename MapKey, typename MapValue> | |
| 101 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) { | |
| 102 if (!input) | |
| 103 return 0; | |
| 104 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey; | |
| 105 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue; | |
| 106 | |
| 107 size_t count = input.size(); | |
| 108 size_t struct_overhead = sizeof(mojo::internal::Map_Data<DataKey, DataValue>); | |
| 109 size_t key_base_size = | |
| 110 sizeof(internal::ArrayHeader) + | |
| 111 internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count); | |
| 112 size_t value_base_size = | |
| 113 sizeof(internal::ArrayHeader) + | |
| 114 internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count); | |
| 115 | |
| 116 size_t key_data_size = 0; | |
| 117 size_t value_data_size = 0; | |
| 118 for (auto it = input.begin(); it != input.end(); ++it) { | |
| 119 key_data_size += | |
| 120 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey()); | |
| 121 value_data_size += | |
| 122 internal::MapSerializer<MapValue, DataValue>::GetItemSize( | |
| 123 it.GetValue()); | |
| 124 } | |
| 125 | |
| 126 return struct_overhead + key_base_size + key_data_size + value_base_size + | |
| 127 value_data_size; | |
| 128 } | |
| 129 | |
| 130 // We don't need an ArrayValidateParams instance for key validation since | |
| 131 // we can deduce it from the Key type. (which can only be primitive types or | |
| 132 // non-nullable strings.) | |
| 133 template <typename MapKey, | |
| 134 typename MapValue, | |
| 135 typename DataKey, | |
| 136 typename DataValue> | |
| 137 inline void SerializeMap_( | |
| 138 Map<MapKey, MapValue> input, | |
| 139 internal::Buffer* buf, | |
| 140 internal::Map_Data<DataKey, DataValue>** output, | |
| 141 const internal::ArrayValidateParams* value_validate_params) { | |
| 142 if (input) { | |
| 143 internal::Map_Data<DataKey, DataValue>* result = | |
| 144 internal::Map_Data<DataKey, DataValue>::New(buf); | |
| 145 if (result) { | |
| 146 Array<MapKey> keys; | |
| 147 Array<MapValue> values; | |
| 148 input.DecomposeMapTo(&keys, &values); | |
| 149 const internal::ArrayValidateParams* key_validate_params = | |
| 150 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | |
| 151 SerializeArray_(keys.Pass(), buf, &result->keys.ptr, key_validate_params); | |
| 152 SerializeArray_(values.Pass(), buf, &result->values.ptr, | |
| 153 value_validate_params); | |
| 154 } | |
| 155 *output = result; | |
| 156 } else { | |
| 157 *output = nullptr; | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 template <typename MapKey, | |
| 162 typename MapValue, | |
| 163 typename DataKey, | |
| 164 typename DataValue> | |
| 165 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, | |
| 166 Map<MapKey, MapValue>* output) { | |
| 167 if (input) { | |
| 168 Array<MapKey> keys; | |
| 169 Array<MapValue> values; | |
| 170 | |
| 171 Deserialize_(input->keys.ptr, &keys); | |
| 172 Deserialize_(input->values.ptr, &values); | |
| 173 | |
| 174 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); | |
| 175 } else { | |
| 176 output->reset(); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 } // namespace mojo | |
| 181 | |
| 182 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | |
| OLD | NEW |