Automated rollback of commit 888810af33.

PiperOrigin-RevId: 504428590
pull/11641/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 888810af33
commit 5bbc6fc6aa
  1. 1
      src/google/protobuf/BUILD.bazel
  2. 31
      src/google/protobuf/map.h
  3. 4
      src/google/protobuf/map_test.inc

@ -337,7 +337,6 @@ cc_library(
"//src/google/protobuf/stubs:lite", "//src/google/protobuf/stubs:lite",
"@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/meta:type_traits", "@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/numeric:bits", "@com_google_absl//absl/numeric:bits",
"@com_google_absl//absl/strings:internal", "@com_google_absl//absl/strings:internal",

@ -46,15 +46,17 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#if defined(__cpp_lib_string_view)
#include <string_view>
#endif // defined(__cpp_lib_string_view)
#if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__) #if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__)
#include <mach/mach_time.h> #include <mach/mach_time.h>
#endif #endif
#include "google/protobuf/stubs/common.h" #include "google/protobuf/stubs/common.h"
#include "absl/container/btree_map.h" #include "absl/container/btree_map.h"
#include "absl/hash/hash.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/arena.h" #include "google/protobuf/arena.h"
#include "google/protobuf/generated_enum_util.h" #include "google/protobuf/generated_enum_util.h"
#include "google/protobuf/map_type_handler.h" #include "google/protobuf/map_type_handler.h"
@ -227,31 +229,33 @@ struct TransparentSupport {
using key_arg = key_type; using key_arg = key_type;
}; };
// We add transparent support for std::string keys. We use #if defined(__cpp_lib_string_view)
// std::hash<absl::string_view> as it supports the input types we care about. // If std::string_view is available, we add transparent support for std::string
// The lookup functions accept arbitrary `K`. This will include any key type // keys. We use std::hash<std::string_view> as it supports the input types we
// that is convertible to absl::string_view. // care about. The lookup functions accept arbitrary `K`. This will include any
// key type that is convertible to std::string_view.
template <> template <>
struct TransparentSupport<std::string> { struct TransparentSupport<std::string> {
static absl::string_view ImplicitConvert(absl::string_view str) { static std::string_view ImplicitConvert(std::string_view str) { return str; }
return str; // If the element is not convertible to std::string_view, try to convert to
}
// If the element is not convertible to absl::string_view, try to convert to
// std::string first. // std::string first.
// The template makes this overload lose resolution when both have the same // The template makes this overload lose resolution when both have the same
// rank otherwise. // rank otherwise.
template <typename = void> template <typename = void>
static absl::string_view ImplicitConvert(const std::string& str) { static std::string_view ImplicitConvert(const std::string& str) {
return str; return str;
} }
struct hash : public absl::Hash<absl::string_view> { struct hash : private std::hash<std::string_view> {
using is_transparent = void; using is_transparent = void;
template <typename T> template <typename T>
size_t operator()(const T& str) const { size_t operator()(const T& str) const {
return absl::Hash<absl::string_view>::operator()(ImplicitConvert(str)); return base()(ImplicitConvert(str));
} }
private:
const std::hash<std::string_view>& base() const { return *this; }
}; };
struct less { struct less {
using is_transparent = void; using is_transparent = void;
@ -270,6 +274,7 @@ struct TransparentSupport<std::string> {
template <typename K> template <typename K>
using key_arg = K; using key_arg = K;
}; };
#endif // defined(__cpp_lib_string_view)
struct NodeBase { struct NodeBase {
// Align the node to allow KeyNode to predict the location of the key. // Align the node to allow KeyNode to predict the location of the key.

@ -1404,7 +1404,9 @@ void TestTransparent(const Key& key, const Key& miss_key) {
TEST_F(MapImplTest, TransparentLookupForString) { TEST_F(MapImplTest, TransparentLookupForString) {
TestTransparent("ABC", "LKJ"); TestTransparent("ABC", "LKJ");
TestTransparent(std::string("ABC"), std::string("LKJ")); TestTransparent(std::string("ABC"), std::string("LKJ"));
TestTransparent(absl::string_view("ABC"), absl::string_view("LKJ")); #if defined(__cpp_lib_string_view)
TestTransparent(std::string_view("ABC"), std::string_view("LKJ"));
#endif // defined(__cpp_lib_string_view)
// std::reference_wrapper // std::reference_wrapper
std::string abc = "ABC", lkj = "LKJ"; std::string abc = "ABC", lkj = "LKJ";

Loading…
Cancel
Save