Skip to content

Latest commit

 

History

History
87 lines (73 loc) · 2.04 KB

main_statements.md

File metadata and controls

87 lines (73 loc) · 2.04 KB
layout title parent nav_order
prev_next_page
Main statements
Jinja2C++ Usage
2

Main Jinja2C++ statements

{: .no_toc }

Table of contents

{: .no_toc .text-delta }

  1. TOC {:toc}

Condition statement

TODO:

Iteration statement

TODO:

'set' statement

But what if enum Animals will be in the namespace?

namespace world
{
enum Animals
{
    Dog,
    Cat,
    Monkey,
    Elephant
};
}

In this case you need to prefix both enum name and it's items with namespace prefix in the generated code. Like this: {% raw %}

std::string enum2StringConvertor = R"(
inline const char* {{enum.enumName}}ToString({{enum.nsScope}}::{{enum.enumName}} e)
{
    switch (e)
    {
{% for item in enum.items %}
    case {{enum.nsScope}}::{{item}}:
        return "{{item}}";
{% endfor %}
    }
    return "Unknown Item";
})";

{% endraw %}

This template will produce 'world::' prefix for our new scoped enum (and enum itmes). And '::' for the ones in global scope. But you may want to eliminate the unnecessary global scope prefix. And you can do it this way: {% raw %}

{% set prefix = enum.nsScope + '::' if enum.nsScope else '' %}
std::string enum2StringConvertor = R"(inline const char* {{enum.enumName}}ToString({{prefix}}::{{enum.enumName}} e)
{
    switch (e)
    {
{% for item in enum.items %}
    case {{prefix}}::{{item}}:
        return "{{item}}";
{% endfor %}
    }
    return "Unknown Item";
})";

{% endraw %}

This template uses two significant jinja2 template features:

  1. The 'set' statement. You can declare new variables in your template. And you can access them by the name.
  2. if-expression. It works like a ternary '?:' operator in C/C++. In C++ the code from the sample could be written in this way:
std::string prefix = !descr.nsScope.empty() ? descr.nsScope + "::" : "";

I.e. left part of this expression (before 'if') is a true-branch of the statement. Right part (after 'else') - false-branch, which can be omitted. As a condition you can use any expression convertible to bool.

'with' statement

TODO: