OLD | NEW |
| (Empty) |
1 {#--- | |
2 Macro for enum definition, and the declaration of associated functions. | |
3 `is_static` is relevant if this enum declaration is defined within a class, in | |
4 which case associated functions need to be static. | |
5 ---#} | |
6 {%- macro enum_decl(enum, is_static=false) %} | |
7 enum class {{enum.name}} : int32_t { | |
8 {%- for field in enum.fields %} | |
9 {%- if field.value %} | |
10 {{field.name}} = {{field.value|expression_to_text}}, | |
11 {%- else %} | |
12 {{field.name}}, | |
13 {%- endif %} | |
14 {%- endfor %} | |
15 }; | |
16 {{is_valid_enum_decl(enum, is_static)}} | |
17 {%- endmacro %} | |
18 | |
19 {#--- macros for the declaration & definitions of enum-associated functions. | |
20 Namely: | |
21 * {enum_name}_IsValidValue: returns true if the given enum has a valid value | |
22 for this generated version of enum. | |
23 ---#} | |
24 | |
25 {%- macro is_valid_enum_decl(enum, is_static=false) %} | |
26 {% if is_static %}static {% endif -%} | |
27 bool {{enum.name}}_IsValidValue({{enum.name}} value); | |
28 {%- endmacro %} | |
29 | |
30 {%- macro is_valid_enum_def(enum, class_name = '') %} | |
31 {% if class_name != '' -%} | |
32 // static | |
33 bool {{class_name}}:: | |
34 {%- else -%} | |
35 {{"bool "}} | |
36 {%- endif -%} | |
37 {{enum.name}}_IsValidValue({{enum.name}} value) { | |
38 switch (static_cast<int32_t>(value)) { | |
39 {%- for enum_field in enum.fields|groupby('numeric_value') %} | |
40 case {{enum_field[0]}}: | |
41 {%- endfor %} | |
42 return true; | |
43 } | |
44 return false; | |
45 } | |
46 {%- endmacro %} | |
47 | |
48 {%- macro global_enum_operators_decl(enum, class_name = '') %} | |
49 {% if class_name != '' -%} | |
50 std::ostream& operator<<(std::ostream& stream, | |
51 const {{class_name}}::{{enum.name}}& val); | |
52 {%- else -%} | |
53 std::ostream& operator<<(std::ostream& stream, const {{enum.name}}& val); | |
54 {%- endif -%} | |
55 {%- endmacro %} | |
56 | |
57 {%- macro global_enum_operators_def(enum, class_name = '') %} | |
58 {% if class_name != '' -%} | |
59 std::ostream& operator<<(std::ostream& stream, | |
60 const {{class_name}}::{{enum.name}}& val) { | |
61 return (stream << static_cast<int32_t>(val)); | |
62 } | |
63 {%- else -%} | |
64 std::ostream& operator<<(std::ostream& stream, const {{enum.name}}& val) { | |
65 return (stream << static_cast<int32_t>(val)); | |
66 } | |
67 {%- endif -%} | |
68 {%- endmacro %} | |
OLD | NEW |