OLD | NEW |
| 1 {%- import "struct_macros.tmpl" as struct_macros %} |
1 {%- set class_name = struct.name ~ "_Data" %} | 2 {%- set class_name = struct.name ~ "_Data" %} |
2 // static | 3 // static |
3 {{class_name}}* {{class_name}}::New(mojo::Buffer* buf, mojo::Buffer::Destructor
dtor) { | 4 {{class_name}}* {{class_name}}::New(mojo::Buffer* buf, mojo::Buffer::Destructor
dtor) { |
4 return new (buf->Allocate(sizeof({{class_name}}), dtor)) {{class_name}}(); | 5 return new (buf->Allocate(sizeof({{class_name}}), dtor)) {{class_name}}(); |
5 } | 6 } |
6 | 7 |
7 {{class_name}}::{{class_name}}() { | 8 {{class_name}}::{{class_name}}() { |
8 _header_.num_bytes = sizeof(*this); | 9 _header_.num_bytes = sizeof(*this); |
9 _header_.num_fields = {{struct.packed.packed_fields|length}}; | 10 _header_.num_fields = {{struct.packed.packed_fields|length}}; |
10 } | 11 } |
| 12 |
| 13 size_t {{class_name}}::ComputeSize() const { |
| 14 size_t result = sizeof(*this); |
| 15 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind %} |
| 16 if ({{pf.field.name}}_.ptr) |
| 17 result += {{pf.field.name}}_.ptr->ComputeSize(); |
| 18 {%- endfor %} |
| 19 return result; |
| 20 } |
| 21 |
| 22 {{class_name}}* {{class_name}}::Clone(mojo::Buffer* buf) const { |
| 23 {{class_name}}* clone = New(buf); |
| 24 memcpy(clone, this, sizeof(*this)); |
| 25 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind %} |
| 26 if ({{pf.field.name}}_.ptr) |
| 27 clone->set_{{pf.field.name}}({{pf.field.name}}_.ptr->Clone(buf)); |
| 28 {%- endfor %} |
| 29 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_handle_kind %} |
| 30 mojo::internal::ResetIfNonNull({{pf.field.name}}()); |
| 31 {%- endfor %} |
| 32 return clone; |
| 33 } |
| 34 |
| 35 void {{class_name}}::CloseHandles() { |
| 36 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind %} |
| 37 if ({{pf.field.name}}_.ptr) |
| 38 {{pf.field.name}}_.ptr->CloseHandles(); |
| 39 {%- endfor %} |
| 40 {%- if struct|is_struct_with_handles %} |
| 41 {{struct.name}}_Data_Destructor(this); |
| 42 {%- endif %} |
| 43 } |
| 44 |
| 45 void {{class_name}}::EncodePointersAndHandles( |
| 46 std::vector<mojo::Handle>* handles) { |
| 47 {{ struct_macros.encodes(struct)|indent(2) }} |
| 48 } |
| 49 |
| 50 bool {{class_name}}::DecodePointersAndHandles(mojo::Message* message) { |
| 51 {{ struct_macros.decodes(struct)|indent(2) }} |
| 52 return true; |
| 53 } |
OLD | NEW |