From c97faa25605260e552e6ab405dd82eeb75c34a06 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Mon, 17 Sep 2018 18:46:10 -0700 Subject: [PATCH] specialize std::hash for enum classes Summary: This diff fixes the compilation error: "implicit instantiation of undefined template std::hash" when using TextAttributes in Android Reviewed By: shergin Differential Revision: D9849407 fbshipit-source-id: 7fcb94b1d4f7715d8037ecbf302d8f345e99e9fd --- .../fabric/attributedstring/primitives.h | 89 +++++++++++++++++++ .../fabric/core/layout/LayoutPrimitives.h | 15 ++++ .../fabric/graphics/platform/android/Color.h | 13 +++ 3 files changed, 117 insertions(+) diff --git a/ReactCommon/fabric/attributedstring/primitives.h b/ReactCommon/fabric/attributedstring/primitives.h index 84d30e1566b..a59024e53a6 100644 --- a/ReactCommon/fabric/attributedstring/primitives.h +++ b/ReactCommon/fabric/attributedstring/primitives.h @@ -7,6 +7,10 @@ #pragma once + +#include +#include + namespace facebook { namespace react { @@ -81,6 +85,7 @@ enum class TextDecorationLineStyle { Double }; + enum class TextDecorationLinePattern { Solid, Dot, @@ -89,6 +94,90 @@ enum class TextDecorationLinePattern { DashDotDot, }; + } // namespace react } // namespace facebook +namespace std +{ + template <> + struct hash + { + size_t operator()(const facebook::react::FontVariant& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::TextAlignment& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::FontStyle& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::TextDecorationLineType& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::WritingDirection& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::TextDecorationLinePattern& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::TextDecorationLineStyle& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::FontWeight& v) const + { + return hash()(static_cast(v)); + } + }; + + template <> + struct hash + { + size_t operator()(const facebook::react::EllipsizeMode& v) const + { + return hash()(static_cast(v)); + } + }; +} diff --git a/ReactCommon/fabric/core/layout/LayoutPrimitives.h b/ReactCommon/fabric/core/layout/LayoutPrimitives.h index bce2bc46fdf..fc74187e823 100644 --- a/ReactCommon/fabric/core/layout/LayoutPrimitives.h +++ b/ReactCommon/fabric/core/layout/LayoutPrimitives.h @@ -7,6 +7,9 @@ #pragma once +#include +#include + namespace facebook { namespace react { @@ -31,3 +34,15 @@ enum class LayoutDirection { } // namespace react } // namespace facebook + +namespace std +{ + template <> + struct hash + { + size_t operator()(const facebook::react::LayoutDirection& v) const + { + return hash()(static_cast(v)); + } + }; +} diff --git a/ReactCommon/fabric/graphics/platform/android/Color.h b/ReactCommon/fabric/graphics/platform/android/Color.h index 69b13e76c78..3ae7d5cb8e8 100644 --- a/ReactCommon/fabric/graphics/platform/android/Color.h +++ b/ReactCommon/fabric/graphics/platform/android/Color.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include @@ -58,3 +59,15 @@ ColorComponents colorComponentsFromColor(SharedColor color); } // namespace react } // namespace facebook + +namespace std +{ + template <> + struct hash + { + size_t operator()(const facebook::react::SharedColor& sharedColor) const + { + return hash{}(*sharedColor); + } + }; +}