You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2017/11/20 03:47:15 UTC

[2/4] mesos git commit: Added support for enum's to hashset and hashmap.

Added support for enum's to hashset and hashmap.

Review: https://reviews.apache.org/r/63941


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/1a204ef1
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/1a204ef1
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/1a204ef1

Branch: refs/heads/master
Commit: 1a204ef14f742b762aaceb02045ac6ea37b0e3d0
Parents: e8d47e6
Author: Benjamin Hindman <be...@gmail.com>
Authored: Sun Aug 27 17:06:48 2017 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sun Nov 19 19:44:28 2017 -0800

----------------------------------------------------------------------
 3rdparty/stout/include/stout/hashmap.hpp |  7 ++++---
 3rdparty/stout/include/stout/hashset.hpp | 25 +++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/1a204ef1/3rdparty/stout/include/stout/hashmap.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/hashmap.hpp b/3rdparty/stout/include/stout/hashmap.hpp
index 539bbfd..faf8002 100644
--- a/3rdparty/stout/include/stout/hashmap.hpp
+++ b/3rdparty/stout/include/stout/hashmap.hpp
@@ -24,14 +24,15 @@
 #include "none.hpp"
 #include "option.hpp"
 
-
 // Provides a hash map via 'std::unordered_map'. We inherit from it to add
 // new functions as well as to provide better names for some of the
 // existing functions.
-
 template <typename Key,
           typename Value,
-          typename Hash = std::hash<Key>,
+          typename Hash = typename std::conditional<
+            std::is_enum<Key>::value,
+            EnumClassHash,
+            std::hash<Key>>::type,
           typename Equal = std::equal_to<Key>>
 class hashmap : public std::unordered_map<Key, Value, Hash, Equal>
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/1a204ef1/3rdparty/stout/include/stout/hashset.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/hashset.hpp b/3rdparty/stout/include/stout/hashset.hpp
index d2f7d5d..6af209c 100644
--- a/3rdparty/stout/include/stout/hashset.hpp
+++ b/3rdparty/stout/include/stout/hashset.hpp
@@ -21,13 +21,34 @@
 
 #include "foreach.hpp"
 
+// Prior to C++14 we can't use an enum type as the key to any
+// hash-based collection because of a defect in the standard. See
+// www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148 for more
+// details. The workaround for now is to use the following hash
+// function when using an enum.
+//
+// TODO(benh): Remove this once we move to C++14.
+struct EnumClassHash
+{
+  template <typename T>
+  std::size_t operator()(T t) const
+  {
+    static_assert(
+        sizeof(typename std::underlying_type<T>::type) <= sizeof(std::size_t),
+        "Expecting enum type to be convertible to std::size_t");
+    return static_cast<std::size_t>(t);
+  }
+};
+
 
 // Provides a hash set via 'std::unordered_set'. We inherit from it to add
 // new functions as well as to provide better naming for some of the
 // existing functions.
-
 template <typename Elem,
-          typename Hash = std::hash<Elem>,
+          typename Hash = typename std::conditional<
+            std::is_enum<Elem>::value,
+            EnumClassHash,
+            std::hash<Elem>>::type,
           typename Equal = std::equal_to<Elem>>
 class hashset : public std::unordered_set<Elem, Hash, Equal>
 {