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

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

Issue 317273006: Add serialization/deserialization of structs for mojo java bindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 {% from "constant_definition.tmpl" import constant_def %} 1 {% from "constant_definition.tmpl" import constant_def %}
2 {% from "enum_definition.tmpl" import enum_def %} 2 {% from "enum_definition.tmpl" import enum_def %}
3 3
4 {% macro encode(variable, kind, offset, bit, level=0) %}
5 {% if kind|is_pointer_array_kind %}
6 {% set sub_kind = kind.kind %}
7 if ({{variable}} == null) {
8 encoder{{level}}.encodeNullPointer({{offset}});
9 } else {
10 org.chromium.mojo.bindings.Encoder encoder{{level + 1}} = encoder{{level}}.e ncodePointerArray({{variable}}.length, {{offset}});
11 for (int i{{level}} = 0; i{{level}} < {{variable}}.length; ++i{{level}}) {
12 {{encode(variable~'[i'~level~']', sub_kind, 'DataHeader.HEADER_SIZE + or g.chromium.mojo.bindings.BindingsHelper.POINTER_SIZE * i'~level, 0, level+1)|ind ent(8)}}
13 }
14 }
15 {% else %}
16 encoder{{level}}.{{kind|encode_method(variable, offset, bit)}};
17 {% endif %}
18 {% endmacro %}
19
20 {% macro decode(variable, kind, offset, bit, level=0) %}
21 {% if kind|is_struct_kind or kind|is_pointer_array_kind %}
22 org.chromium.mojo.bindings.Decoder decoder{{level+1}} = decoder{{level}}.readPoi nter({{offset}});
23 {% if kind|is_struct_kind %}
24 {{variable}} = {{kind|java_type}}.decode(decoder{{level+1}});
25 {% else %}{# kind|is_pointer_array_kind #}
26 if (decoder{{level+1}} == null) {
27 {{variable}} = null;
28 } else {
29 DataHeader si{{level+1}} = decoder{{level+1}}.readDataHeader();
30 {{variable}} = {{kind|new_array('si'~(level+1)~'.numFields')}};
31 for (int i{{level+1}} = 0; i{{level+1}} < si{{level+1}}.numFields; ++i{{leve l+1}}) {
32 {{decode(variable~'[i'~(level+1)~']', kind.kind, 'DataHeader.HEADER_SIZE + org.chromium.mojo.bindings.BindingsHelper.POINTER_SIZE * i'~(level+1), 0, lev el+1)|indent(8)}}
33 }
34 }
35 {% endif %}
36 {% else %}
37 {{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}};
38 {% endif %}
39 {% endmacro %}
40
4 {% macro struct_def(struct, inner_class=False) %} 41 {% macro struct_def(struct, inner_class=False) %}
5 {{'static' if inner_class else 'public'}} final class {{struct|name}} extends or g.chromium.mojo.bindings.Struct { 42 {{'static' if inner_class else 'public'}} final class {{struct|name}} extends or g.chromium.mojo.bindings.Struct {
43
44 private static final int STRUCT_SIZE = {{struct.packed|struct_size}};
45 private static final DataHeader DEFAULT_STRUCT_INFO = new DataHeader(STRUCT_ SIZE, {{struct.packed.packed_fields|length}});
6 {% for constant in struct.constants %} 46 {% for constant in struct.constants %}
7 47
8 {{constant_def(constant)|indent(4)}} 48 {{constant_def(constant)|indent(4)}}
9 {% endfor %} 49 {% endfor %}
10 {% for enum in struct.enums %} 50 {% for enum in struct.enums %}
11 51
12 {{enum_def(enum, false)|indent(4)}} 52 {{enum_def(enum, false)|indent(4)}}
13 {% endfor %} 53 {% endfor %}
14 {% if struct.fields %} 54 {% if struct.fields %}
15 55
16 {% for field in struct.fields %} 56 {% for field in struct.fields %}
17 public {{field.kind|java_type}} {{field|name}}; 57 public {{field.kind|java_type}} {{field|name}};
18 {% endfor %} 58 {% endfor %}
19 {% endif %} 59 {% endif %}
20 60
21 public {{struct|name}}() { 61 public {{struct|name}}() {
62 super(STRUCT_SIZE);
22 {% for field in struct.fields %} 63 {% for field in struct.fields %}
23 {% if field.default %} 64 {% if field.default %}
24 {{field|name}} = {{field|default_value}}; 65 {{field|name}} = {{field|default_value}};
25 {% elif field.kind|is_handle %} 66 {% elif field.kind|is_handle %}
26 {{field|name}} = org.chromium.mojo.system.InvalidHandle.INSTANCE; 67 {{field|name}} = org.chromium.mojo.system.InvalidHandle.INSTANCE;
27 {% endif %} 68 {% endif %}
28 {% endfor %} 69 {% endfor %}
29 } 70 }
30 71
72 public static {{struct|name}} deserialize(org.chromium.mojo.bindings.Message message) {
73 return decode(new org.chromium.mojo.bindings.Decoder(message));
74 }
75
76 public static {{struct|name}} decode(org.chromium.mojo.bindings.Decoder deco der0) {
77 if (decoder0 == null) {
78 return null;
79 }
80 {{struct|name}} result = new {{struct|name}}();
81 DataHeader mainDataHeader = decoder0.readDataHeader();
82 {% for byte in struct.bytes %}
83 {% for packed_field in byte.packed_fields %}
84 if (mainDataHeader.numFields > {{packed_field.ordinal}}) {
85 {{decode('result.' ~ packed_field.field|name, packed_field.field.kin d, 8+packed_field.offset, packed_field.bit)|indent(12)}}
86 }
87 {% endfor %}
88 {% endfor %}
89 return result;
90 }
91
92 @Override
93 protected final void encode(org.chromium.mojo.bindings.Encoder encoder) {
94 org.chromium.mojo.bindings.Encoder encoder0 = encoder.getEncoderAtDataOf fset(DEFAULT_STRUCT_INFO);
95 {% for byte in struct.bytes %}
96 {% for packed_field in byte.packed_fields %}
97 {{encode(packed_field.field|name, packed_field.field.kind, 8+packed_fiel d.offset, packed_field.bit)|indent(8)}}
98 {% endfor %}
99 {% endfor %}
100 }
31 } 101 }
32 {% endmacro %} 102 {% endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698