12e2dbba45b5128fd0bdaefd1eda4088fe1b640b
[platform/framework/web/crosswalk.git] / src / mojo / public / tools / bindings / generators / cpp_templates / struct_macros.tmpl
1 {%- macro validate(struct, class_name) %}
2   if (!data)
3     return true;
4
5   if (!ValidateStructHeader(
6           data, sizeof({{class_name}}),
7           {{struct.packed.packed_fields|length}}, bounds_checker)) {
8     return false;
9   }
10
11   const {{class_name}}* MOJO_ALLOW_UNUSED object =
12       static_cast<const {{class_name}}*>(data);
13
14 {%-   for packed_field in struct.packed.packed_fields %}
15 {%-     set name = packed_field.field.name %}
16 {%-     set kind = packed_field.field.kind %}
17 {%-     if kind|is_object_kind %}
18 {%-       set wrapper_type = kind|cpp_wrapper_type %}
19 {%-       if not kind|is_nullable_kind %}
20   if (mojo::internal::IsNonNullableValidationEnabled() &&
21       !object->{{name}}.offset) {
22     ReportValidationError(
23         mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
24     return false;
25   }
26 {%-       endif %}
27   if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
28     ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
29     return false;
30   }
31 {%-       if kind|is_any_array_kind or kind|is_string_kind %}
32   if (!{{wrapper_type}}::Data_::Validate<
33           {{kind|get_array_validate_params|indent(10)}}>(
34               mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
35               bounds_checker)) {
36 {%-       else %}
37   if (!{{wrapper_type}}::Data_::Validate(
38           mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
39           bounds_checker)) {
40 {%-       endif %}
41     return false;
42   }
43 {%-     elif kind|is_any_handle_kind %}
44 {%-       if not kind|is_nullable_kind %}
45   if (mojo::internal::IsNonNullableValidationEnabled() &&
46       object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) {
47     ReportValidationError(
48         mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE);
49     return false;
50   }
51 {%-       endif %}
52   if (!bounds_checker->ClaimHandle(object->{{name}})) {
53     ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE);
54     return false;
55   }
56 {%-     endif %}
57 {%-   endfor %}
58
59   return true;
60 {%- endmacro %}
61
62 {%- macro field_line(field) %}
63 {%-   set type = field.kind|cpp_field_type %}
64 {%-   set name = field.name -%}
65 {%-   if field.kind.spec == 'b' -%}
66   uint8_t {{name}} : 1;
67 {%-   elif field.kind|is_enum_kind -%}
68   int32_t {{name}};
69 {%-   else -%}
70   {{type}} {{name}};
71 {%-   endif %}
72 {%- endmacro %}
73
74 {%- macro fields(struct) %}
75 {%-   for packed_field in struct.packed.packed_fields %}
76   {{field_line(packed_field.field)}}
77 {%-     if not loop.last %}
78 {%-       set next_pf = struct.packed.packed_fields[loop.index0 + 1] %}
79 {%-       set pad = next_pf.offset - (packed_field.offset + packed_field.size) %}
80 {%-       if pad > 0 %}
81   uint8_t pad{{loop.index0}}_[{{pad}}];
82 {%-       endif %}
83 {%-     endif %}
84 {%-   endfor -%}
85
86 {%-   set num_fields = struct.packed.packed_fields|length %}
87 {%-   if num_fields > 0 %}
88 {%-     set last_field = struct.packed.packed_fields[num_fields - 1] %}
89 {%-     set offset = last_field.offset + last_field.size %}
90 {%-     set pad = offset|get_pad(8) -%}
91 {%-     if pad > 0 %}
92   uint8_t padfinal_[{{pad}}];
93 {%-     endif %}
94 {%-   endif %}
95 {%- endmacro %}
96
97 {%- macro encodes(struct) -%}
98 {%-   for pf in struct.packed.packed_fields %}
99 {%-     if pf.field.kind|is_object_kind %}
100 mojo::internal::Encode(&{{pf.field.name}}, handles);
101 {%-     elif pf.field.kind|is_any_handle_kind %}
102 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
103 {%-     endif %}
104 {%-   endfor %}
105 {%- endmacro -%}
106
107 {%- macro decodes(struct) -%}
108 {%-   for pf in struct.packed.packed_fields %}
109 {%-     if pf.field.kind|is_object_kind %}
110 mojo::internal::Decode(&{{pf.field.name}}, handles);
111 {%-     elif pf.field.kind|is_any_handle_kind %}
112 mojo::internal::DecodeHandle(&{{pf.field.name}}, handles);
113 {%-     endif %}
114 {%-   endfor %}
115 {%- endmacro -%}