diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-09-12 11:12:37 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-09-12 11:12:37 +0900 |
commit | 944653463fe4134c82d85e2d01e2bc0fa43fd727 (patch) | |
tree | c035e1b2a0b7b5491cbd21d2313e2157066482d4 | |
parent | 473686bf953aa8726c7b747935d260be5d9f8ba1 (diff) | |
download | libquotient-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.
-rw-r--r-- | lib/connection.cpp | 2 | ||||
-rw-r--r-- | lib/connection.h | 2 | ||||
-rw-r--r-- | lib/converters.h | 14 | ||||
-rw-r--r-- | lib/events/stateevent.h | 10 | ||||
-rw-r--r-- | lib/room.cpp | 4 | ||||
-rw-r--r-- | lib/util.h | 14 |
6 files changed, 22 insertions, 24 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index 5ebdcf6c..c3e46356 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -98,7 +98,7 @@ public: // See https://github.com/quotient-im/libQuotient/wiki/Handling-direct-chat-events DirectChatsMap dcLocalAdditions; DirectChatsMap dcLocalRemovals; - std::unordered_map<QString, EventPtr> accountData; + UnorderedMap<QString, EventPtr> accountData; int syncLoopTimeout = -1; GetCapabilitiesJob* capabilitiesJob = nullptr; diff --git a/lib/connection.h b/lib/connection.h index 7e32e5c9..b4b16679 100644 --- a/lib/connection.h +++ b/lib/connection.h @@ -124,7 +124,7 @@ public: using IgnoredUsersList = IgnoredUsersEvent::content_type; using UsersToDevicesToEvents = - std::unordered_map<QString, std::unordered_map<QString, const Event&>>; + UnorderedMap<QString, UnorderedMap<QString, const Event&>>; enum RoomVisibility { PublishRoom, diff --git a/lib/converters.h b/lib/converters.h index 587e4544..b753a80b 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -28,16 +28,10 @@ #include <QtCore/QUrlQuery> #include <QtCore/QVector> -#include <unordered_map> #include <vector> -#if 0 // Waiting for C++17 -# include <experimental/optional> - -template <typename T> -using optional = std::experimental::optional<T>; -#endif // Enable std::unordered_map<QString, T> +// REMOVEME in favor of UnorderedMap, once we regenerate API files namespace std { template <> struct hash<QString> { @@ -296,9 +290,9 @@ struct HashMapFromJson { } }; -template <typename T> -struct JsonObjectConverter<std::unordered_map<QString, T>> - : public HashMapFromJson<std::unordered_map<QString, T>> {}; +template <typename T, typename HashT> +struct JsonObjectConverter<std::unordered_map<QString, T, HashT>> + : public HashMapFromJson<std::unordered_map<QString, T, HashT>> {}; template <typename T> struct JsonObjectConverter<QHash<QString, T>> diff --git a/lib/events/stateevent.h b/lib/events/stateevent.h index 74e36e74..710b4271 100644 --- a/lib/events/stateevent.h +++ b/lib/events/stateevent.h @@ -129,13 +129,3 @@ private: std::unique_ptr<Prev<ContentT>> _prev; }; } // namespace Quotient - -namespace std { -template <> -struct hash<Quotient::StateEventKey> { - size_t operator()(const Quotient::StateEventKey& k) const Q_DECL_NOEXCEPT - { - return qHash(k); - } -}; -} // namespace std diff --git a/lib/room.cpp b/lib/room.cpp index 2c9fca63..2f697589 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -98,7 +98,7 @@ public: RoomSummary summary = { none, 0, none }; /// The state of the room at timeline position before-0 /// \sa timelineBase - std::unordered_map<StateEventKey, StateEventPtr> baseState; + UnorderedMap<StateEventKey, StateEventPtr> baseState; /// State event stubs - events without content, just type and state key static decltype(baseState) stubbedState; /// The state of the room at timeline position after-maxTimelineIndex() @@ -131,7 +131,7 @@ public: QHash<const User*, QString> lastReadEventIds; QString serverReadMarker; TagsMap tags; - std::unordered_map<QString, EventPtr> accountData; + UnorderedMap<QString, EventPtr> accountData; QString prevBatch; QPointer<GetRoomEventsJob> eventsHistoryJob; QPointer<GetMembersByRoomJob> allMembersJob; @@ -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 {}; |