OLD | NEW |
---|---|
1 {#--- | 1 {#--- |
2 Macro for enum definition, and the declaration of associated functions. | 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 | 3 `is_static` is relevant if this enum declaration is defined within a class, in |
4 which case associated functions need to be static. | 4 which case associated functions need to be static. |
5 ---#} | 5 ---#} |
6 {%- macro enum_decl(enum, is_static=false) %} | 6 {%- macro enum_decl(enum, is_static=false) %} |
7 {%- if enum.native_only %} | |
8 using {{enum.name}} = {{enum|cpp_wrapper_type}}; | |
9 {%- else %} | |
7 enum class {{enum.name}} : int32_t { | 10 enum class {{enum.name}} : int32_t { |
8 {%- for field in enum.fields %} | 11 {%- for field in enum.fields %} |
9 {%- if field.value %} | 12 {%- if field.value %} |
10 {{field.name}} = {{field.value|expression_to_text}}, | 13 {{field.name}} = {{field.value|expression_to_text}}, |
11 {%- else %} | 14 {%- else %} |
12 {{field.name}}, | 15 {{field.name}}, |
13 {%- endif %} | 16 {%- endif %} |
14 {%- endfor %} | 17 {%- endfor %} |
15 }; | 18 }; |
16 {{is_valid_enum_decl(enum, is_static)}} | 19 {{is_valid_enum_decl(enum, is_static)}} |
20 {% endif %} | |
yzshen1
2016/01/26 04:36:29
nit: "{%-" (and also line 54)
| |
17 {%- endmacro %} | 21 {%- endmacro %} |
18 | 22 |
19 {#--- macros for the declaration & definitions of enum-associated functions. | 23 {#--- macros for the declaration & definitions of enum-associated functions. |
20 Namely: | 24 Namely: |
21 * {enum_name}_IsValidValue: returns true if the given enum has a valid value | 25 * {enum_name}_IsValidValue: returns true if the given enum has a valid value |
22 for this generated version of enum. | 26 for this generated version of enum. |
23 ---#} | 27 ---#} |
24 | 28 |
25 {%- macro is_valid_enum_decl(enum, is_static=false) %} | 29 {%- macro is_valid_enum_decl(enum, is_static=false) %} |
26 {% if is_static %}static {% endif -%} | 30 {% if is_static %}static {% endif -%} |
27 bool {{enum.name}}_IsValidValue({{enum.name}} value); | 31 bool {{enum.name}}_IsValidValue({{enum.name}} value); |
28 {%- endmacro %} | 32 {%- endmacro %} |
29 | 33 |
30 {%- macro enum_stream_operator(enum) %} | 34 {%- macro enum_stream_operator(enum) %} |
35 {%- if not enum.native_only %} | |
31 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val ue) { | 36 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val ue) { |
32 switch(value) { | 37 switch(value) { |
33 {%- for _, values in enum.fields|groupby('numeric_value') %} | 38 {%- for _, values in enum.fields|groupby('numeric_value') %} |
34 case {{enum|get_name_for_kind}}::{{values[0].name}}: | 39 case {{enum|get_name_for_kind}}::{{values[0].name}}: |
35 return os << "{{enum|get_name_for_kind}}:: | 40 return os << "{{enum|get_name_for_kind}}:: |
36 {%- if values|length > 1 -%} | 41 {%- if values|length > 1 -%} |
37 {{'{'}} | 42 {{'{'}} |
38 {%- endif -%} | 43 {%- endif -%} |
39 {{values|map(attribute='name')|join(', ')}} | 44 {{values|map(attribute='name')|join(', ')}} |
40 {%- if values|length > 1 -%} | 45 {%- if values|length > 1 -%} |
41 {{'}'}} | 46 {{'}'}} |
42 {%- endif -%} | 47 {%- endif -%} |
43 "; | 48 "; |
44 {%- endfor %} | 49 {%- endfor %} |
45 default: | 50 default: |
46 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i nt32_t>(value); | 51 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i nt32_t>(value); |
47 } | 52 } |
48 } | 53 } |
54 {%- endif %} | |
49 {%- endmacro %} | 55 {%- endmacro %} |
50 | 56 |
51 {%- macro is_valid_enum_def(enum, class_name = '') %} | 57 {%- macro is_valid_enum_def(enum, class_name = '') %} |
52 {% if class_name != '' -%} | 58 {%- if not enum.native_only %} |
59 {% if class_name != '' -%} | |
53 // static | 60 // static |
54 bool {{class_name}}:: | 61 bool {{class_name}}:: |
55 {%- else -%} | 62 {%- else -%} |
56 {{"bool "}} | 63 {{"bool "}} |
57 {%- endif -%} | 64 {%- endif -%} |
58 {{enum.name}}_IsValidValue({{enum.name}} value) { | 65 {{enum.name}}_IsValidValue({{enum.name}} value) { |
59 switch (static_cast<int32_t>(value)) { | 66 switch (static_cast<int32_t>(value)) { |
60 {%- for enum_field in enum.fields|groupby('numeric_value') %} | 67 {%- for enum_field in enum.fields|groupby('numeric_value') %} |
61 case {{enum_field[0]}}: | 68 case {{enum_field[0]}}: |
62 {%- endfor %} | 69 {%- endfor %} |
63 return true; | 70 return true; |
64 } | 71 } |
65 return false; | 72 return false; |
66 } | 73 } |
74 {% endif %} | |
67 {%- endmacro %} | 75 {%- endmacro %} |
OLD | NEW |