Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: mojo/public/cpp/bindings/lib/hash_util.h

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Address all review comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_
7
8 #include <functional>
9 #include <type_traits>
10
11 #include "mojo/public/cpp/bindings/lib/template_util.h"
12
13 namespace mojo {
14 namespace internal {
15
16 template <typename T>
17 size_t HashCombine(size_t seed, const T& value) {
18 // Based on proposal in:
19 // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf
20 return seed ^ (std::hash<T>()(value) + (seed << 6) + (seed >> 2));
21 }
22
23 template <typename T>
24 struct HasHashMethod {
25 template <typename U>
26 static char Test(decltype(&U::Hash));
27 template <typename U>
28 static int Test(...);
29 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
30
31 private:
32 EnsureTypeIsComplete<T> check_t_;
33 };
34
35 template <typename T, bool has_hash_method = HasHashMethod<T>::value>
36 struct HashTraits;
37
38 template <typename T>
39 size_t Hash(size_t seed, const T& value);
40
41 template <typename T>
42 struct HashTraits<T, true> {
43 static size_t Hash(size_t seed, const T& value) { return value.Hash(seed); }
44 };
45
46 template <typename T>
47 struct HashTraits<T, false> {
48 static size_t Hash(size_t seed, const T& value) {
49 return HashCombine(seed, value);
50 }
51 };
52
53 template <typename T>
54 size_t Hash(size_t seed, const T& value) {
55 return HashTraits<T>::Hash(seed, value);
56 }
57
58 } // namespace internal
59 } // namespace mojo
60
61 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698