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

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
1 {%- macro setters(struct) %}
2 {% for packed_field in struct.packed.packed_fields %}
3 {%- set type = packed_field.field.kind|cpp_type %}
4 {%- set name = packed_field.field.name %}
5 {%- if packed_field.field.kind|is_object_kind %}
6 void set_{{name}}({{type}} {{name}}) { {{name}}_.ptr = {{name}}; }
7 {%- else %}
8 void set_{{name}}({{type}} {{name}}) { {{name}}_ = {{name}}; }
9 {%- endif %}
10 {%- endfor %}
11 {%- endmacro %}
12
13 {%- macro getters(struct) %}
14 {% for packed_field in struct.packed.packed_fields %}
15 {%- set type = packed_field.field.kind|cpp_type %}
16 {%- set name = packed_field.field.name %}
17 {%- if packed_field.field.kind|is_object_kind %}
18 const {{type}} {{name}}() const { return {{name}}_.ptr; }
19 {%- elif packed_field.field.kind|is_handle_kind %}
20 {{type}}* {{name}}() const { return &{{name}}_; }
21 {%- else %}
22 {{type}} {{name}}() const { return {{name}}_; }
23 {%- endif %}
24 {%- endfor %}
25 {%- endmacro %}
26
27 {%- macro field_line(field) %} 1 {%- macro field_line(field) %}
28 {%- set type = field.kind|cpp_field_type %} 2 {%- set type = field.kind|cpp_field_type %}
29 {%- set name = field.name -%} 3 {%- set name = field.name -%}
30 {%- if field.kind.spec == 'b' -%} 4 {%- if field.kind.spec == 'b' -%}
31 uint8_t {{name}}_ : 1; 5 uint8_t {{name}} : 1;
32 {%- elif field.kind|is_handle_kind -%}
33 mutable {{type}} {{name}}_;
34 {%- elif field.kind|is_enum_kind -%} 6 {%- elif field.kind|is_enum_kind -%}
35 int32_t {{name}}_; 7 int32_t {{name}};
36 {%- else -%} 8 {%- else -%}
37 {{type}} {{name}}_; 9 {{type}} {{name}};
38 {%- endif %} 10 {%- endif %}
39 {%- endmacro %} 11 {%- endmacro %}
40 12
41 {%- macro fields(struct) %} 13 {%- macro fields(struct) %}
42 {%- for packed_field in struct.packed.packed_fields %} 14 {%- for packed_field in struct.packed.packed_fields %}
43 {{field_line(packed_field.field)}} 15 {{field_line(packed_field.field)}}
44 {%- if not loop.last %} 16 {%- if not loop.last %}
45 {%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %} 17 {%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %}
46 {%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) % } 18 {%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) % }
47 {%- if pad > 0 %} 19 {%- if pad > 0 %}
48 uint8_t _pad{{loop.index0}}_[{{pad}}]; 20 uint8_t pad{{loop.index0}}_[{{pad}}];
49 {%- endif %} 21 {%- endif %}
50 {%- endif %} 22 {%- endif %}
51 {%- endfor -%} 23 {%- endfor -%}
52 24
53 {%- set num_fields = struct.packed.packed_fields|length %} 25 {%- set num_fields = struct.packed.packed_fields|length %}
54 {%- if num_fields > 0 %} 26 {%- if num_fields > 0 %}
55 {%- set last_field = struct.packed.packed_fields[num_fields - 1] %} 27 {%- set last_field = struct.packed.packed_fields[num_fields - 1] %}
56 {%- set offset = last_field.offset + last_field.size %} 28 {%- set offset = last_field.offset + last_field.size %}
57 {%- set pad = offset|get_pad(8) -%} 29 {%- set pad = offset|get_pad(8) -%}
58 {%- if pad > 0 %} 30 {%- if pad > 0 %}
59 uint8_t _padfinal_[{{pad}}]; 31 uint8_t padfinal_[{{pad}}];
60 {%- endif %} 32 {%- endif %}
61 {%- endif %} 33 {%- endif %}
62 {%- endmacro %} 34 {%- endmacro %}
63 35
64 {%- macro encodes(struct) -%} 36 {%- macro encodes(struct) -%}
65 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind -%} 37 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind -%}
66 mojo::internal::Encode(&{{pf.field.name}}_, handles); 38 mojo::internal::Encode(&{{pf.field.name}}, handles);
67 {% endfor %} 39 {% endfor %}
68 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_handle_kind -%} 40 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_handle_kind -%}
69 mojo::internal::EncodeHandle(&{{pf.field.name}}_, handles); 41 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
70 {% endfor %} 42 {% endfor %}
71 {%- endmacro -%} 43 {%- endmacro -%}
72 44
73 {%- macro decodes(struct) -%} 45 {%- macro decodes(struct) -%}
74 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind -%} 46 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_object_kind -%}
75 if (!mojo::internal::Decode(&{{pf.field.name}}_, message)) 47 if (!mojo::internal::Decode(&{{pf.field.name}}, message))
76 return false; 48 return false;
77 {% endfor %} 49 {% endfor %}
78 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_handle_kind -%} 50 {%- for pf in struct.packed.packed_fields if pf.field.kind|is_handle_kind -%}
79 if (!mojo::internal::DecodeHandle(&{{pf.field.name}}_, 51 if (!mojo::internal::DecodeHandle(&{{pf.field.name}},
80 message->mutable_handles())) 52 message->mutable_handles()))
81 return false; 53 return false;
82 {% endfor %} 54 {% endfor %}
83 {%- endmacro -%} 55 {%- endmacro -%}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698