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

Side by Side Diff: mojo/public/cpp/bindings/lib/array_serialization.h

Issue 622593002: mojo: Allow circular dependencies between structs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
7 7
8 #include <string.h> // For |memcpy()|. 8 #include <string.h> // For |memcpy()|.
9 9
10 #include <vector> 10 #include <vector>
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 111 }
112 static void DeserializeElements( 112 static void DeserializeElements(
113 Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) { 113 Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) {
114 Array<ScopedHandleBase<H> > result(input->size()); 114 Array<ScopedHandleBase<H> > result(input->size());
115 for (size_t i = 0; i < input->size(); ++i) 115 for (size_t i = 0; i < input->size(); ++i)
116 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); 116 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i)));
117 output->Swap(&result); 117 output->Swap(&result);
118 } 118 }
119 }; 119 };
120 120
121 template <typename S> struct ArraySerializer<S, typename S::Data_*, true> { 121 template <typename S>
122 struct ArraySerializer<S,
123 typename internal::EnableIf<
124 internal::IsPointer<
125 typename internal::WrapperTraits<
126 S>::DataType>::value,
127 typename internal::WrapperTraits<
128 S>::DataType>::type,
129 true> {
130 typedef typename internal::RemovePointer<
131 typename internal::WrapperTraits<S>::DataType>::type S_Data;
122 static size_t GetSerializedSize(const Array<S>& input) { 132 static size_t GetSerializedSize(const Array<S>& input) {
123 size_t size = sizeof(Array_Data<typename S::Data_*>) + 133 size_t size = sizeof(Array_Data<S_Data*>) +
124 input.size() * sizeof(internal::StructPointer<typename S::Data_>); 134 input.size() * sizeof(internal::StructPointer<S_Data>);
125 for (size_t i = 0; i < input.size(); ++i) 135 for (size_t i = 0; i < input.size(); ++i)
126 size += GetSerializedSize_(input[i]); 136 size += GetSerializedSize_(input[i]);
127 return size; 137 return size;
128 } 138 }
129 template <bool element_is_nullable, typename ElementValidateParams> 139 template <bool element_is_nullable, typename ElementValidateParams>
130 static void SerializeElements(Array<S> input, 140 static void SerializeElements(Array<S> input,
131 Buffer* buf, 141 Buffer* buf,
132 Array_Data<typename S::Data_*>* output) { 142 Array_Data<S_Data*>* output) {
133 for (size_t i = 0; i < input.size(); ++i) { 143 for (size_t i = 0; i < input.size(); ++i) {
134 typename S::Data_* element; 144 S_Data* element;
135 SerializeCaller<S, ElementValidateParams>::Run( 145 SerializeCaller<S, ElementValidateParams>::Run(
136 input[i].Pass(), buf, &element); 146 input[i].Pass(), buf, &element);
137 output->at(i) = element; 147 output->at(i) = element;
138 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 148 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
139 !element_is_nullable && !element, 149 !element_is_nullable && !element,
140 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 150 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
141 MakeMessageWithArrayIndex( 151 MakeMessageWithArrayIndex(
142 "null in array expecting valid pointers", input.size(), i)); 152 "null in array expecting valid pointers", input.size(), i));
143 } 153 }
144 } 154 }
145 static void DeserializeElements( 155 static void DeserializeElements(
146 Array_Data<typename S::Data_*>* input, Array<S>* output) { 156 Array_Data<S_Data*>* input, Array<S>* output) {
147 Array<S> result(input->size()); 157 Array<S> result(input->size());
148 for (size_t i = 0; i < input->size(); ++i) { 158 for (size_t i = 0; i < input->size(); ++i) {
149 S element; 159 S element;
150 Deserialize_(input->at(i), &element); 160 Deserialize_(input->at(i), &element);
151 result[i] = element.Pass(); 161 result[i] = element.Pass();
152 } 162 }
153 output->Swap(&result); 163 output->Swap(&result);
154 } 164 }
155 165
156 private: 166 private:
157 template <typename T, typename Params> 167 template <typename T, typename Params>
158 struct SerializeCaller { 168 struct SerializeCaller {
159 static void Run(T input, Buffer* buf, typename T::Data_** output) { 169 static void Run(
170 T input,
171 Buffer* buf,
172 typename internal::WrapperTraits<T>::DataType* output) {
160 MOJO_COMPILE_ASSERT((IsSame<Params, NoValidateParams>::value), 173 MOJO_COMPILE_ASSERT((IsSame<Params, NoValidateParams>::value),
161 Struct_type_should_not_have_array_validate_params); 174 Struct_type_should_not_have_array_validate_params);
162 175
163 Serialize_(input.Pass(), buf, output); 176 Serialize_(input.Pass(), buf, output);
164 } 177 }
165 }; 178 };
166 179
167 template <typename T, typename Params> 180 template <typename T, typename Params>
168 struct SerializeCaller<Array<T>, Params> { 181 struct SerializeCaller<Array<T>, Params> {
169 static void Run(Array<T> input, 182 static void Run(Array<T> input,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 if (input) { 266 if (input) {
254 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 267 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
255 } else { 268 } else {
256 output->reset(); 269 output->reset();
257 } 270 }
258 } 271 }
259 272
260 } // namespace mojo 273 } // namespace mojo
261 274
262 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 275 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/bindings_internal.h » ('j') | mojo/public/cpp/bindings/lib/template_util.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698