Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/map_data_internal.h |
| diff --git a/mojo/public/cpp/bindings/lib/map_data_internal.h b/mojo/public/cpp/bindings/lib/map_data_internal.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2754f11b3078aae265f78085fce979b6f6f22bb7 |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/map_data_internal.h |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |
| +#define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |
| + |
| +#include "mojo/public/cpp/bindings/lib/array_internal.h" |
| +#include "mojo/public/cpp/bindings/lib/validate_params.h" |
| +#include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| + |
| +namespace mojo { |
| +namespace internal { |
| + |
| +template <typename MapKey> |
| +struct MapDataKeyValidateParams { |
| + public: |
| + typedef NoValidateParams ElementValidateParams; |
| + static const uint32_t expected_num_elements = 0; |
| + static const bool element_is_nullable = false; |
| +}; |
| + |
| +template <> |
| +struct MapDataKeyValidateParams<mojo::internal::Array_Data<char>*> { |
|
yzshen1
2014/10/08 21:58:32
Please add a comment saying that this is for non-n
|
| + public: |
| + typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams; |
| + static const uint32_t expected_num_elements = 0; |
| + static const bool element_is_nullable = false; |
| +}; |
| + |
| +// Map serializes into a struct which has two arrays as struct fields, the keys |
| +// and the values. |
| +template <typename First, typename Second> |
|
yzshen1
2014/10/08 21:58:32
nit: calling them Key and Value may be more consis
|
| +class Map_Data { |
| + public: |
| + static Map_Data* New(mojo::internal::Buffer* buf) { |
| + return new (buf->Allocate(sizeof(Map_Data))) Map_Data(); |
| + } |
| + |
| + template <typename ValueParams> |
| + static bool Validate(const void* data, |
| + mojo::internal::BoundsChecker* bounds_checker) { |
| + if (!data) |
| + return true; |
| + |
| + if (!ValidateStructHeader(data, sizeof(Map_Data), 2, bounds_checker)) |
| + return false; |
| + |
| + const Map_Data* MOJO_ALLOW_UNUSED object = |
|
yzshen1
2014/10/08 21:58:32
no need to have MOJO_ALLOW_UNUSED. (we have this i
|
| + static_cast<const Map_Data*>(data); |
| + if (!mojo::internal::ValidateEncodedPointer(&object->first.offset)) { |
|
yzshen1
2014/10/08 21:58:32
we are already in mojo::internal (here and elsewhe
|
| + ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
| + return false; |
| + } |
| + if (!object->first.offset) { |
| + ReportValidationError( |
| + mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| + "null key array in map struct"); |
| + return false; |
| + } |
| + if (!internal::Array_Data<First>::template Validate< |
|
yzshen1
2014/10/08 21:58:32
We are already in internal:: (here and elsewhere)
|
| + MapDataKeyValidateParams<First>>( |
| + mojo::internal::DecodePointerRaw(&object->first.offset), |
|
yzshen1
2014/10/08 21:58:32
indent 4 more spaces?
Elliot Glaysher
2014/10/08 23:40:49
This patch was run through git cl-format.
|
| + bounds_checker)) { |
| + return false; |
| + } |
| + |
| + if (!mojo::internal::ValidateEncodedPointer(&object->second.offset)) { |
| + ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
| + return false; |
| + } |
| + if (!object->second.offset) { |
| + ReportValidationError( |
| + mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| + "null value array in map struct"); |
| + return false; |
| + } |
| + if (!internal::Array_Data<Second>::template Validate<ValueParams>( |
| + mojo::internal::DecodePointerRaw(&object->second.offset), |
| + bounds_checker)) { |
| + return false; |
| + } |
| + |
| + const ArrayHeader* first_header = static_cast<const ArrayHeader*>( |
| + mojo::internal::DecodePointerRaw(&object->first.offset)); |
| + const ArrayHeader* second_header = static_cast<const ArrayHeader*>( |
| + mojo::internal::DecodePointerRaw(&object->second.offset)); |
| + if (first_header->num_elements != second_header->num_elements) { |
| + ReportValidationError( |
| + mojo::internal::VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP); |
| + return false; |
| + } |
| + |
| + return true; |
| + } |
| + |
| + mojo::internal::StructHeader header_; |
| + |
| + mojo::internal::ArrayPointer<First> first; |
| + mojo::internal::ArrayPointer<Second> second; |
| + |
| + void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) { |
| + mojo::internal::Encode(&first, handles); |
| + mojo::internal::Encode(&second, handles); |
| + } |
| + |
| + void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) { |
| + mojo::internal::Decode(&first, handles); |
| + mojo::internal::Decode(&second, handles); |
| + } |
| + |
| + private: |
| + Map_Data() { |
| + header_.num_bytes = sizeof(*this); |
| + header_.num_fields = 2; |
| + } |
| + ~Map_Data() = delete; |
|
yzshen1
2014/10/08 21:58:32
This makes sense. And I think we should do the sam
|
| +}; |
| + |
|
yzshen1
2014/10/08 21:58:32
maybe we can static_assert the size (of some Map_D
|
| +} // namespace internal |
| +} // namespace mojo |
| + |
| +#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |