aboutsummaryrefslogtreecommitdiff
path: root/lib/util.h
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2019-09-12 11:12:37 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2019-09-12 11:12:37 +0900
commit944653463fe4134c82d85e2d01e2bc0fa43fd727 (patch)
treec035e1b2a0b7b5491cbd21d2313e2157066482d4 /lib/util.h
parent473686bf953aa8726c7b747935d260be5d9f8ba1 (diff)
downloadlibquotient-944653463fe4134c82d85e2d01e2bc0fa43fd727.tar.gz
libquotient-944653463fe4134c82d85e2d01e2bc0fa43fd727.zip
Introduce HashQ<> and UnorderedMap<>
Invading into std:: is frowned upon, even though legitimate from the C++ standard perspective. Given that it's possible to pass a hash object to unordered_map, it only takes an alias for std::unordered_map to avoid having to specialize std::hash. And besides, a generic compatibility bridge between qHash and std::hash has been long needed. std::hash<QString> in converters.h remains for now; it will be dropped separately when the API files get regenerated to use UnorderedMap.
Diffstat (limited to 'lib/util.h')
-rw-r--r--lib/util.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/util.h b/lib/util.h
index 7c79804b..788ce5bc 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -19,9 +19,11 @@
#pragma once
#include <QtCore/QLatin1String>
+#include <QtCore/QHashFunctions>
#include <functional>
#include <memory>
+#include <unordered_map>
// Along the lines of Q_DISABLE_COPY - the upstream version comes in Qt 5.13
#define DISABLE_MOVE(_ClassName) \
@@ -29,6 +31,18 @@
_ClassName& operator=(_ClassName&&) Q_DECL_EQ_DELETE;
namespace Quotient {
+/// An equivalent of std::hash for QTypes to enable std::unordered_map<QType, ...>
+template <typename T>
+struct HashQ {
+ size_t operator()(const T& s) const Q_DECL_NOEXCEPT
+ {
+ return qHash(s, uint(qGlobalQHashSeed()));
+ }
+};
+/// A wrapper around std::unordered_map compatible with types that have qHash
+template <typename KeyT, typename ValT>
+using UnorderedMap = std::unordered_map<KeyT, ValT, HashQ<KeyT>>;
+
struct NoneTag {};
constexpr NoneTag none {};