Chromium Code Reviews| 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 ---#} | 3 ---#} |
| 4 | |
| 4 {%- macro enum_decl(enum) %} | 5 {%- macro enum_decl(enum) %} |
| 5 enum class {{enum.name}} : int32_t { | 6 {%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %} |
|
Ken Rockot(use gerrit already)
2016/08/17 15:10:55
clever solution :)
| |
| 6 {%- for field in enum.fields %} | 7 enum class {{enum_name}} : int32_t { |
| 7 {%- if field.value %} | 8 {%- for field in enum.fields %} |
| 9 {%- if field.value %} | |
| 8 {{field.name}} = {{field.value|expression_to_text}}, | 10 {{field.name}} = {{field.value|expression_to_text}}, |
| 9 {%- else %} | 11 {%- else %} |
| 10 {{field.name}}, | 12 {{field.name}}, |
| 11 {%- endif %} | 13 {%- endif %} |
| 12 {%- endfor %} | 14 {%- endfor %} |
| 13 }; | 15 }; |
| 16 | |
| 17 inline std::ostream& operator<<(std::ostream& os, {{enum_name}} value) { | |
| 18 switch(value) { | |
| 19 {%- for _, values in enum.fields|groupby('numeric_value') %} | |
| 20 case {{enum_name}}::{{values[0].name}}: | |
| 21 return os << "{{enum_name}}:: | |
| 22 {%- if values|length > 1 -%} | |
| 23 {{'{'}} | |
| 24 {%- endif -%} | |
| 25 {{values|map(attribute='name')|join(', ')}} | |
| 26 {%- if values|length > 1 -%} | |
| 27 {{'}'}} | |
| 28 {%- endif -%} | |
| 29 "; | |
| 30 {%- endfor %} | |
| 31 default: | |
| 32 return os << "Unknown {{enum_name}} value: " << static_cast<int32_t>(value ); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 {#- Returns true if the given enum value exists in this version of enum. #} | |
| 37 inline bool IsKnownEnumValue({{enum_name}} value) { | |
| 38 return {{enum|get_name_for_kind(internal=True, | |
| 39 flatten_nested_kind=True)}}::IsKnownValue( | |
| 40 static_cast<int32_t>(value)); | |
| 41 } | |
| 14 {%- endmacro %} | 42 {%- endmacro %} |
| 15 | 43 |
| 16 {%- macro enum_data_decl(enum) %} | 44 {%- macro enum_data_decl(enum) %} |
| 17 struct {{enum.name}}_Data { | 45 {%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %} |
| 46 struct {{enum_name}}_Data { | |
| 18 public: | 47 public: |
| 19 static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{ % endif %}; | 48 static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{ % endif %}; |
| 20 | 49 |
| 21 static bool IsKnownValue(int32_t value) { | 50 static bool IsKnownValue(int32_t value) { |
| 22 {%- if enum.fields %} | 51 {%- if enum.fields %} |
| 23 switch (value) { | 52 switch (value) { |
| 24 {%- for enum_field in enum.fields|groupby('numeric_value') %} | 53 {%- for enum_field in enum.fields|groupby('numeric_value') %} |
| 25 case {{enum_field[0]}}: | 54 case {{enum_field[0]}}: |
| 26 {%- endfor %} | 55 {%- endfor %} |
| 27 return true; | 56 return true; |
| 28 } | 57 } |
| 29 {%- endif %} | 58 {%- endif %} |
| 30 return false; | 59 return false; |
| 31 } | 60 } |
| 32 | 61 |
| 33 static bool Validate(int32_t value, | 62 static bool Validate(int32_t value, |
| 34 mojo::internal::ValidationContext* validation_context) { | 63 mojo::internal::ValidationContext* validation_context) { |
| 35 if (kIsExtensible || IsKnownValue(value)) | 64 if (kIsExtensible || IsKnownValue(value)) |
| 36 return true; | 65 return true; |
| 37 | 66 |
| 38 ReportValidationError(validation_context, | 67 ReportValidationError(validation_context, |
| 39 mojo::internal::VALIDATION_ERROR_UNKNOWN_ENUM_VALUE); | 68 mojo::internal::VALIDATION_ERROR_UNKNOWN_ENUM_VALUE); |
| 40 return false; | 69 return false; |
| 41 } | 70 } |
| 42 }; | 71 }; |
| 43 {%- endmacro %} | 72 {%- endmacro %} |
| 44 | 73 |
| 45 {#--- macros for enum-associated functions. Namely: | 74 {%- macro enum_hash(enum) %} |
| 46 * operator<<(): outputs the given enum value. | 75 {%- set enum_name = enum|get_qualified_name_for_kind( |
| 47 * IsKnownEnumValue(): returns true if the given enum value exists in this | 76 flatten_nested_kind=True) %} |
| 48 generated version of enum. | 77 template <> |
| 49 ---#} | 78 struct hash<{{enum_name}}> |
| 50 | 79 : public mojo::internal::EnumHashImpl<{{enum_name}}> {}; |
| 51 {%- macro enum_stream_operator(enum) %} | |
| 52 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val ue) { | |
| 53 switch(value) { | |
| 54 {%- for _, values in enum.fields|groupby('numeric_value') %} | |
| 55 case {{enum|get_name_for_kind}}::{{values[0].name}}: | |
| 56 return os << "{{enum|get_name_for_kind}}:: | |
| 57 {%- if values|length > 1 -%} | |
| 58 {{'{'}} | |
| 59 {%- endif -%} | |
| 60 {{values|map(attribute='name')|join(', ')}} | |
| 61 {%- if values|length > 1 -%} | |
| 62 {{'}'}} | |
| 63 {%- endif -%} | |
| 64 "; | |
| 65 {%- endfor %} | |
| 66 default: | |
| 67 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i nt32_t>(value); | |
| 68 } | |
| 69 } | |
| 70 {%- endmacro %} | 80 {%- endmacro %} |
| 71 | |
| 72 {%- macro is_known_enum_value(enum) %} | |
| 73 inline bool IsKnownEnumValue({{enum|get_name_for_kind}} value) { | |
| 74 return {{enum|get_qualified_name_for_kind(internal=True)}}::IsKnownValue( | |
| 75 static_cast<int32_t>(value)); | |
| 76 } | |
| 77 {%- endmacro %} | |
| 78 | |
| 79 {%- macro enum_hash(enum) %} | |
| 80 template <> | |
| 81 struct hash<{{enum|get_qualified_name_for_kind}}> | |
| 82 : public mojo::internal::EnumHashImpl<{{enum|get_qualified_name_for_kind}}> {}; | |
| 83 {%- endmacro %} | |
| OLD | NEW |