OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
7 | 7 |
8 #include <new> | 8 #include <new> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "mojo/public/c/system/macros.h" | 11 #include "mojo/public/c/system/macros.h" |
12 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 12 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
13 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | 13 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
14 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 14 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
15 #include "mojo/public/cpp/bindings/lib/buffer.h" | 15 #include "mojo/public/cpp/bindings/lib/buffer.h" |
| 16 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
16 #include "mojo/public/cpp/bindings/lib/template_util.h" | 17 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 18 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
17 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 19 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
18 #include "mojo/public/cpp/environment/logging.h" | 20 #include "mojo/public/cpp/environment/logging.h" |
19 | 21 |
20 namespace mojo { | 22 namespace mojo { |
21 template <typename T> | 23 template <typename T> |
22 class Array; | 24 class Array; |
23 class String; | 25 class String; |
24 | 26 |
25 namespace internal { | 27 namespace internal { |
26 | 28 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 return sizeof(ArrayHeader) + ((num_elements + 7) / 8); | 136 return sizeof(ArrayHeader) + ((num_elements + 7) / 8); |
135 } | 137 } |
136 static BitRef ToRef(StorageType* storage, size_t offset) { | 138 static BitRef ToRef(StorageType* storage, size_t offset) { |
137 return BitRef(&storage[offset / 8], 1 << (offset % 8)); | 139 return BitRef(&storage[offset / 8], 1 << (offset % 8)); |
138 } | 140 } |
139 static bool ToConstRef(const StorageType* storage, size_t offset) { | 141 static bool ToConstRef(const StorageType* storage, size_t offset) { |
140 return (storage[offset / 8] & (1 << (offset % 8))) != 0; | 142 return (storage[offset / 8] & (1 << (offset % 8))) != 0; |
141 } | 143 } |
142 }; | 144 }; |
143 | 145 |
144 // Array type information needed for valdiation. | |
145 template <uint32_t in_expected_num_elements, | |
146 bool in_element_is_nullable, | |
147 typename InElementValidateParams> | |
148 class ArrayValidateParams { | |
149 public: | |
150 // Validation information for elements. It is either another specialization of | |
151 // ArrayValidateParams (if elements are arrays) or NoValidateParams. | |
152 typedef InElementValidateParams ElementValidateParams; | |
153 | |
154 // If |expected_num_elements| is not 0, the array is expected to have exactly | |
155 // that number of elements. | |
156 static const uint32_t expected_num_elements = in_expected_num_elements; | |
157 // Whether the elements are nullable. | |
158 static const bool element_is_nullable = in_element_is_nullable; | |
159 }; | |
160 | |
161 // NoValidateParams is used to indicate the end of an ArrayValidateParams chain. | |
162 class NoValidateParams {}; | |
163 | |
164 // What follows is code to support the serialization of Array_Data<T>. There | 146 // What follows is code to support the serialization of Array_Data<T>. There |
165 // are two interesting cases: arrays of primitives and arrays of objects. | 147 // are two interesting cases: arrays of primitives and arrays of objects. |
166 // Arrays of objects are represented as arrays of pointers to objects. | 148 // Arrays of objects are represented as arrays of pointers to objects. |
167 | 149 |
168 template <typename T, bool is_handle> | 150 template <typename T, bool is_handle> |
169 struct ArraySerializationHelper; | 151 struct ArraySerializationHelper; |
170 | 152 |
171 template <typename T> | 153 template <typename T> |
172 struct ArraySerializationHelper<T, false> { | 154 struct ArraySerializationHelper<T, false> { |
173 typedef typename ArrayDataTraits<T>::StorageType ElementType; | 155 typedef typename ArrayDataTraits<T>::StorageType ElementType; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 template <typename T, typename Params> | 288 template <typename T, typename Params> |
307 struct ValidateCaller { | 289 struct ValidateCaller { |
308 static bool Run(const void* data, BoundsChecker* bounds_checker) { | 290 static bool Run(const void* data, BoundsChecker* bounds_checker) { |
309 static_assert((IsSame<Params, NoValidateParams>::value), | 291 static_assert((IsSame<Params, NoValidateParams>::value), |
310 "Struct type should not have array validate params"); | 292 "Struct type should not have array validate params"); |
311 | 293 |
312 return T::Validate(data, bounds_checker); | 294 return T::Validate(data, bounds_checker); |
313 } | 295 } |
314 }; | 296 }; |
315 | 297 |
| 298 template <typename Key, typename Value, typename Params> |
| 299 struct ValidateCaller<Map_Data<Key, Value>, Params> { |
| 300 static bool Run(const void* data, BoundsChecker* bounds_checker) { |
| 301 return Map_Data<Key, Value>::template Validate<Params>(data, |
| 302 bounds_checker); |
| 303 } |
| 304 }; |
| 305 |
316 template <typename T, typename Params> | 306 template <typename T, typename Params> |
317 struct ValidateCaller<Array_Data<T>, Params> { | 307 struct ValidateCaller<Array_Data<T>, Params> { |
318 static bool Run(const void* data, BoundsChecker* bounds_checker) { | 308 static bool Run(const void* data, BoundsChecker* bounds_checker) { |
319 return Array_Data<T>::template Validate<Params>(data, bounds_checker); | 309 return Array_Data<T>::template Validate<Params>(data, bounds_checker); |
320 } | 310 } |
321 }; | 311 }; |
322 }; | 312 }; |
323 | 313 |
324 template <typename T> | 314 template <typename T> |
325 class Array_Data { | 315 class Array_Data { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 366 |
377 const Array_Data<T>* object = static_cast<const Array_Data<T>*>(data); | 367 const Array_Data<T>* object = static_cast<const Array_Data<T>*>(data); |
378 return Helper::template ValidateElements< | 368 return Helper::template ValidateElements< |
379 Params::element_is_nullable, | 369 Params::element_is_nullable, |
380 typename Params::ElementValidateParams>( | 370 typename Params::ElementValidateParams>( |
381 &object->header_, object->storage(), bounds_checker); | 371 &object->header_, object->storage(), bounds_checker); |
382 } | 372 } |
383 | 373 |
384 size_t size() const { return header_.num_elements; } | 374 size_t size() const { return header_.num_elements; } |
385 | 375 |
| 376 const internal::ArrayHeader& header() { return header_; } |
| 377 |
386 Ref at(size_t offset) { | 378 Ref at(size_t offset) { |
387 MOJO_DCHECK(offset < static_cast<size_t>(header_.num_elements)); | 379 MOJO_DCHECK(offset < static_cast<size_t>(header_.num_elements)); |
388 return Traits::ToRef(storage(), offset); | 380 return Traits::ToRef(storage(), offset); |
389 } | 381 } |
390 | 382 |
391 ConstRef at(size_t offset) const { | 383 ConstRef at(size_t offset) const { |
392 MOJO_DCHECK(offset < static_cast<size_t>(header_.num_elements)); | 384 MOJO_DCHECK(offset < static_cast<size_t>(header_.num_elements)); |
393 return Traits::ToConstRef(storage(), offset); | 385 return Traits::ToConstRef(storage(), offset); |
394 } | 386 } |
395 | 387 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 | 505 |
514 template <> | 506 template <> |
515 struct WrapperTraits<String, false> { | 507 struct WrapperTraits<String, false> { |
516 typedef String_Data* DataType; | 508 typedef String_Data* DataType; |
517 }; | 509 }; |
518 | 510 |
519 } // namespace internal | 511 } // namespace internal |
520 } // namespace mojo | 512 } // namespace mojo |
521 | 513 |
522 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 514 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
OLD | NEW |