From: Andreas C. Osowski Date: Fri, 17 Mar 2017 00:35:15 +0000 (-0700) Subject: Add hash and equal_to implementations to folly::Uri X-Git-Tag: v2017.03.20.00~3 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=cb969f7ac49162965bdcbfadd2b2d4b8ce71a7c4;hp=ebebe68b72029bd6321dabf986baa18b206328c1 Add hash and equal_to implementations to folly::Uri Summary: Adds a default inline implementation for `std::hash` and `std::equal_to` to `folly::Uri`. Both do the comparison / hash based upon the return value of `folly::Uri::toString`. Reviewed By: yfeldblum Differential Revision: D4711506 fbshipit-source-id: f4c2a9de8d4302fd315a9f31329cc8ba9f5e0409 --- diff --git a/folly/Uri-inl.h b/folly/Uri-inl.h index 5cb1967c..86a63a0c 100644 --- a/folly/Uri-inl.h +++ b/folly/Uri-inl.h @@ -18,10 +18,40 @@ #error This file may only be included from folly/Uri.h #endif +#include +#include + #include +#include namespace folly { +namespace uri_detail { + +using UriTuple = std::tuple< + const fbstring&, + const fbstring&, + const fbstring&, + const fbstring&, + uint16_t, + const fbstring&, + const fbstring&, + const fbstring&>; + +inline UriTuple as_tuple(const folly::Uri& k) { + return UriTuple( + k.scheme(), + k.username(), + k.password(), + k.host(), + k.port(), + k.path(), + k.query(), + k.fragment()); +} + +} // namespace uri_detail + template String Uri::toString() const { String str; @@ -49,4 +79,23 @@ String Uri::toString() const { return str; } -} // namespace folly +} // namespace folly + +namespace std { + +template <> +struct hash { + std::size_t operator()(const folly::Uri& k) const { + return std::hash{}( + folly::uri_detail::as_tuple(k)); + } +}; + +template <> +struct equal_to { + bool operator()(const folly::Uri& a, const folly::Uri& b) const { + return folly::uri_detail::as_tuple(a) == folly::uri_detail::as_tuple(b); + } +}; + +} // namespace std