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

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

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 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 {%- import "encoding_macros.tmpl" as encoding_macros %}
3 {%- macro struct_def(struct, typepkg, package) %}
4 {#--- Enums #}
5 {%- from "enum_definition.tmpl" import enum_def %}
6 {%- for enum in struct.enums %}
7 {{enum_def(enum, typepkg, package)}}
8 {%- endfor %}
9
10 class {{struct|name}} extends bindings.Struct {
11 static const List<bindings.StructDataHeader> kVersions = const [
12 {%- for version in struct.versions %}
13 const bindings.StructDataHeader({{version.num_bytes}}, {{version.version}}){ % if not loop.last %},{% endif %}
14 {%- endfor %}
15 ];
16
17 {#--- Constants #}
18 {%- for constant in struct.constants %}
19 static const {{constant.kind|dart_type}} {{constant|name}} = {{constant.resolv ed_value}};
20 {%- endfor %}
21
22 {#--- initDefaults() #}
23 {%- for packed_field in struct.packed.packed_fields %}
24 {{packed_field.field.kind|dart_type}} {{packed_field.field|name}} = {{packed_f ield.field|default_value}};
25 {%- endfor %}
26
27 {{struct|name}}() : super(kVersions.last.size);
28
29 {{struct|name}}.init(
30 {%- for packed_field in struct.packed.packed_fields %}
31 {{packed_field.field.kind|dart_type}} this.{{packed_field.field|name}}{% if not loop.last %}, {% endif %}
32 {%- endfor %}
33 ) : super(kVersions.last.size);
34
35 static {{struct|name}} deserialize(bindings.Message message) =>
36 bindings.Struct.deserialize(decode, message);
37
38 static {{struct|name}} decode(bindings.Decoder decoder0) {
39 if (decoder0 == null) {
40 return null;
41 }
42 {{struct|name}} result = new {{struct|name}}();
43
44 {%- if struct.bytes %}
45 var mainDataHeader = bindings.Struct.checkVersion(decoder0, kVersions);
46 {%- else %}
47 bindings.Struct.checkVersion(decoder0, kVersions);
48 {%- endif %}
49 {%- for byte in struct.bytes %}
50 {%- for packed_field in byte.packed_fields %}
51 if (mainDataHeader.version >= {{packed_field.min_version}}) {
52 {{encoding_macros.decode('result.' ~ packed_field.field|name, packed_field .field.kind, 8+packed_field.offset, packed_field.bit)|indent(6)}}
53 }
54 {%- endfor %}
55 {%- endfor %}
56 return result;
57 }
58
59 void encode(bindings.Encoder encoder) {
60 {%- if not struct.bytes %}
61 encoder.getStructEncoderAtOffset(kVersions.last);
62 {%- else %}
63 var encoder0 = encoder.getStructEncoderAtOffset(kVersions.last);
64 {%- endif %}
65 const String structName = "{{struct|name}}";
66 String fieldName;
67 try {
68 {%- for byte in struct.bytes %}
69 {%- for packed_field in byte.packed_fields %}
70 fieldName = "{{packed_field.field|name}}";
71 {{encoding_macros.encode(packed_field.field|name, packed_field.field.kind, 8+packed_field.offset, packed_field.bit)|indent(6)}}
72 {%- endfor %}
73 {%- endfor %}
74 } on bindings.MojoCodecError catch(e) {
75 bindings.Struct.fixErrorMessage(e, fieldName, structName);
76 rethrow;
77 }
78 }
79
80 String toString() {
81 return "{{struct|name}}("
82 {%- for packed_field in struct.packed.packed_fields %}
83 "{{packed_field.field|name}}: ${{packed_field.field|name}}" {% if not loop.last %}", "{% endif %}
84 {%- endfor %}")";
85 }
86
87 Map toJson() {
88 {%- if struct|is_cloneable_kind %}
89 Map map = new Map();
90 {%- for packed_field in struct.packed.packed_fields %}
91 map["{{packed_field.field|name}}"] = {{packed_field.field|name}};
92 {%- endfor %}
93 return map;
94 {%- else %}
95 throw new bindings.MojoCodecError(
96 'Object containing handles cannot be encoded to JSON.');
97 {%- endif %}
98 }
99 }
100
101 {%- endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698