{# This provides some helper macros for C++ templates.  See also moo.templates.cpp #}

{%- macro field_type(ft) %}
{% if ft is mapping %}std::vector<{{field_type(ft["items"])}}>{% elif ft == "string" %}std::string{% elif ft == "any"%}model.anytype{% else %}{{ ft }}{% endif %}
{%- endmacro -%}

{% macro declare_sequence(model, t) %}
using {{t.name}} = {{model.lang.types.sequence}}<{{t["items"]|replace(".","::")}}>;
{%- endmacro -%}

{% macro declare_record(model, t) %}
struct {{t.name}}{% set comma=":" %}{% for b in t.bases %}{{comma}} public {{(b.path+[b.name])|join("::") }} {% set comma = ","%}{%endfor%} 
{
    {% for f in t.fields %}

    // @brief {{f.doc}}
    {{f.item|listify|relpath(model.path)|join("::")}} {{f.name}} = {{cpp.field_default(model.all_types, f)}};
    {% endfor %}
};
{%- endmacro -%}

{% macro declare_boolean(model, t) %}
using {{t.name}} = {{model.lang.types.boolean}};
{%- endmacro -%}

{% macro declare_string(model, t) %}
using {{t.name}} = {{model.lang.types.string}};
{%- endmacro -%}

{% macro declare_number(model, t) %}
{% if t.dtype[0] == "u" %}
using {{t.name}} = {{model.lang.dtypes[t.dtype]}}; // NOLINT
{% else %}
using {{t.name}} = {{model.lang.dtypes[t.dtype]}};
{% endif %}
{%- endmacro -%}

{% macro declare_any(model, t) %}
using {{t.name}} = {{model.lang.types.any}};
{%- endmacro -%}

{% macro declare_oneOf(model, t) %}
using {{t.name}} = std::variant<{%for one in t.types%}{{one|replace(".","::")}}{{ ", " if not loop.last }}{%endfor%}>;
{%- endmacro -%}

{% macro declare_enum(model, t) %}
enum class {{t.name}}: unsigned {
    {% for sname in t.symbols %}
    {{sname}},
    {% endfor %}
};
// return a string representation of a {{t.name}}.
inline
const char* str({{t.name}} val) {
    {% for sname in t.symbols %}
    if (val == {{t.name}}::{{sname}}) { return "{{sname}}" ;}
    {% endfor %}
    return "";                  // should not reach
}
inline
{{t.name}} parse_{{t.name}}(std::string val, {{t.name}} def = {{t.name}}::{{t.default}}) {
    {% for sname in t.symbols %}
    if (val == "{{sname}}") { return {{t.name}}::{{sname}}; }
    {% endfor %}
    return def;
}
{%- endmacro -%}

{% macro ns(model) %}
namespace {{ "::".join(model.path) }}
{%- endmacro -%}

{% macro headerguard(model, what) %}
{{ "_".join(model.path).upper() + "_" + what.upper() + "_HPP" }}
{%- endmacro -%}
