OLD | NEW |
| (Empty) |
1 {% from "constant_definition.tmpl" import constant_def %} | |
2 {% from "enum_definition.tmpl" import enum_def %} | |
3 | |
4 {%- macro inequality(kind, v1, v2) -%} | |
5 {%- if kind|is_reference_kind -%} | |
6 {%- if kind|is_array_kind -%} | |
7 {%- if kind.kind|is_reference_kind -%} | |
8 !java.util.Arrays.deepEquals({{v1}}, {{v2}}) | |
9 {%- else -%} | |
10 !java.util.Arrays.equals({{v1}}, {{v2}}) | |
11 {%- endif -%} | |
12 {%- else -%} | |
13 !org.chromium.mojo.bindings.BindingsHelper.equals({{v1}}, {{v2}}) | |
14 {%- endif -%} | |
15 {%- else -%} | |
16 {{v1}} != {{v2}} | |
17 {%- endif -%} | |
18 {%- endmacro -%} | |
19 | |
20 {%- macro hash(kind, v) -%} | |
21 {%- if kind|is_array_kind -%} | |
22 {%- if kind.kind|is_reference_kind -%} | |
23 java.util.Arrays.deepHashCode({{v}}) | |
24 {%- else -%} | |
25 java.util.Arrays.hashCode({{v}}) | |
26 {%- endif -%} | |
27 {%- else -%} | |
28 org.chromium.mojo.bindings.BindingsHelper.hashCode({{v}}) | |
29 {%- endif -%} | |
30 {%- endmacro -%} | |
31 | |
32 {% macro encode(variable, kind, offset, bit, level=0, check_for_null=True) %} | |
33 {% if kind|is_pointer_array_kind %} | |
34 {% set sub_kind = kind.kind %} | |
35 {% if check_for_null %} | |
36 if ({{variable}} == null) { | |
37 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|java_
true_false}}); | |
38 } else { | |
39 {% else %} | |
40 { | |
41 {% endif %} | |
42 org.chromium.mojo.bindings.Encoder encoder{{level + 1}} = encoder{{level}}.e
ncodePointerArray({{variable}}.length, {{offset}}, {{kind|array_expected_length}
}); | |
43 for (int i{{level}} = 0; i{{level}} < {{variable}}.length; ++i{{level}}) { | |
44 {{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)}} | |
45 } | |
46 } | |
47 {% elif kind|is_map_kind %} | |
48 if ({{variable}} == null) { | |
49 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|java_
true_false}}); | |
50 } else { | |
51 org.chromium.mojo.bindings.Encoder encoder{{level + 1}} = encoder{{level}}.e
ncoderForMap({{offset}}); | |
52 int size{{level}} = {{variable}}.size(); | |
53 {{kind.key_kind|java_type}}[] keys{{level}} = {{kind.key_kind|array|new_arra
y('size'~level)}}; | |
54 {{kind.value_kind|java_type}}[] values{{level}} = {{kind.value_kind|array|ne
w_array('size'~level)}}; | |
55 int index{{level}} = 0; | |
56 for (java.util.Map.Entry<{{kind.key_kind|java_type(true)}}, {{kind.value_kin
d|java_type(true)}}> entry{{level}} : {{variable}}.entrySet()) { | |
57 keys{{level}}[index{{level}}] = entry{{level}}.getKey(); | |
58 values{{level}}[index{{level}}] = entry{{level}}.getValue(); | |
59 ++index{{level}}; | |
60 } | |
61 {{encode('keys'~level, kind.key_kind|array, 'DataHeader.HEADER_SIZE', 0, lev
el+1, False)|indent(4)}} | |
62 {{encode('values'~level, kind.value_kind|array, 'DataHeader.HEADER_SIZE + or
g.chromium.mojo.bindings.BindingsHelper.POINTER_SIZE', 0, level+1, False)|indent
(4)}} | |
63 } | |
64 {% else %} | |
65 encoder{{level}}.{{kind|encode_method(variable, offset, bit)}}; | |
66 {% endif %} | |
67 {% endmacro %} | |
68 | |
69 {% macro decode(variable, kind, offset, bit, level=0) %} | |
70 {% if kind|is_struct_kind or kind|is_pointer_array_kind or kind|is_map_kind %} | |
71 org.chromium.mojo.bindings.Decoder decoder{{level+1}} = decoder{{level}}.readPoi
nter({{offset}}, {{kind|is_nullable_kind|java_true_false}}); | |
72 {% if kind|is_struct_kind %} | |
73 {{variable}} = {{kind|java_type}}.decode(decoder{{level+1}}); | |
74 {% else %}{# kind|is_pointer_array_kind or is_map_kind #} | |
75 {% if kind|is_nullable_kind %} | |
76 if (decoder{{level+1}} == null) { | |
77 {{variable}} = null; | |
78 } else { | |
79 {% else %} | |
80 { | |
81 {% endif %} | |
82 {% if kind|is_map_kind %} | |
83 decoder{{level+1}}.readDataHeaderForMap(); | |
84 {{kind.key_kind|java_type}}[] keys{{level}}; | |
85 {{kind.value_kind|java_type}}[] values{{level}}; | |
86 { | |
87 {{decode('keys'~level, kind.key_kind|array, 'DataHeader.HEADER_SIZE', 0,
level+1)|indent(8)}} | |
88 } | |
89 { | |
90 {{decode('values'~level, kind.value_kind|array('keys'~level~'.length'),
'DataHeader.HEADER_SIZE + org.chromium.mojo.bindings.BindingsHelper.POINTER_SIZE
', 0, level+1)|indent(8)}} | |
91 } | |
92 {{variable}} = new java.util.HashMap<{{kind.key_kind|java_type(true)}}, {{ki
nd.value_kind|java_type(true)}}>(); | |
93 for (int index{{level}} = 0; index{{level}} < keys{{level}}.length; ++index{
{level}}) { | |
94 {{variable}}.put(keys{{level}}[index{{level}}], values{{level}}[index{{
level}}]); | |
95 } | |
96 {% else %} | |
97 DataHeader si{{level+1}} = decoder{{level+1}}.readDataHeaderForPointerArray(
{{kind|array_expected_length}}); | |
98 {{variable}} = {{kind|new_array('si'~(level+1)~'.numFields')}}; | |
99 for (int i{{level+1}} = 0; i{{level+1}} < si{{level+1}}.numFields; ++i{{leve
l+1}}) { | |
100 {{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)}} | |
101 } | |
102 {% endif %} | |
103 } | |
104 {% endif %} | |
105 {% else %} | |
106 {{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}}; | |
107 {% endif %} | |
108 {% endmacro %} | |
109 | |
110 {% macro struct_def(struct, inner_class=False) %} | |
111 {{'static' if inner_class else 'public'}} final class {{struct|name}} extends or
g.chromium.mojo.bindings.Struct { | |
112 | |
113 private static final int STRUCT_SIZE = {{struct.packed|struct_size}}; | |
114 private static final DataHeader DEFAULT_STRUCT_INFO = new DataHeader(STRUCT_
SIZE, {{struct.packed.packed_fields|length}}); | |
115 {% for constant in struct.constants %} | |
116 | |
117 {{constant_def(constant)|indent(4)}} | |
118 {% endfor %} | |
119 {% for enum in struct.enums %} | |
120 | |
121 {{enum_def(enum, false)|indent(4)}} | |
122 {% endfor %} | |
123 {% if struct.fields %} | |
124 | |
125 {% for field in struct.fields %} | |
126 public {{field.kind|java_type}} {{field|name}}; | |
127 {% endfor %} | |
128 {% endif %} | |
129 | |
130 public {{struct|name}}() { | |
131 super(STRUCT_SIZE); | |
132 {% for field in struct.fields %} | |
133 {% if field.default %} | |
134 {{field|name}} = {{field|default_value}}; | |
135 {% elif field.kind|is_handle %} | |
136 {{field|name}} = org.chromium.mojo.system.InvalidHandle.INSTANCE; | |
137 {% endif %} | |
138 {% endfor %} | |
139 } | |
140 | |
141 public static {{struct|name}} deserialize(org.chromium.mojo.bindings.Message
message) { | |
142 return decode(new org.chromium.mojo.bindings.Decoder(message)); | |
143 } | |
144 | |
145 @SuppressWarnings("unchecked") | |
146 public static {{struct|name}} decode(org.chromium.mojo.bindings.Decoder deco
der0) { | |
147 if (decoder0 == null) { | |
148 return null; | |
149 } | |
150 {{struct|name}} result = new {{struct|name}}(); | |
151 {% if not struct.bytes %} | |
152 decoder0.readDataHeader(); | |
153 {% else %} | |
154 DataHeader mainDataHeader = decoder0.readDataHeader(); | |
155 {% endif %} | |
156 {% for byte in struct.bytes %} | |
157 {% for packed_field in byte.packed_fields %} | |
158 if (mainDataHeader.numFields > {{packed_field.ordinal}}) { | |
159 {{decode('result.' ~ packed_field.field|name, packed_field.field.kin
d, 8+packed_field.offset, packed_field.bit)|indent(12)}} | |
160 } | |
161 {% endfor %} | |
162 {% endfor %} | |
163 return result; | |
164 } | |
165 | |
166 @SuppressWarnings("unchecked") | |
167 @Override | |
168 protected final void encode(org.chromium.mojo.bindings.Encoder encoder) { | |
169 {% if not struct.bytes %} | |
170 encoder.getEncoderAtDataOffset(DEFAULT_STRUCT_INFO); | |
171 {% else %} | |
172 org.chromium.mojo.bindings.Encoder encoder0 = encoder.getEncoderAtDataOf
fset(DEFAULT_STRUCT_INFO); | |
173 {% endif %} | |
174 {% for byte in struct.bytes %} | |
175 {% for packed_field in byte.packed_fields %} | |
176 {{encode(packed_field.field|name, packed_field.field.kind, 8+packed_fiel
d.offset, packed_field.bit)|indent(8)}} | |
177 {% endfor %} | |
178 {% endfor %} | |
179 } | |
180 | |
181 /** | |
182 * @see Object#equals(Object) | |
183 */ | |
184 @Override | |
185 public boolean equals(Object object) { | |
186 if (object == this) | |
187 return true; | |
188 if (object == null) | |
189 return false; | |
190 if (getClass() != object.getClass()) | |
191 return false; | |
192 {% if struct.fields|length %} | |
193 {{struct|name}} other = ({{struct|name}}) object; | |
194 {% for field in struct.fields %} | |
195 if ({{inequality(field.kind, field|name, 'other.'~field|name)}}) | |
196 return false; | |
197 {% endfor %} | |
198 {% endif %} | |
199 return true; | |
200 } | |
201 | |
202 /** | |
203 * @see Object#hashCode() | |
204 */ | |
205 @Override | |
206 public int hashCode() { | |
207 final int prime = 31; | |
208 int result = prime + getClass().hashCode(); | |
209 {% for field in struct.fields %} | |
210 result = prime * result + {{hash(field.kind, field|name)}}; | |
211 {% endfor %} | |
212 return result; | |
213 } | |
214 } | |
215 {% endmacro %} | |
OLD | NEW |