Commit 254bd40b authored by davidben's avatar davidben Committed by Commit bot

Align base::hash_map with C++11, part 1.

C++11's std::hash provides a default hash for T* that hashes by pointer value.
So does MSVC's stdext::hash_value, but not GCC's __gnu_cxx::hash (and STLPort's
std::hash). To align non-MSVC with both MSVC and the standard, provide a
default hash for pointers. This removes a lot of GCC-specific hash definitions.

Confusingly, all three provide a default hash for const char * that hashes as C
string, while not changing their equality. This CL does not change this on any
platform.

BUG=420242

Review URL: https://codereview.chromium.org/630503002

Cr-Commit-Position: refs/heads/master@{#299217}
parent 45097b20
...@@ -85,6 +85,17 @@ DEFINE_TRIVIAL_HASH(unsigned long long); ...@@ -85,6 +85,17 @@ DEFINE_TRIVIAL_HASH(unsigned long long);
#undef DEFINE_TRIVIAL_HASH #undef DEFINE_TRIVIAL_HASH
#endif // !defined(OS_ANDROID) #endif // !defined(OS_ANDROID)
// To align with C++11's std::hash and MSVC's pre-standard stdext::hash_value,
// provide a default hash function for raw pointers. Note: const char * is still
// specialized to hash as a C string. This is consistent with the currently used
// stdext::hash_value, but not C++11.
template<typename T>
struct hash<T*> {
std::size_t operator()(T* value) const {
return hash<uintptr_t>()(reinterpret_cast<uintptr_t>(value));
}
};
// Implement string hash functions so that strings of various flavors can // Implement string hash functions so that strings of various flavors can
// be used as keys in STL maps and sets. The hash algorithm comes from the // be used as keys in STL maps and sets. The hash algorithm comes from the
// GNU C++ library, in <tr1/functional>. It is duplicated here because GCC // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
......
...@@ -45,17 +45,6 @@ ...@@ -45,17 +45,6 @@
template <typename Type> template <typename Type>
struct DefaultSingletonTraits; struct DefaultSingletonTraits;
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<base::MessageLoop*> {
std::size_t operator()(base::MessageLoop* value) const {
return reinterpret_cast<std::size_t>(value);
}
};
} // BASE_HASH_NAMESPACE
#endif
namespace base { namespace base {
class WaitableEvent; class WaitableEvent;
......
...@@ -38,18 +38,6 @@ class DiscardableMemoryManagerAllocation { ...@@ -38,18 +38,6 @@ class DiscardableMemoryManagerAllocation {
} // namespace internal } // namespace internal
} // namespace base } // namespace base
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<base::internal::DiscardableMemoryManagerAllocation*> {
size_t operator()(
base::internal::DiscardableMemoryManagerAllocation* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
namespace base { namespace base {
namespace internal { namespace internal {
......
...@@ -19,16 +19,6 @@ ...@@ -19,16 +19,6 @@
#include "cc/trees/proxy.h" #include "cc/trees/proxy.h"
#include "ui/gfx/size.h" #include "ui/gfx/size.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <> struct hash<cc::PrioritizedResource*> {
size_t operator()(cc::PrioritizedResource* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
namespace cc { namespace cc {
class PriorityCalculator; class PriorityCalculator;
......
...@@ -16,19 +16,6 @@ ...@@ -16,19 +16,6 @@
#include "ui/gfx/rect_f.h" #include "ui/gfx/rect_f.h"
#include "ui/gfx/vector3d_f.h" #include "ui/gfx/vector3d_f.h"
#if defined(COMPILER_GCC)
namespace cc { struct GraphEdge; }
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<cc::GraphEdge*> {
size_t operator()(cc::GraphEdge* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
namespace gfx { namespace gfx {
class Transform; class Transform;
} }
......
...@@ -18,17 +18,6 @@ ...@@ -18,17 +18,6 @@
#include "cc/output/renderer.h" #include "cc/output/renderer.h"
#include "cc/resources/ui_resource_client.h" #include "cc/resources/ui_resource_client.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<cc::LayerImpl*> {
size_t operator()(cc::LayerImpl* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
namespace base { namespace base {
namespace debug { namespace debug {
class TracedValue; class TracedValue;
......
...@@ -34,17 +34,6 @@ namespace user_prefs { ...@@ -34,17 +34,6 @@ namespace user_prefs {
class PrefRegistrySyncable; class PrefRegistrySyncable;
} }
#if defined(COMPILER_GCC) && defined(ENABLE_EXTENSIONS)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<extensions::CrxInstaller*> {
std::size_t operator()(extensions::CrxInstaller* const& p) const {
return reinterpret_cast<std::size_t>(p);
}
};
} // namespace BASE_HASH_NAMESPACE
#endif
// This is the Chrome side helper for the download system. // This is the Chrome side helper for the download system.
class ChromeDownloadManagerDelegate class ChromeDownloadManagerDelegate
: public content::DownloadManagerDelegate, : public content::DownloadManagerDelegate,
......
...@@ -407,17 +407,4 @@ struct ProfileCompare { ...@@ -407,17 +407,4 @@ struct ProfileCompare {
bool operator()(Profile* a, Profile* b) const; bool operator()(Profile* a, Profile* b) const;
}; };
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<Profile*> {
std::size_t operator()(Profile* const& p) const {
return reinterpret_cast<std::size_t>(p);
}
};
} // namespace BASE_HASH_NAMESPACE
#endif
#endif // CHROME_BROWSER_PROFILES_PROFILE_H_ #endif // CHROME_BROWSER_PROFILES_PROFILE_H_
...@@ -36,19 +36,6 @@ class URLFetcher; ...@@ -36,19 +36,6 @@ class URLFetcher;
class URLRequestContextGetter; class URLRequestContextGetter;
} // namespace net } // namespace net
#if defined(COMPILER_GCC)
// Allows us to use URLFetchers in a hash_map with gcc (MSVC is okay without
// specifying this).
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<const net::URLFetcher*> {
size_t operator()(const net::URLFetcher* fetcher) const {
return reinterpret_cast<size_t>(fetcher);
}
};
}
#endif
class SBProtocolManagerFactory; class SBProtocolManagerFactory;
class SafeBrowsingProtocolManagerDelegate; class SafeBrowsingProtocolManagerDelegate;
......
...@@ -9,17 +9,6 @@ ...@@ -9,17 +9,6 @@
#include "base/id_map.h" #include "base/id_map.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<content::RenderFrameHost*> {
uint64 operator()(content::RenderFrameHost* ptr) const {
return hash<uint64>()(reinterpret_cast<uint64>(ptr));
}
};
}
#endif
namespace content { namespace content {
class RenderFrameHost; class RenderFrameHost;
......
...@@ -21,35 +21,6 @@ namespace base { ...@@ -21,35 +21,6 @@ namespace base {
class MessageLoopProxy; class MessageLoopProxy;
} }
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<NPObject*> {
std::size_t operator()(NPObject* const& ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
template<>
struct hash<struct _NPP*> {
std::size_t operator()(struct _NPP* const& ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace __gnu_cxx
#elif defined(COMPILER_MSVC)
namespace stdext {
template<>
inline size_t hash_value(NPObject* const& ptr) {
return hash_value(reinterpret_cast<size_t>(ptr));
}
} // namespace stdext
#endif // COMPILER
namespace content { namespace content {
// Encapsulates an IPC channel between a renderer and another process. Used to // Encapsulates an IPC channel between a renderer and another process. Used to
......
...@@ -12,17 +12,6 @@ ...@@ -12,17 +12,6 @@
using gpu::MemoryAllocation; using gpu::MemoryAllocation;
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<content::GpuMemoryManagerClient*> {
uint64 operator()(content::GpuMemoryManagerClient* ptr) const {
return hash<uint64>()(reinterpret_cast<uint64>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
class FakeMemoryTracker : public gpu::gles2::MemoryTracker { class FakeMemoryTracker : public gpu::gles2::MemoryTracker {
public: public:
virtual void TrackMemoryAllocatedChange( virtual void TrackMemoryAllocatedChange(
......
...@@ -168,17 +168,4 @@ class CONTENT_EXPORT BrowserContext : public base::SupportsUserData { ...@@ -168,17 +168,4 @@ class CONTENT_EXPORT BrowserContext : public base::SupportsUserData {
} // namespace content } // namespace content
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<content::BrowserContext*> {
std::size_t operator()(content::BrowserContext* const& p) const {
return reinterpret_cast<std::size_t>(p);
}
};
} // namespace BASE_HASH_NAMESPACE
#endif
#endif // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_ #endif // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
...@@ -15,17 +15,6 @@ ...@@ -15,17 +15,6 @@
#include "gpu/command_buffer/service/texture_manager.h" #include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/gpu_export.h" #include "gpu/gpu_export.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<gpu::gles2::TextureRef*> {
size_t operator()(gpu::gles2::TextureRef* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
namespace gfx { namespace gfx {
class GLContext; class GLContext;
} }
......
...@@ -10,17 +10,6 @@ ...@@ -10,17 +10,6 @@
#include "base/containers/hash_tables.h" #include "base/containers/hash_tables.h"
#include "net/quic/quic_protocol.h" #include "net/quic/quic_protocol.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<net::QuicAckNotifier*> {
std::size_t operator()(const net::QuicAckNotifier* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
}
#endif
namespace net { namespace net {
class QuicAckNotifier; class QuicAckNotifier;
......
...@@ -315,20 +315,4 @@ class Target : public Item { ...@@ -315,20 +315,4 @@ class Target : public Item {
DISALLOW_COPY_AND_ASSIGN(Target); DISALLOW_COPY_AND_ASSIGN(Target);
}; };
namespace BASE_HASH_NAMESPACE {
#if defined(COMPILER_GCC)
template<> struct hash<const Target*> {
std::size_t operator()(const Target* t) const {
return reinterpret_cast<std::size_t>(t);
}
};
#elif defined(COMPILER_MSVC)
inline size_t hash_value(const Target* t) {
return reinterpret_cast<size_t>(t);
}
#endif // COMPILER...
} // namespace BASE_HASH_NAMESPACE
#endif // TOOLS_GN_TARGET_H_ #endif // TOOLS_GN_TARGET_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment