| OLD | NEW |
| 1 {%- import "validation_macros.tmpl" as validation_macros %} | 1 {%- import "validation_macros.tmpl" as validation_macros %} |
| 2 {%- set class_name = struct.name ~ "_Data" %} | 2 {%- set class_name = struct.name ~ "_Data" %} |
| 3 | 3 |
| 4 // static | 4 // static |
| 5 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { | 5 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { |
| 6 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); | 6 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); |
| 7 } | 7 } |
| 8 | 8 |
| 9 // static | 9 // static |
| 10 bool {{class_name}}::Validate( | 10 bool {{class_name}}::Validate( |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 return true; | 66 return true; |
| 67 {%- endif %} | 67 {%- endif %} |
| 68 {%- set field_expr = "object->" ~ packed_field.field.name %} | 68 {%- set field_expr = "object->" ~ packed_field.field.name %} |
| 69 {{validation_macros.validate_field(packed_field.field, field_expr, struct.name,
true)}} | 69 {{validation_macros.validate_field(packed_field.field, field_expr, struct.name,
true)}} |
| 70 {%- endif %} | 70 {%- endif %} |
| 71 {%- endfor %} | 71 {%- endfor %} |
| 72 | 72 |
| 73 return true; | 73 return true; |
| 74 } | 74 } |
| 75 | 75 |
| 76 void {{class_name}}::EncodePointers() { | |
| 77 CHECK(header_.version == {{struct.versions[-1].version}}); | |
| 78 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | |
| 79 {%- if pf.field.kind|is_union_kind %} | |
| 80 this->{{pf.field.name}}.EncodePointers(); | |
| 81 {%- elif pf.field.kind|is_object_kind %} | |
| 82 mojo::internal::Encode(&this->{{pf.field.name}}); | |
| 83 {%- endif %} | |
| 84 {%- endfor %} | |
| 85 } | |
| 86 | |
| 87 void {{class_name}}::DecodePointers() { | |
| 88 // NOTE: The memory backing |this| may be smaller than |sizeof(*this)|, if the | |
| 89 // message comes from an older version. | |
| 90 {#- Before decoding fields introduced at a certain version, we need to add | |
| 91 a version check, which makes sure we skip further decoding if |this| | |
| 92 is from an earlier version. |last_checked_version| records the last | |
| 93 version that we have added such version check. #} | |
| 94 {%- set last_checked_version = 0 %} | |
| 95 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | |
| 96 {%- set name = pf.field.name %} | |
| 97 {%- set kind = pf.field.kind %} | |
| 98 {%- if kind|is_object_kind %} | |
| 99 {%- if pf.min_version > last_checked_version %} | |
| 100 {%- set last_checked_version = pf.min_version %} | |
| 101 if (header_.version < {{pf.min_version}}) | |
| 102 return; | |
| 103 {%- endif %} | |
| 104 {%- if kind|is_union_kind %} | |
| 105 this->{{name}}.DecodePointers(); | |
| 106 {%- elif kind|is_object_kind %} | |
| 107 mojo::internal::Decode(&this->{{name}}); | |
| 108 {%- endif %} | |
| 109 {%- endif %} | |
| 110 {%- endfor %} | |
| 111 } | |
| 112 | |
| 113 {{class_name}}::{{class_name}}() { | 76 {{class_name}}::{{class_name}}() { |
| 114 header_.num_bytes = sizeof(*this); | 77 header_.num_bytes = sizeof(*this); |
| 115 header_.version = {{struct.versions[-1].version}}; | 78 header_.version = {{struct.versions[-1].version}}; |
| 116 } | 79 } |
| OLD | NEW |