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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
8 #include "mojo/public/cpp/bindings/lib/array_internal.h"
9 #include "mojo/public/cpp/bindings/lib/validate_params.h"
10 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
15 // Data types for keys.
16 template <typename MapKey>
17 struct MapKeyValidateParams {
19 typedef NoValidateParams ElementValidateParams;
20 static const uint32_t expected_num_elements = 0;
21 static const bool element_is_nullable = false;
24 // For non-nullable strings only. (Which is OK; map keys can't be null.)
26 struct MapKeyValidateParams<mojo::internal::Array_Data<char>*> {
28 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams;
29 static const uint32_t expected_num_elements = 0;
30 static const bool element_is_nullable = false;
33 // Map serializes into a struct which has two arrays as struct fields, the keys
35 template <typename Key, typename Value>
38 static Map_Data* New(Buffer* buf) {
39 return new (buf->Allocate(sizeof(Map_Data))) Map_Data();
42 template <typename ValueParams>
43 static bool Validate(const void* data, BoundsChecker* bounds_checker) {
47 if (!ValidateStructHeader(data, sizeof(Map_Data), 2, bounds_checker))
50 const Map_Data* object = static_cast<const Map_Data*>(data);
51 if (!ValidateEncodedPointer(&object->keys.offset)) {
52 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER);
55 if (!object->keys.offset) {
56 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
57 "null key array in map struct");
60 if (!Array_Data<Key>::template Validate<MapKeyValidateParams<Key>>(
61 DecodePointerRaw(&object->keys.offset), bounds_checker)) {
65 if (!ValidateEncodedPointer(&object->values.offset)) {
66 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER);
69 if (!object->values.offset) {
70 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
71 "null value array in map struct");
74 if (!Array_Data<Value>::template Validate<ValueParams>(
75 DecodePointerRaw(&object->values.offset), bounds_checker)) {
79 const ArrayHeader* key_header =
80 static_cast<const ArrayHeader*>(DecodePointerRaw(&object->keys.offset));
81 const ArrayHeader* value_header = static_cast<const ArrayHeader*>(
82 DecodePointerRaw(&object->values.offset));
83 if (key_header->num_elements != value_header->num_elements) {
84 ReportValidationError(VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP);
93 ArrayPointer<Key> keys;
94 ArrayPointer<Value> values;
96 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) {
97 Encode(&keys, handles);
98 Encode(&values, handles);
101 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) {
102 Decode(&keys, handles);
103 Decode(&values, handles);
108 header_.num_bytes = sizeof(*this);
109 header_.num_fields = 2;
111 ~Map_Data() = delete;
113 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)");
115 } // namespace internal
118 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_