1{%- set class_name = struct.name ~ "_Data" -%} 2 3class {{export_attribute}} {{class_name}} { 4 public: 5 class BufferWriter { 6 public: 7 BufferWriter() = default; 8 9 void Allocate(mojo::internal::Buffer* serialization_buffer) { 10 serialization_buffer_ = serialization_buffer; 11 index_ = serialization_buffer_->Allocate(sizeof({{class_name}})); 12 new (data()) {{class_name}}(); 13 } 14 15 bool is_null() const { return !serialization_buffer_; } 16 {{class_name}}* data() { 17 DCHECK(!is_null()); 18 return serialization_buffer_->Get<{{class_name}}>(index_); 19 } 20 {{class_name}}* operator->() { return data(); } 21 22 private: 23 mojo::internal::Buffer* serialization_buffer_ = nullptr; 24 size_t index_ = 0; 25 26 DISALLOW_COPY_AND_ASSIGN(BufferWriter); 27 }; 28 29 static bool Validate(const void* data, 30 mojo::internal::ValidationContext* validation_context); 31 32 mojo::internal::StructHeader header_; 33{%- for packed_field in struct.packed.packed_fields %} 34{%- set name = packed_field.field.name %} 35{%- set kind = packed_field.field.kind %} 36{%- if kind.spec == 'b' %} 37 uint8_t {{name}} : 1; 38{%- else %} 39 {{kind|cpp_field_type}} {{name}}; 40{%- endif %} 41{%- if not loop.last %} 42{%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %} 43{%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) %} 44{%- if pad > 0 %} 45 uint8_t pad{{loop.index0}}_[{{pad}}]; 46{%- endif %} 47{%- endif %} 48{%- endfor %} 49 50{%- set num_fields = struct.versions[-1].num_fields %} 51{%- if num_fields > 0 %} 52{%- set last_field = struct.packed.packed_fields[num_fields - 1] %} 53{%- set offset = last_field.offset + last_field.size %} 54{%- set pad = offset|get_pad(8) %} 55{%- if pad > 0 %} 56 uint8_t padfinal_[{{pad}}]; 57{%- endif %} 58{%- endif %} 59 60 private: 61 {{class_name}}(); 62 ~{{class_name}}() = delete; 63}; 64static_assert(sizeof({{class_name}}) == {{struct.versions[-1].num_bytes}}, 65 "Bad sizeof({{class_name}})"); 66