1{% from "constant_definition.tmpl" import constant_def %} 2{% from "enum_definition.tmpl" import enum_def %} 3 4{%- macro array_expected_length(kind) -%} 5{%- if kind|is_fixed_array_kind -%} 6{{kind.length}} 7{%- else -%} 8org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH 9{%- endif -%} 10{%- endmacro -%} 11 12{% macro encode(variable, kind, offset, bit, level=0) %} 13{% if kind|is_pointer_array_kind %} 14{% set sub_kind = kind.kind %} 15if ({{variable}} == null) { 16 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|java_true_false}}); 17} else { 18 org.chromium.mojo.bindings.Encoder encoder{{level + 1}} = encoder{{level}}.encodePointerArray({{variable}}.length, {{offset}}, {{array_expected_length(kind)}}); 19 for (int i{{level}} = 0; i{{level}} < {{variable}}.length; ++i{{level}}) { 20 {{encode(variable~'[i'~level~']', sub_kind, 'DataHeader.HEADER_SIZE + org.chromium.mojo.bindings.BindingsHelper.POINTER_SIZE * i'~level, 0, level+1)|indent(8)}} 21 } 22} 23{% else %} 24encoder{{level}}.{{kind|encode_method(variable, offset, bit)}}; 25{% endif %} 26{% endmacro %} 27 28{% macro decode(variable, kind, offset, bit, level=0) %} 29{% if kind|is_struct_kind or kind|is_pointer_array_kind %} 30org.chromium.mojo.bindings.Decoder decoder{{level+1}} = decoder{{level}}.readPointer({{offset}}, {{kind|is_nullable_kind|java_true_false}}); 31{% if kind|is_struct_kind %} 32{{variable}} = {{kind|java_type}}.decode(decoder{{level+1}}); 33{% else %}{# kind|is_pointer_array_kind #} 34if (decoder{{level+1}} == null) { 35 {{variable}} = null; 36} else { 37 DataHeader si{{level+1}} = decoder{{level+1}}.readDataHeaderForPointerArray({{array_expected_length(kind)}}); 38 {{variable}} = {{kind|new_array('si'~(level+1)~'.numFields')}}; 39 for (int i{{level+1}} = 0; i{{level+1}} < si{{level+1}}.numFields; ++i{{level+1}}) { 40 {{decode(variable~'[i'~(level+1)~']', kind.kind, 'DataHeader.HEADER_SIZE + org.chromium.mojo.bindings.BindingsHelper.POINTER_SIZE * i'~(level+1), 0, level+1)|indent(8)}} 41 } 42} 43{% endif %} 44{% else %} 45{{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}}; 46{% endif %} 47{% endmacro %} 48 49{% macro struct_def(struct, inner_class=False) %} 50{{'static' if inner_class else 'public'}} final class {{struct|name}} extends org.chromium.mojo.bindings.Struct { 51 52 private static final int STRUCT_SIZE = {{struct.packed|struct_size}}; 53 private static final DataHeader DEFAULT_STRUCT_INFO = new DataHeader(STRUCT_SIZE, {{struct.packed.packed_fields|length}}); 54{% for constant in struct.constants %} 55 56 {{constant_def(constant)|indent(4)}} 57{% endfor %} 58{% for enum in struct.enums %} 59 60 {{enum_def(enum, false)|indent(4)}} 61{% endfor %} 62{% if struct.fields %} 63 64{% for field in struct.fields %} 65 public {{field.kind|java_type}} {{field|name}}; 66{% endfor %} 67{% endif %} 68 69 public {{struct|name}}() { 70 super(STRUCT_SIZE); 71{% for field in struct.fields %} 72{% if field.default %} 73 {{field|name}} = {{field|default_value}}; 74{% elif field.kind|is_handle %} 75 {{field|name}} = org.chromium.mojo.system.InvalidHandle.INSTANCE; 76{% endif %} 77{% endfor %} 78 } 79 80 public static {{struct|name}} deserialize(org.chromium.mojo.bindings.Message message) { 81 return decode(new org.chromium.mojo.bindings.Decoder(message)); 82 } 83 84 public static {{struct|name}} decode(org.chromium.mojo.bindings.Decoder decoder0) { 85 if (decoder0 == null) { 86 return null; 87 } 88 {{struct|name}} result = new {{struct|name}}(); 89{% if not struct.bytes %} 90 decoder0.readDataHeader(); 91{% else %} 92 DataHeader mainDataHeader = decoder0.readDataHeader(); 93{% endif %} 94{% for byte in struct.bytes %} 95{% for packed_field in byte.packed_fields %} 96 if (mainDataHeader.numFields > {{packed_field.ordinal}}) { 97 {{decode('result.' ~ packed_field.field|name, packed_field.field.kind, 8+packed_field.offset, packed_field.bit)|indent(12)}} 98 } 99{% endfor %} 100{% endfor %} 101 return result; 102 } 103 104 @Override 105 protected final void encode(org.chromium.mojo.bindings.Encoder encoder) { 106{% if not struct.bytes %} 107 encoder.getEncoderAtDataOffset(DEFAULT_STRUCT_INFO); 108{% else %} 109 org.chromium.mojo.bindings.Encoder encoder0 = encoder.getEncoderAtDataOffset(DEFAULT_STRUCT_INFO); 110{% endif %} 111{% for byte in struct.bytes %} 112{% for packed_field in byte.packed_fields %} 113 {{encode(packed_field.field|name, packed_field.field.kind, 8+packed_field.offset, packed_field.bit)|indent(8)}} 114{% endfor %} 115{% endfor %} 116 } 117} 118{% endmacro %} 119