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

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gn build. 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
1 {%- macro validate_field(struct, name, kind) %}
yzshen1 2014/10/03 18:03:38 Please validate that both arrays for a map are of
Elliot Glaysher 2014/10/03 21:11:49 I believe that I've now added the validation code.
2 {%- if kind|is_map_kind %}
3 {%- set name_keys = name + "_keys" %}
4 {%- set name_values = name + "_values" %}
5 {{ validate_field(struct, name_keys, kind.key_kind) }}
6 {{ validate_field(struct, name_values, kind.value_kind) }}
7 {%- elif kind|is_object_kind %}
8 {%- set wrapper_type = kind|cpp_wrapper_type %}
9 {%- if not kind|is_nullable_kind %}
10 if (!object->{{name}}.offset) {
11 ReportValidationError(
12 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
13 "null {{name}} field in {{struct.name}} struct");
14 return false;
15 }
16 {%- endif %}
17 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
18 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
19 return false;
20 }
21 {%- if kind|is_any_array_kind or kind|is_string_kind %}
22 if (!{{wrapper_type}}::Data_::Validate<
23 {{kind|get_array_validate_params|indent(10)}}>(
24 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
25 bounds_checker)) {
26 {%- else %}
27 if (!{{wrapper_type}}::Data_::Validate(
28 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
29 bounds_checker)) {
30 {%- endif %}
31 return false;
32 }
33 {%- elif kind|is_any_handle_kind %}
34 {%- if not kind|is_nullable_kind %}
35 if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) {
36 ReportValidationError(
37 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
38 "invalid {{name}} field in {{struct.name}} struct");
39 return false;
40 }
41 {%- endif %}
42 if (!bounds_checker->ClaimHandle(object->{{name}})) {
43 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE);
44 return false;
45 }
46 {%- endif %}
47 {%- endmacro -%}
48
49
1 {%- macro validate(struct, class_name) %} 50 {%- macro validate(struct, class_name) %}
2 if (!data) 51 if (!data)
3 return true; 52 return true;
4 53
5 if (!ValidateStructHeader( 54 if (!ValidateStructHeader(
6 data, sizeof({{class_name}}), 55 data, sizeof({{class_name}}),
7 {{struct.packed.packed_fields|length}}, bounds_checker)) { 56 {{struct.packed.packed_fields|length}}, bounds_checker)) {
8 return false; 57 return false;
9 } 58 }
10 59
11 const {{class_name}}* MOJO_ALLOW_UNUSED object = 60 const {{class_name}}* MOJO_ALLOW_UNUSED object =
12 static_cast<const {{class_name}}*>(data); 61 static_cast<const {{class_name}}*>(data);
13 62
14 {%- for packed_field in struct.packed.packed_fields %} 63 {%- for packed_field in struct.packed.packed_fields %}
15 {%- set name = packed_field.field.name %} 64 {%- set name = packed_field.field.name %}
16 {%- set kind = packed_field.field.kind %} 65 {%- set kind = packed_field.field.kind %}
17 {%- if kind|is_object_kind %} 66 {{ validate_field(struct, name, kind) }}
18 {%- set wrapper_type = kind|cpp_wrapper_type %}
19 {%- if not kind|is_nullable_kind %}
20 if (!object->{{name}}.offset) {
21 ReportValidationError(
22 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
23 "null {{name}} field in {{struct.name}} struct");
24 return false;
25 }
26 {%- endif %}
27 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
28 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
29 return false;
30 }
31 {%- if kind|is_any_array_kind or kind|is_string_kind %}
32 if (!{{wrapper_type}}::Data_::Validate<
33 {{kind|get_array_validate_params|indent(10)}}>(
34 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
35 bounds_checker)) {
36 {%- else %}
37 if (!{{wrapper_type}}::Data_::Validate(
38 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
39 bounds_checker)) {
40 {%- endif %}
41 return false;
42 }
43 {%- elif kind|is_any_handle_kind %}
44 {%- if not kind|is_nullable_kind %}
45 if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) {
46 ReportValidationError(
47 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
48 "invalid {{name}} field in {{struct.name}} struct");
49 return false;
50 }
51 {%- endif %}
52 if (!bounds_checker->ClaimHandle(object->{{name}})) {
53 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE);
54 return false;
55 }
56 {%- endif %}
57 {%- endfor %} 67 {%- endfor %}
58 68
59 return true; 69 return true;
60 {%- endmacro %} 70 {%- endmacro %}
61 71
62 {%- macro field_line(field) %} 72 {%- macro field_line(field) %}
63 {%- set type = field.kind|cpp_field_type %}
64 {%- set name = field.name -%} 73 {%- set name = field.name -%}
65 {%- if field.kind.spec == 'b' -%} 74 {%- if field.kind.spec == 'b' -%}
66 uint8_t {{name}} : 1; 75 uint8_t {{name}} : 1;
67 {%- elif field.kind|is_enum_kind -%} 76 {%- elif field.kind|is_enum_kind -%}
68 int32_t {{name}}; 77 int32_t {{name}};
78 {%- elif field.kind|is_map_kind -%}
79 mojo::internal::ArrayPointer<{{field.kind.key_kind|cpp_type}}> {{name}}_keys;
80 mojo::internal::ArrayPointer<{{field.kind.value_kind|cpp_type}}> {{name}}_valu es;
69 {%- else -%} 81 {%- else -%}
70 {{type}} {{name}}; 82 {{field.kind|cpp_field_type}} {{name}};
71 {%- endif %} 83 {%- endif %}
72 {%- endmacro %} 84 {%- endmacro %}
73 85
74 {%- macro fields(struct) %} 86 {%- macro fields(struct) %}
75 {%- for packed_field in struct.packed.packed_fields %} 87 {%- for packed_field in struct.packed.packed_fields %}
76 {{field_line(packed_field.field)}} 88 {{field_line(packed_field.field)}}
77 {%- if not loop.last %} 89 {%- if not loop.last %}
78 {%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %} 90 {%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %}
79 {%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) % } 91 {%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) % }
80 {%- if pad > 0 %} 92 {%- if pad > 0 %}
81 uint8_t pad{{loop.index0}}_[{{pad}}]; 93 uint8_t pad{{loop.index0}}_[{{pad}}];
82 {%- endif %} 94 {%- endif %}
83 {%- endif %} 95 {%- endif %}
84 {%- endfor -%} 96 {%- endfor -%}
85 97
86 {%- set num_fields = struct.packed.packed_fields|length %} 98 {%- set num_fields = struct.packed.packed_fields|length %}
87 {%- if num_fields > 0 %} 99 {%- if num_fields > 0 %}
88 {%- set last_field = struct.packed.packed_fields[num_fields - 1] %} 100 {%- set last_field = struct.packed.packed_fields[num_fields - 1] %}
89 {%- set offset = last_field.offset + last_field.size %} 101 {%- set offset = last_field.offset + last_field.size %}
90 {%- set pad = offset|get_pad(8) -%} 102 {%- set pad = offset|get_pad(8) -%}
91 {%- if pad > 0 %} 103 {%- if pad > 0 %}
92 uint8_t padfinal_[{{pad}}]; 104 uint8_t padfinal_[{{pad}}];
93 {%- endif %} 105 {%- endif %}
94 {%- endif %} 106 {%- endif %}
95 {%- endmacro %} 107 {%- endmacro %}
96 108
97 {%- macro encodes(struct) -%} 109 {%- macro encodes(struct) -%}
98 {%- for pf in struct.packed.packed_fields %} 110 {%- for pf in struct.packed.packed_fields %}
99 {%- if pf.field.kind|is_object_kind %} 111 {%- if pf.field.kind|is_map_kind %}
112 mojo::internal::Encode(&{{pf.field.name}}_keys, handles);
113 mojo::internal::Encode(&{{pf.field.name}}_values, handles);
114 {%- elif pf.field.kind|is_object_kind %}
100 mojo::internal::Encode(&{{pf.field.name}}, handles); 115 mojo::internal::Encode(&{{pf.field.name}}, handles);
101 {%- elif pf.field.kind|is_any_handle_kind %} 116 {%- elif pf.field.kind|is_any_handle_kind %}
102 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); 117 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
103 {%- endif %} 118 {%- endif %}
104 {%- endfor %} 119 {%- endfor %}
105 {%- endmacro -%} 120 {%- endmacro -%}
106 121
107 {%- macro decodes(struct) -%} 122 {%- macro decodes(struct) -%}
108 {%- for pf in struct.packed.packed_fields %} 123 {%- for pf in struct.packed.packed_fields %}
109 {%- if pf.field.kind|is_object_kind %} 124 {%- if pf.field.kind|is_map_kind %}
125 mojo::internal::Decode(&{{pf.field.name}}_keys, handles);
126 mojo::internal::Decode(&{{pf.field.name}}_values, handles);
127 {%- elif pf.field.kind|is_object_kind %}
110 mojo::internal::Decode(&{{pf.field.name}}, handles); 128 mojo::internal::Decode(&{{pf.field.name}}, handles);
111 {%- elif pf.field.kind|is_any_handle_kind %} 129 {%- elif pf.field.kind|is_any_handle_kind %}
112 mojo::internal::DecodeHandle(&{{pf.field.name}}, handles); 130 mojo::internal::DecodeHandle(&{{pf.field.name}}, handles);
113 {%- endif %} 131 {%- endif %}
114 {%- endfor %} 132 {%- endfor %}
115 {%- endmacro -%} 133 {%- endmacro -%}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698