Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Unified Diff: mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl

Issue 2715153004: Mojo: Support enums as map keys in WTF (Closed)
Patch Set: Address review comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl
diff --git a/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl
index 9400ca77041e65cf319e0512101d619d4b63ecb6..92073bc5d5973e07791753c57e3de40799b4e286 100644
--- a/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl
+++ b/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl
@@ -78,3 +78,50 @@ template <>
struct hash<{{enum_name}}>
: public mojo::internal::EnumHashImpl<{{enum_name}}> {};
{%- endmacro %}
+
+{%- macro enum_hash_blink(enum) %}
+{%- set enum_name = enum|get_qualified_name_for_kind(
+ flatten_nested_kind=True, include_variant=False) %}
+{%- set hash_fn_name = enum|wtf_hash_fn_name_for_enum %}
+{# We need two unused enum values: #}
+{%- set empty_value = -1000000 %}
+{%- set deleted_value = -1000001 %}
+{%- set empty_value_unused = "false" if empty_value in enum|all_enum_values else "true" %}
+{%- set deleted_value_unused = "false" if empty_value in enum|all_enum_values else "true" %}
+namespace WTF {
+struct {{hash_fn_name}} {
+ static unsigned hash(const {{enum_name}}& value) {
+ typedef base::underlying_type<{{enum_name}}>::type utype;
+ return DefaultHash<utype>::Hash().hash(static_cast<utype>(value));
+ }
+ static bool equal(const {{enum_name}}& left, const {{enum_name}}& right) {
+ return left == right;
+ }
+ static const bool safeToCompareToEmptyOrDeleted = true;
+};
+
+template <>
+struct DefaultHash<{{enum_name}}> {
+ using Hash = {{hash_fn_name}};
+};
+
+template <>
+struct HashTraits<{{enum_name}}>
+ : public GenericHashTraits<{{enum_name}}> {
+ static_assert({{empty_value_unused}},
+ "{{empty_value}} is a reserved enum value");
+ static_assert({{deleted_value_unused}},
+ "{{deleted_value}} is a reserved enum value");
+ static const bool hasIsEmptyValueFunction = true;
+ static bool isEmptyValue(const {{enum_name}}& value) {
+ return value == static_cast<{{enum_name}}>({{empty_value}});
+ }
+ static void constructDeletedValue({{enum_name}}& slot, bool) {
+ slot = static_cast<{{enum_name}}>({{deleted_value}});
+ }
+ static bool isDeletedValue(const {{enum_name}}& value) {
+ return value == static_cast<{{enum_name}}>({{deleted_value}});
+ }
+};
+} // namespace WTF
+{%- endmacro %}

Powered by Google App Engine
This is Rietveld 408576698