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

Side by Side Diff: mojo/public/tools/bindings/generators/dart_templates/struct_definition.tmpl

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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 {#--- Begin #}
2
3 {%- macro encode(variable, kind, offset, bit, level=0, check_for_null=True) %}
4 {%- if kind|is_pointer_array_kind %}
5 {%- set sub_kind = kind.kind %}
6 {%- if check_for_null %}
7 if ({{variable}} == null) {
8 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|dart_tr ue_false}});
9 } else {
10 {%- else %}
11 {
12 {%- endif %}
13 var encoder{{level + 1}} = encoder{{level}}.encodePointerArray({{variable}}.le ngth, {{offset}}, {{kind|array_expected_length}});
14 for (int i{{level}} = 0; i{{level}} < {{variable}}.length; ++i{{level}}) {
15 {{encode(variable~'[i'~level~']', sub_kind, 'bindings.DataHeader.kHeaderSize + bindings.kPointerSize * i'~level, 0, level+1)|indent(4)}}
16 }
17 }
18 {%- elif kind|is_map_kind %}
19 if ({{variable}} == null) {
20 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|dart_tr ue_false}});
21 } else {
22 var encoder{{level + 1}} = encoder{{level}}.encoderForMap({{offset}});
23 int size{{level}} = {{variable}}.length;
24 var keys{{level}} = {{variable}}.keys.toList();
25 var values{{level}} = {{variable}}.values.toList();
26 {{encode('keys'~level, kind.key_kind|array, 'bindings.DataHeader.kHeaderSize', 0, level+1, False)|indent(2)}}
27 {{encode('values'~level, kind.value_kind|array, 'bindings.DataHeader.kHeaderSi ze + bindings.kPointerSize', 0, level+1, False)|indent(2)}}
28 }
29 {%- else %}
30 encoder{{level}}.{{kind|encode_method(variable, offset, bit)}};
31 {%- endif %}
32 {%- endmacro %}
33
34
35 {%- macro decode(variable, kind, offset, bit, level=0) %}
36 {%- if kind|is_struct_kind or kind|is_pointer_array_kind or kind|is_map_kind %}
37 var decoder{{level+1}} = decoder{{level}}.decodePointer({{offset}}, {{kind|is_nu llable_kind|dart_true_false}});
38 {%- if kind|is_struct_kind %}
39 {{variable}} = {{kind|dart_type}}.decode(decoder{{level+1}});
40 {%- else %}{# kind|is_pointer_array_kind or is_map_kind #}
41 {%- if kind|is_nullable_kind %}
42 if (decoder{{level+1}} == null) {
43 {{variable}} = null;
44 } else {
45 {%- else %}
46 {
47 {%- endif %}
48 {%- if kind|is_map_kind %}
49 decoder{{level+1}}.decodeDataHeaderForMap();
50 List<{{kind.key_kind|dart_type}}> keys{{level}};
51 List<{{kind.value_kind|dart_type}}> values{{level}};
52 {
53 {{decode('keys'~level, kind.key_kind|array, 'DataHeader.HEADER_SIZE', 0, lev el+1)|indent(4)}}
54 }
55 {
56 {{decode('values'~level, kind.value_kind|array('keys'~level~'.length'), 'bin dings.DataHeader.kHeaderSize + bindings.kPointerSize', 0, level+1)|indent(4)}}
57 }
58 {{variable}} = new Map<{{kind.key_kind|dart_type}}, {{kind.value_kind|dart_typ e}}>.fromIterables(keys, values);
59 {%- else %}
60 var si{{level+1}} = decoder{{level+1}}.decodeDataHeaderForPointerArray({{kind| array_expected_length}});
61 {{variable}} = new {{kind|dart_type}}(si{{level+1}}.numFields);
62 for (int i{{level+1}} = 0; i{{level+1}} < si{{level+1}}.numFields; ++i{{level+ 1}}) {
63 {{decode(variable~'[i'~(level+1)~']', kind.kind, 'bindings.DataHeader.kHeade rSize + bindings.kPointerSize * i'~(level+1), 0, level+1)|indent(4)}}
64 }
65 {%- endif %}
66 }
67 {%- endif %}
68 {%- else %}
69 {{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}};
70 {%- endif %}
71 {%- endmacro %}
72
73
74 {%- macro struct_def(struct) %}
75 class {{struct|name}} extends bindings.Struct {
76 static const int kStructSize = {{struct.packed|struct_size}};
77 static const bindings.DataHeader kDefaultStructInfo =
78 const bindings.DataHeader(kStructSize, {{struct.packed.packed_fields|lengt h}});
79
80 {#--- Enums #}
81 {%- from "enum_definition.tmpl" import enum_def %}
82 {%- for enum in struct.enums %}
83 {{enum_def(" static ", enum)}}
84 {%- endfor %}
85
86
87 {#--- Constants #}
88 {%- for constant in struct.constants %}
89 static final {{constant.name}} = {{constant.value|expression_to_text}};
90 {%- endfor %}
91
92 {#--- initDefaults() #}
93 {%- for packed_field in struct.packed.packed_fields %}
94 {{packed_field.field.kind|dart_type}} {{packed_field.field|name}} = {{packed_f ield.field|default_value}};
95 {%- endfor %}
96
97 {{struct|name}}() : super(kStructSize);
98
99 static {{struct|name}} deserialize(bindings.Message message) {
100 return decode(new bindings.Decoder(message));
101 }
102
103 static {{struct|name}} decode(bindings.Decoder decoder0) {
104 if (decoder0 == null) {
105 return null;
106 }
107 {{struct|name}} result = new {{struct|name}}();
108 {%- if not struct.bytes %}
109 decoder0.decodeDataHeader();
110 {%- else %}
111 var mainDataHeader = decoder0.decodeDataHeader();
112 {%- endif %}
113 {%- for byte in struct.bytes %}
114 {%- for packed_field in byte.packed_fields %}
115 if (mainDataHeader.numFields > {{packed_field.ordinal}}) {
116 {{decode('result.' ~ packed_field.field|name, packed_field.field.kind, 8+p acked_field.offset, packed_field.bit)|indent(6)}}
117 }
118 {%- endfor %}
119 {%- endfor %}
120 return result;
121 }
122
123 void encode(bindings.Encoder encoder) {
124 {%- if not struct.bytes %}
125 encoder.getEncoderAtOffset(kDefaultStructInfo);
126 {%- else %}
127 var encoder0 = encoder.getEncoderAtOffset(kDefaultStructInfo);
128 {%- endif %}
129 {%- for byte in struct.bytes %}
130 {%- for packed_field in byte.packed_fields %}
131 {{encode(packed_field.field|name, packed_field.field.kind, 8+packed_field.of fset, packed_field.bit)|indent(4)}}
132 {%- endfor %}
133 {%- endfor %}
134 }
135 }
136 {%- endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698