chromium/mojo/public/tools/bindings/generators/cpp_templates/validation_macros.tmpl

{#- Validates the specified field, which is supposed to be an object
    (struct/array/string/map/union). If it is a union, |union_is_inlined|
    indicates whether the union is inlined. (Nested unions are not inlined.)
    This macro is expanded by the Validate() method. #}
{%- macro validate_object(field, field_index, field_expr, union_is_inlined) %}
{%-   set name = field.name %}
{%-   set kind = field.kind %}
{%-   if not kind|is_nullable_kind %}
{%-     if kind|is_union_kind and union_is_inlined %}
  if (!mojo::internal::ValidateInlinedUnionNonNullable(
          {{field_expr}}, {{field_index}}, validation_context)) {
    return false;
  }
{%-     else %}
  if (!mojo::internal::ValidatePointerNonNullable(
          {{field_expr}}, {{field_index}}, validation_context)) {
    return false;
  }
{%-     endif %}
{%-   endif %}
{%-   if kind|is_array_kind or kind|is_string_kind or kind|is_map_kind %}
  constexpr const mojo::internal::ContainerValidateParams& {{name}}_validate_params =
      {{kind|get_container_validate_params_ctor_args|indent(6)}};
  if (!mojo::internal::ValidateContainer({{field_expr}}, validation_context,
                                         &{{name}}_validate_params)) {
    return false;
  }
{%-   elif kind|is_struct_kind %}
  if (!mojo::internal::ValidateStruct({{field_expr}}, validation_context))
    return false;
{%-   elif kind|is_union_kind %}
{%-     if union_is_inlined %}
  if (!mojo::internal::ValidateInlinedUnion({{field_expr}}, validation_context))
    return false;
{%-     else %}
  if (!mojo::internal::ValidateNonInlinedUnion({{field_expr}},
                                               validation_context))
    return false;
{%-     endif %}
{%-   else %}
#error Not reached!
{%-   endif %}
{%- endmacro %}

{#- Validates the specified field, which is supposed to be a handle,
    an interface, an associated interface or an associated interface request.
    This macro is expanded by the Validate() method. #}
{%- macro validate_handle_or_interface(field, field_index, field_expr) %}
{%-   set name = field.name %}
{%-   set kind = field.kind %}
{%-   if not kind|is_nullable_kind %}
  if (!mojo::internal::ValidateHandleOrInterfaceNonNullable(
          {{field_expr}}, {{field_index}}, validation_context)) {
    return false;
  }
{%-   endif %}
  if (!mojo::internal::ValidateHandleOrInterface({{field_expr}},
                                                 validation_context)) {
    return false;
  }
{%- endmacro %}

{#- Validates the specified field, which is supposed to be an enum.
    This macro is expanded by the Validate() method. #}
{%- macro validate_enum(field, field_expr) %}
  if (!{{field.kind|get_qualified_name_for_kind(internal=True,flatten_nested_kind=True)}}
        ::Validate({{field_expr}}, validation_context))
    return false;
{%- endmacro %}

{%- macro validate_field(field, field_index, field_expr, union_is_inlined) %}
{%-   if field.kind|is_object_kind -%}
{{validate_object(field, field_index, field_expr, union_is_inlined)}}
{%-   elif field.kind|is_any_handle_or_interface_kind -%}
{{validate_handle_or_interface(field, field_index, field_expr)}}
{%-   elif field.kind|is_enum_kind %}
{{validate_enum(field, field_expr)}}
{%-   endif %}
{%- endmacro %}