summaryrefslogtreecommitdiff
path: root/chromium/mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl
blob: 2d32b59316999adb551f1c8fcd53432a44e92be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{#  TODO(yzshen): Make these templates more readable. #}

{#  Computes the serialized size for the specified struct.
    |struct| is the struct definition.
    |input_field_pattern| should be a pattern that contains one string
    placeholder, for example, "input->%s", "p_%s". The placeholder will be
    substituted with struct field names to refer to the input fields.
    |context| is the name of the serialization context.
    This macro is expanded to compute seriailized size for both:
    - user-defined structs: the input is an instance of the corresponding struct
      wrapper class.
    - method parameters/response parameters: the input is a list of
      arguments.
    It declares |size| of type size_t to store the resulting size. #}
{%- macro get_serialized_size(struct, input_field_pattern, context) -%}
  size_t size = sizeof(internal::{{struct.name}}_Data);
{%-   for pf in struct.packed.packed_fields_in_ordinal_order if pf.field.kind|is_object_kind %}
{%-     set name = pf.field.name -%}
{%-     set kind = pf.field.kind -%}
{%-     if kind|is_native_only_kind and kind|is_typemapped_kind %}
  size += mojo::internal::GetSerializedSizeNative_(
      {{input_field_pattern|format(name)}}, {{context}});
{%-     elif kind|is_typemapped_kind %}
  size +=
      {{kind|get_name_for_kind}}_SerializerTraits_<{{kind|cpp_wrapper_type}}>
          ::GetSize({{input_field_pattern|format(name)}});
{%-     elif kind|is_union_kind %}
  size += GetSerializedSize_({{input_field_pattern|format(name)}}, true, {{context}});
{%-     else %}
  size += GetSerializedSize_({{input_field_pattern|format(name)}}, {{context}});
{%-     endif %}
{%-   endfor %}
{%- endmacro -%}

{#  Serializes the specified struct.
    |struct| is the struct definition.
    |struct_display_name| is the display name for the struct that can be showed
    in error/log messages, for example, "FooStruct", "FooMethod request".
    |input_field_pattern| should be a pattern that contains one string
    placeholder, for example, "input->%s", "p_%s". The placeholder will be
    substituted with struct field names to refer to the input fields.
    |output| is the name of the output struct instance.
    |buffer| is the name of the Buffer instance used.
    |context| is the name of the serialization context.
    This macro is expanded to do serialization for both:
    - user-defined structs: the input is an instance of the corresponding struct
      wrapper class.
    - method parameters/response parameters: the input is a list of
      arguments. #}
{%- macro serialize(struct, struct_display_name, input_field_pattern, output,
                    buffer, context) -%}
  internal::{{struct.name}}_Data* {{output}} =
      internal::{{struct.name}}_Data::New({{buffer}});
{%- for pf in struct.packed.packed_fields_in_ordinal_order %}
{%-   set input_field = input_field_pattern|format(pf.field.name) %}
{%-   set name = pf.field.name %}
{%-   set kind = pf.field.kind %}
{%-   if kind|is_object_kind %}
{%-     if kind|is_array_kind or
            (kind|is_native_only_kind and not kind|is_typemapped_kind) %}
  const mojo::internal::ArrayValidateParams {{name}}_validate_params(
      {{kind|get_array_validate_params_ctor_args|indent(10)}});
  mojo::SerializeArray_(std::move({{input_field}}), {{buffer}},
      &{{output}}->{{name}}.ptr, &{{name}}_validate_params, {{context}});
{%-     elif kind|is_map_kind %}
  const mojo::internal::ArrayValidateParams {{name}}_validate_params(
      {{kind.value_kind|get_map_validate_params_ctor_args|indent(10)}});
  mojo::SerializeMap_(std::move({{input_field}}), {{buffer}},
      &{{output}}->{{name}}.ptr, &{{name}}_validate_params, {{context}});
{%-     elif kind|is_native_only_kind and kind|is_typemapped_kind %}
  mojo::internal::SerializeNative_(
      {{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr, {{context}});
{%-     elif kind|is_typemapped_kind %}
  {{kind|get_name_for_kind}}_SerializerTraits_<{{kind|cpp_wrapper_type}}>
      ::Serialize({{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr);
{%-     elif kind|is_union_kind %}
  internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}};
  SerializeUnion_(std::move({{input_field}}), {{buffer}}, &{{name}}_ptr, true,
                  {{context}});
{%-     else %}
  Serialize_(std::move({{input_field}}), {{buffer}}, &{{output}}->{{name}}.ptr,
             {{context}});
{%-     endif %}
{%-     if not kind|is_nullable_kind %}
  MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
{%-       if kind|is_union_kind %}
      {{output}}->{{name}}.is_null(),
{%-       else %}
      !{{output}}->{{name}}.ptr,
{%-       endif %}
      mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
      "null {{name}} in {{struct_display_name}}");
{%-     endif %}
{%-   elif kind|is_any_handle_kind or kind|is_interface_kind %}
{%-     if kind|is_interface_kind %}
  mojo::internal::InterfacePointerToData(std::move({{input_field}}), &{{output}}->{{name}});
{%-     elif kind|is_interface_request_kind %}
  {{output}}->{{name}} = {{input_field}}.PassMessagePipe().release();
{%-     else %}
  {{output}}->{{name}} = {{input_field}}.release();
{%-     endif %}
{%-     if not kind|is_nullable_kind %}
  MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
{%-       if kind|is_interface_kind %}
      !{{output}}->{{name}}.handle.is_valid(),
{%-       else %}
      !{{output}}->{{name}}.is_valid(),
{%-       endif %}
      mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
      "invalid {{name}} in {{struct_display_name}}");
{%-     endif %}
{%-   elif kind|is_associated_kind %}
  // TODO(yzshen): add some DCHECKs for non-nullable, is_local, correct router, etc.
{%-     if kind|is_associated_interface_kind %}
  mojo::internal::AssociatedInterfacePtrInfoToData(std::move({{input_field}}), &{{output}}->{{name}});
{%-     else %}
  {{output}}->{{name}} = mojo::internal::AssociatedInterfaceRequestHelper::PassHandle(&{{input_field}}).release();
{%-     endif %}
{%-   elif kind|is_enum_kind %}
  {{output}}->{{name}}.value = static_cast<int32_t>({{input_field}});
{%-   else %}
  {{output}}->{{name}} = {{input_field}};
{%-   endif %}
{%- endfor %}
{%- endmacro -%}

{#  Deserializes the specified struct.
    |struct| is the struct definition.
    |input| is the name of the input struct instance.
    |output_field_pattern| should be a pattern that contains one string
    placeholder, for example, "result->%s", "p_%s". The placeholder will be
    substituted with struct field names to refer to the output fields.
    |context| is the name of the serialization context.
    |success| is the name of a bool variable to track success of the operation.
    This macro is expanded to do deserialization for both:
    - user-defined structs: the output is an instance of the corresponding
      struct wrapper class.
    - method parameters/response parameters: the output is a list of
      arguments. #}
{%- macro deserialize(struct, input, output_field_pattern, context, success) -%}
  do {
    // NOTE: The memory backing |{{input}}| may has be smaller than
    // |sizeof(*{{input}})| if the message comes from an older version.
{#-   Before deserialize fields introduced at a certain version, we need to add
      a version check, which makes sure we skip further deserialization if
      |input| is from an earlier version. |last_checked_version| records the
      last version that we have added such version check. #}
{%-   set last_checked_version = 0 %}
{%-   for pf in struct.packed.packed_fields_in_ordinal_order %}
{%-     set output_field = output_field_pattern|format(pf.field.name) %}
{%-     set name = pf.field.name %}
{%-     set kind = pf.field.kind %}
{%-     if pf.min_version > last_checked_version %}
{%-       set last_checked_version = pf.min_version %}
    if ({{input}}->header_.version < {{pf.min_version}})
      break;
{%-     endif %}
{%-     if kind|is_native_only_kind and kind|is_typemapped_kind %}
    if (!DeserializeNative_(
            {{input}}->{{name}}.ptr, &{{output_field}}, {{context}})) {
      {{success}} = false;
    }
{%-     elif kind|is_typemapped_kind %}
    if (!{{kind|get_name_for_kind}}_SerializerTraits_<{{kind|cpp_wrapper_type}}>
            ::Deserialize(
                {{input}}->{{name}}.ptr, &{{output_field}}, {{context}})) {
      {{success}} = false;
    }
{%-     elif kind|is_object_kind %}
{%-       if kind|is_union_kind %}
    if (!Deserialize_(&{{input}}->{{name}}, &{{output_field}}, {{context}}))
      {{success}} = false;
{%-       else %}
    if (!Deserialize_({{input}}->{{name}}.ptr, &{{output_field}}, {{context}}))
      {{success}} = false;
{%-       endif %}
{%-     elif kind|is_interface_kind %}
    mojo::internal::InterfaceDataToPointer(&{{input}}->{{name}}, &{{output_field}});
{%-     elif kind|is_interface_request_kind %}
    {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(&{{input}}->{{name}})));
{%-     elif kind|is_any_handle_kind %}
    {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}}));
{%-   elif kind|is_associated_interface_kind %}
    mojo::internal::AssociatedInterfaceDataToPtrInfo(&{{input}}->{{name}}, &{{output_field}}, ({{context}})->router.get());
{%-   elif kind|is_associated_interface_request_kind %}
    mojo::internal::AssociatedInterfaceRequestHelper::SetHandle(
        &{{output_field}},
        ({{context}})->router->CreateLocalEndpointHandle(mojo::internal::FetchAndReset(&{{input}}->{{name}})));
{%-     elif kind|is_enum_kind %}
    {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name}}.value);
{%-     else %}
    {{output_field}} = {{input}}->{{name}};
{%-     endif %}
{%-   endfor %}
  } while (false);
{%- endmacro %}