Skip to content

Commit e62cf14

Browse files
WeakValueHashMap: add Hasher and Keyeq template parameters
1 parent 3474816 commit e62cf14

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Common/interface/WeakValueHashMap.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ namespace Diligent
4242

4343
/// \tparam KeyType - Type of the keys in the map. Must be hashable and comparable.
4444
/// \tparam ValueType - Type of the values in the map.
45+
/// \tparam Hasher - Hash function for the keys. Default is std::hash<KeyType>.
46+
/// \tparam Keyeq - Equality comparison function for the keys. Default is std::equal_to<KeyType>.
4547
///
4648
/// When a value is requested via GetOrInsert(), a strong pointer (shared_ptr) to the value is returned
4749
/// wrapped in a ValueHandle object. The ValueHandle object is responsible for removing the entry from the map
@@ -58,7 +60,10 @@ namespace Diligent
5860
/// auto Handle = Map.GetOrInsert(1, "Value");
5961
/// std::cout << *Handle << std::endl; // Outputs "Value"
6062
///
61-
template <typename KeyType, typename ValueType>
63+
template <typename KeyType,
64+
typename ValueType,
65+
typename Hasher = std::hash<KeyType>,
66+
typename Keyeq = std::equal_to<KeyType>>
6267
class WeakValueHashMap
6368
{
6469
private:
@@ -118,7 +123,7 @@ class WeakValueHashMap
118123
explicit operator bool() const { return m_pMap && m_pValue; }
119124

120125
private:
121-
friend class WeakValueHashMap<KeyType, ValueType>;
126+
friend class WeakValueHashMap<KeyType, ValueType, Hasher, Keyeq>;
122127

123128
void Release()
124129
{
@@ -248,8 +253,8 @@ class WeakValueHashMap
248253
}
249254

250255
private:
251-
std::mutex m_Mtx;
252-
std::unordered_map<KeyType, std::weak_ptr<ValueType>> m_Map;
256+
std::mutex m_Mtx;
257+
std::unordered_map<KeyType, std::weak_ptr<ValueType>, Hasher, Keyeq> m_Map;
253258
};
254259
std::shared_ptr<Impl> m_pImpl = std::make_shared<Impl>();
255260
};

0 commit comments

Comments
 (0)