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

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

Issue 674383002: Initial work on Dart bindings for Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Merge. Work on templates. Created 6 years, 1 month 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 class {{struct.name}} implements bindings.MojoType<{{struct.name}}> {
4 {#--- Enums #}
5 {%- from "enum_definition.tmpl" import enum_def %}
6 {% for enum in struct.enums %}
7 {{enum_def(" static ", enum)}}
8 {%- endfor %}
9
10
11 {#--- Constants #}
12 {% for constant in struct.constants %}
13 static final {{constant.name}} = {{constant.value|expression_to_text}};
14 {%- endfor %}
15
16
17 {#--- initDefaults() #}
18 {%- for packed_field in struct.packed.packed_fields %}
19 {{packed_field.field.kind|dart_decl_type}} {{packed_field.field.name}} = {{pac ked_field.field|default_value}};
20 {%- endfor %}
21
22 {{struct.name}}();
23
24 {#--- Encoding and decoding #}
25
26 static const int encodedSize =
27 bindings.kStructHeaderSize + {{struct.packed|payload_size}};
28
29 static {{struct.name}} decode(bindings.MojoDecoder decoder) {
30 var packed;
31 var val = new {{struct.name}}();
32 var numberOfBytes = decoder.readUint32();
33 var numberOfFields = decoder.readUint32();
34 {%- for byte in struct.bytes %}
35 {%- if byte.packed_fields|length > 1 %}
36 packed = decoder.readUint8();
37 {%- for packed_field in byte.packed_fields %}
38 val.{{packed_field.field.name}} = (((packed >> {{packed_field.bit}}) & 1) != 0) ? true : false;
39 {%- endfor %}
40 {%- else %}
41 {%- for packed_field in byte.packed_fields %}
42 val.{{packed_field.field.name}} = decoder.{{packed_field.field.kind|decode_s nippet}};
43 {%- endfor %}
44 {%- endif %}
45 {%- if byte.is_padding %}
46 decoder.skip(1);
47 {%- endif %}
48 {%- endfor %}
49 return val;
50 }
51
52 static void encode(bindings.MojoEncoder encoder, {{struct.name}} val) {
53 var packed;
54 encoder.writeUint32({{struct.name}}.encodedSize);
55 encoder.writeUint32({{struct.packed.packed_fields|length}});
56
57 {%- for byte in struct.bytes %}
58 {%- if byte.packed_fields|length > 1 %}
59 packed = 0;
60 {%- for packed_field in byte.packed_fields %}
61 packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}}
62 {%- endfor %}
63 encoder.writeUint8(packed);
64 {%- else %}
65 {%- for packed_field in byte.packed_fields %}
66 encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.n ame}});
67 {%- endfor %}
68 {%- endif %}
69 {%- if byte.is_padding %}
70 encoder.skip(1);
71 {%- endif %}
72 {%- endfor %}
73 }
74 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698