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

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

Issue 1043903004: Dart bindings: support struct versioning. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 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
« no previous file with comments | « mojo/dart/testing/validation_test_input_parser.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 {#--- Begin #} 1 {#--- Begin #}
2 2
3 {%- macro encode(variable, kind, offset, bit, level=0, check_for_null=True) %} 3 {%- macro encode(variable, kind, offset, bit, level=0, check_for_null=True) %}
4 {%- if kind|is_pointer_array_kind %} 4 {%- if kind|is_pointer_array_kind %}
5 {%- set sub_kind = kind.kind %} 5 {%- set sub_kind = kind.kind %}
6 {%- if check_for_null %} 6 {%- if check_for_null %}
7 if ({{variable}} == null) { 7 if ({{variable}} == null) {
8 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|dart_tr ue_false}}); 8 encoder{{level}}.encodeNullPointer({{offset}}, {{kind|is_nullable_kind|dart_tr ue_false}});
9 } else { 9 } else {
10 {%- else %} 10 {%- else %}
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 } 67 }
68 {%- endif %} 68 {%- endif %}
69 {%- else %} 69 {%- else %}
70 {{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}}; 70 {{variable}} = decoder{{level}}.{{kind|decode_method(offset, bit)}};
71 {%- endif %} 71 {%- endif %}
72 {%- endmacro %} 72 {%- endmacro %}
73 73
74 74
75 {%- macro struct_def(struct) %} 75 {%- macro struct_def(struct) %}
76 class {{struct|name}} extends bindings.Struct { 76 class {{struct|name}} extends bindings.Struct {
77 static const int kStructSize = {{struct.versions[-1].num_bytes}}; 77 static const List<bindings.StructDataHeader> kVersions = const [
78 static const bindings.StructDataHeader kDefaultStructInfo = 78 {%- for version in struct.versions %}
79 const bindings.StructDataHeader(kStructSize, {{struct.versions[-1].version }}); 79 const bindings.StructDataHeader({{version.num_bytes}}, {{version.version}}){ % if not loop.last %},{% endif %}
80 {%- endfor %}
81 ];
80 82
81 {#--- Enums #} 83 {#--- Enums #}
82 {%- from "enum_definition.tmpl" import enum_def %} 84 {%- from "enum_definition.tmpl" import enum_def %}
83 {%- for enum in struct.enums %} 85 {%- for enum in struct.enums %}
84 {{enum_def(" static ", enum)}} 86 {{enum_def(" static ", enum)}}
85 {%- endfor %} 87 {%- endfor %}
86 88
87 89
88 {#--- Constants #} 90 {#--- Constants #}
89 {%- for constant in struct.constants %} 91 {%- for constant in struct.constants %}
90 static final {{constant.name}} = {{constant.value|expression_to_text}}; 92 static final {{constant.name}} = {{constant.value|expression_to_text}};
91 {%- endfor %} 93 {%- endfor %}
92 94
93 {#--- initDefaults() #} 95 {#--- initDefaults() #}
94 {%- for packed_field in struct.packed.packed_fields %} 96 {%- for packed_field in struct.packed.packed_fields %}
95 {{packed_field.field.kind|dart_type}} {{packed_field.field|name}} = {{packed_f ield.field|default_value}}; 97 {{packed_field.field.kind|dart_type}} {{packed_field.field|name}} = {{packed_f ield.field|default_value}};
96 {%- endfor %} 98 {%- endfor %}
97 99
98 {{struct|name}}() : super(kStructSize); 100 {{struct|name}}() : super(kVersions.last.size);
99 101
100 static {{struct|name}} deserialize(bindings.Message message) { 102 static {{struct|name}} deserialize(bindings.Message message) {
101 return decode(new bindings.Decoder(message)); 103 return decode(new bindings.Decoder(message));
102 } 104 }
103 105
104 static {{struct|name}} decode(bindings.Decoder decoder0) { 106 static {{struct|name}} decode(bindings.Decoder decoder0) {
105 if (decoder0 == null) { 107 if (decoder0 == null) {
106 return null; 108 return null;
107 } 109 }
108 {{struct|name}} result = new {{struct|name}}(); 110 {{struct|name}} result = new {{struct|name}}();
109 111
110 var mainDataHeader = decoder0.decodeStructDataHeader(); 112 var mainDataHeader = decoder0.decodeStructDataHeader();
111 if ((mainDataHeader.size < kStructSize) || 113 if (mainDataHeader.version <= kVersions.last.version) {
zra 2015/03/31 14:46:41 I may not understand what the intended semantics a
yzshen1 2015/03/31 15:26:26 Encountering a version number bigger than the late
112 (mainDataHeader.version < {{struct.versions[-1].version}})) { 114 // Scan in reverse order to optimize for more recent versions.
113 throw new bindings.MojoCodecError('Malformed header'); 115 for (int i = kVersions.length - 1; i >= 0; --i) {
116 if (mainDataHeader.version >= kVersions[i].version) {
117 if (mainDataHeader.size != kVersions[i].size)
118 throw new bindings.MojoCodecError(
119 'Header doesn\'t correspond to any known version.');
120 }
121 }
122 } else if (mainDataHeader.size < kVersions.last.size) {
123 throw new bindings.MojoCodecError(
124 'Message newer than the last known version cannot be shorter than '
125 'required by the last known version.');
114 } 126 }
115 127
116 {%- for byte in struct.bytes %} 128 {%- for byte in struct.bytes %}
117 {%- for packed_field in byte.packed_fields %} 129 {%- for packed_field in byte.packed_fields %}
118 { 130 if (mainDataHeader.version >= {{packed_field.min_version}}) {
119 {{decode('result.' ~ packed_field.field|name, packed_field.field.kind, 8+p acked_field.offset, packed_field.bit)|indent(6)}} 131 {{decode('result.' ~ packed_field.field|name, packed_field.field.kind, 8+p acked_field.offset, packed_field.bit)|indent(6)}}
120 } 132 }
121 {%- endfor %} 133 {%- endfor %}
122 {%- endfor %} 134 {%- endfor %}
123 return result; 135 return result;
124 } 136 }
125 137
126 void encode(bindings.Encoder encoder) { 138 void encode(bindings.Encoder encoder) {
127 {%- if not struct.bytes %} 139 {%- if not struct.bytes %}
128 encoder.getStructEncoderAtOffset(kDefaultStructInfo); 140 encoder.getStructEncoderAtOffset(kVersions.last);
129 {%- else %} 141 {%- else %}
130 var encoder0 = encoder.getStructEncoderAtOffset(kDefaultStructInfo); 142 var encoder0 = encoder.getStructEncoderAtOffset(kVersions.last);
131 {%- endif %} 143 {%- endif %}
132 {%- for byte in struct.bytes %} 144 {%- for byte in struct.bytes %}
133 {%- for packed_field in byte.packed_fields %} 145 {%- for packed_field in byte.packed_fields %}
134 {{encode(packed_field.field|name, packed_field.field.kind, 8+packed_field.of fset, packed_field.bit)|indent(4)}} 146 {{encode(packed_field.field|name, packed_field.field.kind, 8+packed_field.of fset, packed_field.bit)|indent(4)}}
135 {%- endfor %} 147 {%- endfor %}
136 {%- endfor %} 148 {%- endfor %}
137 } 149 }
138 150
139 String toString() { 151 String toString() {
140 return "{{struct|name}}(" 152 return "{{struct|name}}("
141 {%- for packed_field in struct.packed.packed_fields %} 153 {%- for packed_field in struct.packed.packed_fields %}
142 "{{packed_field.field|name}}: ${{packed_field.field|name}}" {% if not loop.last %}", "{% endif %} 154 "{{packed_field.field|name}}: ${{packed_field.field|name}}" {% if not loop.last %}", "{% endif %}
143 {%- endfor %}")"; 155 {%- endfor %}")";
144 } 156 }
145 } 157 }
146 {%- endmacro %} 158 {%- endmacro %}
OLDNEW
« no previous file with comments | « mojo/dart/testing/validation_test_input_parser.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698