| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 | 399 |
| 410 void DecodePointersAndHandles(std::vector<Handle>* handles) { | 400 void DecodePointersAndHandles(std::vector<Handle>* handles) { |
| 411 Helper::DecodePointersAndHandles(&header_, storage(), handles); | 401 Helper::DecodePointersAndHandles(&header_, storage(), handles); |
| 412 } | 402 } |
| 413 | 403 |
| 414 private: | 404 private: |
| 415 Array_Data(uint32_t num_bytes, uint32_t num_elements) { | 405 Array_Data(uint32_t num_bytes, uint32_t num_elements) { |
| 416 header_.num_bytes = num_bytes; | 406 header_.num_bytes = num_bytes; |
| 417 header_.num_elements = num_elements; | 407 header_.num_elements = num_elements; |
| 418 } | 408 } |
| 419 ~Array_Data() {} | 409 ~Array_Data() = delete; |
| 420 | 410 |
| 421 internal::ArrayHeader header_; | 411 internal::ArrayHeader header_; |
| 422 | 412 |
| 423 // Elements of type internal::ArrayDataTraits<T>::StorageType follow. | 413 // Elements of type internal::ArrayDataTraits<T>::StorageType follow. |
| 424 }; | 414 }; |
| 425 static_assert(sizeof(Array_Data<char>) == 8, "Bad sizeof(Array_Data)"); | 415 static_assert(sizeof(Array_Data<char>) == 8, "Bad sizeof(Array_Data)"); |
| 426 | 416 |
| 427 // UTF-8 encoded | 417 // UTF-8 encoded |
| 428 typedef Array_Data<char> String_Data; | 418 typedef Array_Data<char> String_Data; |
| 429 | 419 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 | 503 |
| 514 template <> | 504 template <> |
| 515 struct WrapperTraits<String, false> { | 505 struct WrapperTraits<String, false> { |
| 516 typedef String_Data* DataType; | 506 typedef String_Data* DataType; |
| 517 }; | 507 }; |
| 518 | 508 |
| 519 } // namespace internal | 509 } // namespace internal |
| 520 } // namespace mojo | 510 } // namespace mojo |
| 521 | 511 |
| 522 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 512 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
| OLD | NEW |