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

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

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address yzshen's comments. 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
(Empty)
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.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
7
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"
11
12 namespace mojo {
13 namespace internal {
14
15 // Data types for keys. The partial specializations are different from the
yzshen1 2014/10/09 17:41:58 The 2nd sentence is not needed anymore because tho
16 // cases in map_serialization.h because these operate on the wrapper type, not
17 // the given type.
18 template <typename MapKey>
19 struct MapKeyValidateParams {
20 public:
21 typedef NoValidateParams ElementValidateParams;
22 static const uint32_t expected_num_elements = 0;
23 static const bool element_is_nullable = false;
24 };
25
26 // For non-nullable strings only. (Which is OK; map keys can't be null.)
27 template <>
28 struct MapKeyValidateParams<mojo::internal::Array_Data<char>*> {
29 public:
30 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams;
31 static const uint32_t expected_num_elements = 0;
32 static const bool element_is_nullable = false;
33 };
34
35 // Map serializes into a struct which has two arrays as struct fields, the keys
36 // and the values.
37 template <typename Key, typename Value>
38 class Map_Data {
39 public:
40 static Map_Data* New(Buffer* buf) {
41 return new (buf->Allocate(sizeof(Map_Data))) Map_Data();
42 }
43
44 template <typename ValueParams>
45 static bool Validate(const void* data, BoundsChecker* bounds_checker) {
46 if (!data)
47 return true;
48
49 if (!ValidateStructHeader(data, sizeof(Map_Data), 2, bounds_checker))
50 return false;
51
52 const Map_Data* object = static_cast<const Map_Data*>(data);
53 if (!ValidateEncodedPointer(&object->keys.offset)) {
54 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER);
55 return false;
56 }
57 if (!object->keys.offset) {
58 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
59 "null key array in map struct");
60 return false;
61 }
62 if (!Array_Data<Key>::template Validate<MapKeyValidateParams<Key>>(
63 DecodePointerRaw(&object->keys.offset), bounds_checker)) {
64 return false;
65 }
66
67 if (!ValidateEncodedPointer(&object->values.offset)) {
68 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER);
69 return false;
70 }
71 if (!object->values.offset) {
72 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
73 "null value array in map struct");
74 return false;
75 }
76 if (!Array_Data<Value>::template Validate<ValueParams>(
77 DecodePointerRaw(&object->values.offset), bounds_checker)) {
78 return false;
79 }
80
81 const ArrayHeader* key_header =
82 static_cast<const ArrayHeader*>(DecodePointerRaw(&object->keys.offset));
83 const ArrayHeader* value_header = static_cast<const ArrayHeader*>(
84 DecodePointerRaw(&object->values.offset));
85 if (key_header->num_elements != value_header->num_elements) {
86 ReportValidationError(VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP);
87 return false;
88 }
89
90 return true;
91 }
92
93 StructHeader header_;
94
95 ArrayPointer<Key> keys;
96 ArrayPointer<Value> values;
97
98 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) {
99 Encode(&keys, handles);
100 Encode(&values, handles);
101 }
102
103 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) {
104 Decode(&keys, handles);
105 Decode(&values, handles);
106 }
107
108 private:
109 Map_Data() {
110 header_.num_bytes = sizeof(*this);
111 header_.num_fields = 2;
112 }
113 ~Map_Data() = delete;
114 };
115 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)");
116
117 } // namespace internal
118 } // namespace mojo
119
120 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698