From 909b0fe0e79ce7d5ec78bc71d62b1b28b368b64d Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Thu, 12 Dec 2019 12:46:33 -0800 Subject: [PATCH] Fabric: `operator==` and `std::hash` for `AttributedStringBox` Summary: That will allow building a custom caching table for boxed stings to cache text measurements for them in TextLayoutManager. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D18950428 fbshipit-source-id: 8ceef29543dc6ed540043e32d65f3295030ae90f --- .../attributedstring/AttributedStringBox.cpp | 21 +++++++++++++++++++ .../attributedstring/AttributedStringBox.h | 18 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/ReactCommon/fabric/attributedstring/AttributedStringBox.cpp b/ReactCommon/fabric/attributedstring/AttributedStringBox.cpp index d0846b7252d..5983a7fc754 100644 --- a/ReactCommon/fabric/attributedstring/AttributedStringBox.cpp +++ b/ReactCommon/fabric/attributedstring/AttributedStringBox.cpp @@ -40,5 +40,26 @@ std::shared_ptr AttributedStringBox::getOpaquePointer() const { return opaquePointer_; } +bool operator==( + AttributedStringBox const &lhs, + AttributedStringBox const &rhs) { + if (lhs.getMode() != rhs.getMode()) { + return false; + } + + switch (lhs.getMode()) { + case facebook::react::AttributedStringBox::Mode::Value: + return lhs.getValue() == rhs.getValue(); + case facebook::react::AttributedStringBox::Mode::OpaquePointer: + return lhs.getOpaquePointer() == rhs.getOpaquePointer(); + } +} + +bool operator!=( + AttributedStringBox const &lhs, + AttributedStringBox const &rhs) { + return !(lhs == rhs); +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/attributedstring/AttributedStringBox.h b/ReactCommon/fabric/attributedstring/AttributedStringBox.h index ee8cf099223..8b7d4da1c59 100644 --- a/ReactCommon/fabric/attributedstring/AttributedStringBox.h +++ b/ReactCommon/fabric/attributedstring/AttributedStringBox.h @@ -58,5 +58,23 @@ class AttributedStringBox final { std::shared_ptr opaquePointer_; }; +bool operator==(AttributedStringBox const &lhs, AttributedStringBox const &rhs); +bool operator!=(AttributedStringBox const &lhs, AttributedStringBox const &rhs); + } // namespace react } // namespace facebook + +template <> +struct std::hash { + size_t operator()( + facebook::react::AttributedStringBox const &attributedStringBox) const { + switch (attributedStringBox.getMode()) { + case facebook::react::AttributedStringBox::Mode::Value: + return std::hash()( + attributedStringBox.getValue()); + case facebook::react::AttributedStringBox::Mode::OpaquePointer: + return std::hash>()( + attributedStringBox.getOpaquePointer()); + } + } +};