aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/tools/bindings/generators/cpp_templates/validation_macros.tmpl
blob: a50a585c09a89a0c813bd1cddfb7e4b52f27a405 (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
{#- 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_expr, object_name, 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}}, "null {{name}} field in {{object_name}}",
          validation_context)) {
    return false;
  }
{%-     else %}
  if (!mojo::internal::ValidatePointerNonNullable(
          {{field_expr}}, "null {{name}} field in {{object_name}}",
          validation_context)) {
    return false;
  }
{%-     endif %}
{%-   endif %}
{%-   if kind|is_array_kind or kind|is_string_kind or kind|is_map_kind %}
  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_expr, object_name) %}
{%-   set name = field.name %}
{%-   set kind = field.kind %}
{%-   if not kind|is_nullable_kind %}
  if (!mojo::internal::ValidateHandleOrInterfaceNonNullable(
          {{field_expr}},
          "invalid {{name}} field in {{object_name}}", 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_expr, object_name, union_is_inlined) %}
{%-   if field.kind|is_object_kind -%}
{{validate_object(field, field_expr, object_name, union_is_inlined)}}
{%-   elif field.kind|is_any_handle_or_interface_kind -%}
{{validate_handle_or_interface(field, field_expr, object_name)}}
{%-   elif field.kind|is_enum_kind %}
{{validate_enum(field, field_expr)}}
{%-   endif %}
{%- endmacro %}