{% from "macros.j2" import cls_name,pointer,super,argtypes,argnames,method_pointer,
static_method_pointer,function_pointer,template_args_typename,template_args,
template_method_pointer,template_static_method_pointer, template_pointer,
prototype, pybind_overload, trampoline_class %}

// pybind 11 related includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

{% if platform.startswith('win') %}
// Windows specific
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
{% endif %}

// Standard Handle
#include <Standard_Handle.hxx>

// user-defined inclusion per module before includes
{% if module_settings['include_header_pre_top'] %}
{{ module_settings['include_header_pre_top'] }}
{% endif %}

// includes to resolve forward declarations
{% for h in module.headers %}{% for f in h.forwards %}{% if f.name in class_dict %}
#include <{{ class_dict[f.name].splitpath()[-1] }}>
{% endif %}{% endfor %}{% endfor %}

// module includes
{% for h in module.headers | sort(attribute='short_name') %}
#include <{{ h.short_name }}>
{% endfor %}

// template related includes
{% for td in  classes_typedefs %}
{% if 'TypedefInfo' in str(type(td)) %}

{% if not td.pod and td.template_base.__len__()>0
    and not td.type.startswith("opencascade::handle")
    and not td.type.startswith("std::") %}
{% set dep_module = td.type.split("_") %}
#include "{{dep_module[0]}}_tmpl.hxx"
{% endif %}
{% endif %}
{% endfor %}


// user-defined pre
{% if include_pre %}
#include "{{ include_pre }}"
{% endif %}

// user-defined inclusion per module
{% if module_settings['include_header_pre'] %}
{{ module_settings['include_header_pre'] }}
{% endif %}

// Module definiiton
void register_{{module.name}}_enums(py::module &main_module) {


py::module m = main_module.def_submodule("{{module.name}}", R"#({{module.doc}})#");

// add namespaces as submodules
{% for ns in module.namespaces %}
m.def_submodule("{{ns}}");
{% endfor %}

// user-defined inclusion per module in the body
{% if module_settings['include_body_pre'] %}
{{ module_settings['include_body_pre'] }}
{% endif %}

// enums
{% for enum in module.enums %}
{% if enum.anonymous %}
    {% for val in enum.values %}
    m.attr("{{ val }}") = py::cast(int({{ enum.name+"::" if enum.name}}{{ val }}));
    {% endfor %}
{% else %}
    py::enum_<{{enum.name}}>(m, "{{enum.name}}",R"#({{enum.comment}})#")
    {% for val in enum.values %}
        .value("{{val}}",{{enum.name}}::{{val}}){{ ".export_values();" if loop.last }}
    {% endfor %}
{% endif %}
{% endfor %}

//Python trampoline classes
{% for c in module.classes %}{% if c.abstract %}
    {{ trampoline_class(c) }}
{% endif %}{% endfor %}

// pre-register typdefs+classes (topologically sorted)
{% for el in  classes_typedefs %}
{% if 'TypedefInfo' in str(type(el)) %}
{% if not el.pod and not "_H" in el.name and el.template_base.__len__()>0 and not el.type.startswith("opencascade::handle") %}
{% set dep_module = el.template_base[0].split("_") %}
{% set arg = el.type.split(el.template_base[0])[-1] %}
{% set base =  el.template_base[0] %}
{% if not arg.endswith("::Iterator") %}
    preregister_template_{{ base }}{{ arg }}(m,"{{el.name}}");
{% endif %}
{% endif %}
{% else %}
{% if (proper_new_operator(el) and proper_delete_operator(el)) or 'Standard_Transient' in el.rootclass %}
    py::class_<{{el.name}} {{pointer(el)}} {% if el.abstract %},Py_{{el.name}}{% endif %} {{super(el,class_dict,module.typedef_dict)}}>(m,"{{cls_name(el)}}",R"#({{el.comment}})#");
{% endif %}
{% endif %}
{% endfor %}

};

// user-defined post-inclusion per module
{% if module_settings['include_header_post'] %}
{{ module_settings['include_header_post'] }}
{% endif %}

// user-defined post
{% if include_post %}
#include "{{ include_post }}"
{% endif %}
