Fabric: std::hash specification for AttributedString and all derivatives

Summary: We will need that to manage collections of attributed strings.

Reviewed By: mdvacca

Differential Revision: D9803351

fbshipit-source-id: 0ea9719f97ed30ff6dfe17b6dbebf448afe228b3
This commit is contained in:
Valentin Shergin 2018-09-14 15:17:02 -07:00 committed by Facebook Github Bot
parent e663b1ed59
commit 9b863d4da3
3 changed files with 112 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <functional>
#include <memory> #include <memory>
#include <fabric/attributedstring/TextAttributes.h> #include <fabric/attributedstring/TextAttributes.h>
@ -85,3 +86,29 @@ private:
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook
namespace std {
template <>
struct hash<facebook::react::AttributedString::Fragment> {
size_t operator()(const facebook::react::AttributedString::Fragment &fragment) const {
return
std::hash<decltype(fragment.string)>{}(fragment.string) +
std::hash<decltype(fragment.textAttributes)>{}(fragment.textAttributes) +
std::hash<decltype(fragment.shadowNode)>{}(fragment.shadowNode) +
std::hash<decltype(fragment.parentShadowNode)>{}(fragment.parentShadowNode);
}
};
template <>
struct hash<facebook::react::AttributedString> {
size_t operator()(const facebook::react::AttributedString &attributedString) const {
auto result = size_t {0};
for (const auto &fragment : attributedString.getFragments()) {
result += std::hash<facebook::react::AttributedString::Fragment>{}(fragment);
}
return result;
}
};
} // namespace std

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <functional>
#include <limits> #include <limits>
#include <fabric/attributedstring/primitives.h> #include <fabric/attributedstring/primitives.h>
@ -83,3 +84,34 @@ public:
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook
namespace std {
template <>
struct hash<facebook::react::TextAttributes> {
size_t operator()(const facebook::react::TextAttributes &textAttributes) const {
return
std::hash<decltype(textAttributes.foregroundColor)>{}(textAttributes.foregroundColor) +
std::hash<decltype(textAttributes.backgroundColor)>{}(textAttributes.backgroundColor) +
std::hash<decltype(textAttributes.opacity)>{}(textAttributes.opacity) +
std::hash<decltype(textAttributes.fontFamily)>{}(textAttributes.fontFamily) +
std::hash<decltype(textAttributes.fontSize)>{}(textAttributes.fontSize) +
std::hash<decltype(textAttributes.fontSizeMultiplier)>{}(textAttributes.fontSizeMultiplier) +
std::hash<decltype(textAttributes.fontWeight)>{}(textAttributes.fontWeight) +
std::hash<decltype(textAttributes.fontStyle)>{}(textAttributes.fontStyle) +
std::hash<decltype(textAttributes.fontVariant)>{}(textAttributes.fontVariant) +
std::hash<decltype(textAttributes.allowFontScaling)>{}(textAttributes.allowFontScaling) +
std::hash<decltype(textAttributes.letterSpacing)>{}(textAttributes.letterSpacing) +
std::hash<decltype(textAttributes.lineHeight)>{}(textAttributes.lineHeight) +
std::hash<decltype(textAttributes.alignment)>{}(textAttributes.alignment) +
std::hash<decltype(textAttributes.baseWritingDirection)>{}(textAttributes.baseWritingDirection) +
std::hash<decltype(textAttributes.textDecorationColor)>{}(textAttributes.textDecorationColor) +
std::hash<decltype(textAttributes.textDecorationLineType)>{}(textAttributes.textDecorationLineType) +
std::hash<decltype(textAttributes.textDecorationLineStyle)>{}(textAttributes.textDecorationLineStyle) +
std::hash<decltype(textAttributes.textDecorationLinePattern)>{}(textAttributes.textDecorationLinePattern) +
std::hash<decltype(textAttributes.textShadowOffset)>{}(textAttributes.textShadowOffset) +
std::hash<decltype(textAttributes.textShadowRadius)>{}(textAttributes.textShadowRadius) +
std::hash<decltype(textAttributes.textShadowColor)>{}(textAttributes.textShadowColor) +
std::hash<decltype(textAttributes.isHighlighted)>{}(textAttributes.isHighlighted) +
std::hash<decltype(textAttributes.layoutDirection)>{}(textAttributes.layoutDirection);
}
};
} // namespace std

View File

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <functional>
#include <tuple> #include <tuple>
#include <fabric/graphics/Float.h> #include <fabric/graphics/Float.h>
@ -177,3 +178,55 @@ using CornerInsets = RectangleCorners<Float>;
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook
namespace std {
template <>
struct hash<facebook::react::Point> {
size_t operator()(const facebook::react::Point &point) const {
return
hash<decltype(point.x)>{}(point.x) +
hash<decltype(point.y)>{}(point.y);
}
};
template <>
struct hash<facebook::react::Size> {
size_t operator()(const facebook::react::Size &size) const {
return
hash<decltype(size.width)>{}(size.width) +
hash<decltype(size.height)>{}(size.height);
}
};
template <>
struct hash<facebook::react::Rect> {
size_t operator()(const facebook::react::Rect &rect) const {
return
hash<decltype(rect.origin)>{}(rect.origin) +
hash<decltype(rect.size)>{}(rect.size);
}
};
template <typename T>
struct hash<facebook::react::RectangleEdges<T>> {
size_t operator()(const facebook::react::RectangleEdges<T> &edges) const {
return
hash<decltype(edges.left)>{}(edges.left) +
hash<decltype(edges.right)>{}(edges.right) +
hash<decltype(edges.top)>{}(edges.top) +
hash<decltype(edges.bottom)>{}(edges.bottom);
}
};
template <typename T>
struct hash<facebook::react::RectangleCorners<T>> {
size_t operator()(const facebook::react::RectangleCorners<T> &corners) const {
return
hash<decltype(corners.topLeft)>{}(corners.topLeft) +
hash<decltype(corners.bottomLeft)>{}(corners.bottomLeft) +
hash<decltype(corners.topRight)>{}(corners.topRight) +
hash<decltype(corners.bottomRight)>{}(corners.bottomRight);
}
};
} // namespace std