types: Add STL-compliant Hash functor for Qt types
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 15 Oct 2018 19:22:04 +0000 (21:22 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
In order to use Qt types with std::unordered_set (and map), a
compatible hash function needs to be provided. Provide a generic
Hash<T> functor that just uses the qHash() function for the given
type.

This can be used as follows:
    std::unordered_set<QString, Hash<QString>> set;

src/common/types.h

index a806c58..0816223 100644 (file)
@@ -225,6 +225,16 @@ QDataStream& operator>>(QDataStream& in, T& value)
     return in;
 }
 
     return in;
 }
 
+// STL-compliant hash functor for Qt types
+template<typename T>
+struct Hash
+{
+    uint operator()(const T& t) const
+    {
+        return qHash(t);
+    }
+};
+
 // Exceptions
 
 /**
 // Exceptions
 
 /**