OLD | NEW |
| (Empty) |
1 {#--- Begin #} | |
2 function {{struct.name}}(values) { | |
3 this.initDefaults_(); | |
4 this.initFields_(values); | |
5 } | |
6 | |
7 {#--- Enums #} | |
8 {%- from "enum_definition.tmpl" import enum_def %} | |
9 {% for enum in struct.enums %} | |
10 {{enum_def("%s.%s"|format(struct.name, enum.name), enum)}} | |
11 {%- endfor %} | |
12 | |
13 {#--- Constants #} | |
14 {% for constant in struct.constants %} | |
15 {{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}}; | |
16 {%- endfor %} | |
17 | |
18 {#--- initDefaults() #} | |
19 {{struct.name}}.prototype.initDefaults_ = function() { | |
20 {%- for packed_field in struct.packed.packed_fields %} | |
21 this.{{packed_field.field.name}} = {{packed_field.field|default_value}}; | |
22 {%- endfor %} | |
23 }; | |
24 | |
25 {#--- initFields() #} | |
26 {{struct.name}}.prototype.initFields_ = function(fields) { | |
27 for(var field in fields) { | |
28 if (this.hasOwnProperty(field)) | |
29 this[field] = fields[field]; | |
30 } | |
31 }; | |
32 | |
33 {#--- Validation #} | |
34 | |
35 {{struct.name}}.validate = function(messageValidator, offset) { | |
36 var err; | |
37 {% macro check_err() -%} | |
38 if (err !== validator.validationError.NONE) | |
39 return err; | |
40 {%- endmacro %} | |
41 err = messageValidator.validateStructHeader(offset, {{struct.name}}.encodedS
ize, {{struct.packed.packed_fields|length}}); | |
42 {{check_err()}} | |
43 | |
44 {%- for packed_field in struct.packed.packed_fields %} | |
45 {%- set field_name = packed_field.field.name %} | |
46 {%- if packed_field.field|is_string_pointer_field %} | |
47 // validate {{struct.name}}.{{field_name}} | |
48 err = messageValidator.validateStringPointer({{packed_field|validate_string_
params}}) | |
49 {{check_err()}} | |
50 {%- elif packed_field.field|is_array_pointer_field %} | |
51 // validate {{struct.name}}.{{field_name}} | |
52 err = messageValidator.validateArrayPointer({{packed_field|validate_array_pa
rams}}); | |
53 {{check_err()}} | |
54 {%- elif packed_field.field|is_struct_pointer_field %} | |
55 // validate {{struct.name}}.{{field_name}} | |
56 err = messageValidator.validateStructPointer({{packed_field|validate_struct_
params}}); | |
57 {{check_err()}} | |
58 {%- elif packed_field.field|is_map_pointer_field %} | |
59 // validate {{struct.name}}.{{field_name}} | |
60 err = messageValidator.validateMapPointer({{packed_field|validate_map_params
}}); | |
61 {{check_err()}} | |
62 {%- elif packed_field.field|is_handle_field %} | |
63 // validate {{struct.name}}.{{field_name}} | |
64 err = messageValidator.validateHandle({{packed_field|validate_handle_params}
}) | |
65 {{check_err()}} | |
66 {%- endif %} | |
67 {%- endfor %} | |
68 | |
69 return validator.validationError.NONE; | |
70 }; | |
71 | |
72 {#--- Encoding and decoding #} | |
73 | |
74 {{struct.name}}.encodedSize = codec.kStructHeaderSize + {{struct.packed|payloa
d_size}}; | |
75 | |
76 {{struct.name}}.decode = function(decoder) { | |
77 var packed; | |
78 var val = new {{struct.name}}(); | |
79 var numberOfBytes = decoder.readUint32(); | |
80 var numberOfFields = decoder.readUint32(); | |
81 {%- for byte in struct.bytes %} | |
82 {%- if byte.packed_fields|length > 1 %} | |
83 packed = decoder.readUint8(); | |
84 {%- for packed_field in byte.packed_fields %} | |
85 val.{{packed_field.field.name}} = (packed >> {{packed_field.bit}}) & 1 ? tru
e : false; | |
86 {%- endfor %} | |
87 {%- else %} | |
88 {%- for packed_field in byte.packed_fields %} | |
89 val.{{packed_field.field.name}} = decoder.{{packed_field.field.kind|decode_s
nippet}}; | |
90 {%- endfor %} | |
91 {%- endif %} | |
92 {%- if byte.is_padding %} | |
93 decoder.skip(1); | |
94 {%- endif %} | |
95 {%- endfor %} | |
96 return val; | |
97 }; | |
98 | |
99 {{struct.name}}.encode = function(encoder, val) { | |
100 var packed; | |
101 encoder.writeUint32({{struct.name}}.encodedSize); | |
102 encoder.writeUint32({{struct.packed.packed_fields|length}}); | |
103 | |
104 {%- for byte in struct.bytes %} | |
105 {%- if byte.packed_fields|length > 1 %} | |
106 packed = 0; | |
107 {%- for packed_field in byte.packed_fields %} | |
108 packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}} | |
109 {%- endfor %} | |
110 encoder.writeUint8(packed); | |
111 {%- else %} | |
112 {%- for packed_field in byte.packed_fields %} | |
113 encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.n
ame}}); | |
114 {%- endfor %} | |
115 {%- endif %} | |
116 {%- if byte.is_padding %} | |
117 encoder.skip(1); | |
118 {%- endif %} | |
119 {%- endfor %} | |
120 }; | |
OLD | NEW |