{% import "mojolpm_macros.tmpl" as util %}
{%- macro define_struct(struct) %}
{%- set mojom_type = struct|cpp_wrapper_type(add_same_module_namespaces=true) %}
{%- set proto_type = "::mojolpm" ~ (struct|get_qualified_name_for_kind(flatten_nested_kind=True)) %}
{%- set struct_type = proto_type ~ "_ProtoStruct" %}
{%- set dataview_type = (struct|get_qualified_name_for_kind(flatten_nested_kind=True)) ~ "DataView" %}
template <>
struct StructTraits<{{dataview_type}}, {{struct_type}}> {
{%- for field in struct.fields %}
{%- set name = field.name|camel_to_under %}
{%- set kind = field.kind %}
{%- if field.kind|is_nullable_kind and not field.kind|nullable_is_same_kind %}
{%- set field_mojom_type = kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
{%- set unnullable_kind = kind|to_unnullable_kind %}
{%- set field_maybe_mojom_type = unnullable_kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
static {{field_mojom_type}} {{field.name}}(
const {{struct_type}}& input) {
{{field_mojom_type}} maybe_local_{{name}}{ {{- kind|default_constructor_args -}} };
{{field_maybe_mojom_type}} local_{{name}}{ {{- unnullable_kind|default_constructor_args -}} };
if (input.has_m_{{name}}() && mojolpm::FromProto(
input.m_{{name}}(),
local_{{name}})) {
maybe_local_{{name}} = std::move(local_{{name}});
}
return maybe_local_{{name}};
}{{ "\n" }}
{%- else %}
{%- set field_mojom_type = kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
static {{field_mojom_type}} {{field.name}}(
const {{struct_type}}& input) {
{{field_mojom_type}} local_{{name}}{ {{- kind|default_constructor_args -}} };
(void) mojolpm::FromProto(
input.m_{{name}}(),
local_{{name}});
return local_{{name}};
}{{ "\n" }}
{%- endif %}
{%- endfor -%}
};
{% endmacro -%}
{%- macro define_union(union) -%}
{%- set mojom_type = union|cpp_wrapper_type(add_same_module_namespaces=true) %}
{%- set proto_type = "::mojolpm" ~ (union|get_qualified_name_for_kind(flatten_nested_kind=True)) %}
{%- set union_type = proto_type ~ "_ProtoUnion" %}
{%- if union|is_typemapped_kind %}
{%- set dataview_type = (union|get_qualified_name_for_kind(flatten_nested_kind=True)) ~ "DataView" %}
template<>
struct UnionTraits<{{dataview_type}}, {{union_type}}> {
static {{dataview_type}}::Tag GetTag(
const {{union_type}}& input) {
switch (input.union_member_case()) {
{%- for field in union.fields %}
{%- set name = field.name|camel_to_under %}
{%- set kind = field.kind %}
{%- set field_mojom_type = kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
case {{union_type}}::k{{("m_" ~ name)|under_to_camel}}:
return {{dataview_type}}::Tag::k{{name|under_to_camel}};
{%- endfor %}
default:
return static_cast<{{dataview_type}}::Tag>(0);
}
}{{"\n"}}
{%- for field in union.fields %}
{%- set name = field.name|camel_to_under %}
{%- set kind = field.kind %}
{%- if field.kind|is_nullable_kind and not field.kind|nullable_is_same_kind %}
{%- set field_mojom_type = kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
{%- set unnullable_kind = kind|to_unnullable_kind %}
{%- set field_maybe_mojom_type = unnullable_kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
static {{field_mojom_type}} {{field.name}}(
const {{union_type}}& input) {
{{field_mojom_type}} maybe_local_{{name}}{ {{- kind|default_constructor_args -}} };
{{field_maybe_mojom_type}} local_{{name}}{ {{- unnullable_kind|default_constructor_args -}} };
if (mojolpm::FromProto(
input.m_{{name}}(),
local_{{name}})) {
maybe_local_{{name}} = std::move(local_{{name}});
}
return maybe_local_{{name}};
}{{ "\n" }}
{%- else %}
{%- set field_mojom_type = kind|cpp_wrapper_type(add_same_module_namespaces=true) %}
static {{field_mojom_type}} {{field.name}}(
const {{union_type}}& input) {
{{field_mojom_type}} local_{{name}}{ {{- kind|default_constructor_args -}} };
(void) mojolpm::FromProto(
input.m_{{name}}(),
local_{{name}});
return local_{{name}};
}{{ "\n" }}
{%- endif %}
{%- endfor -%}
};
{% endif %}
{% endmacro -%}