Support unique_ptr values in WaitFreeHashMap.

This commit is contained in:
levlam 2022-08-03 21:17:10 +03:00
parent b8d97997a6
commit 36693a3872

View File

@ -27,7 +27,7 @@ class WaitFreeHashMap {
};
unique_ptr<WaitFreeStorage> wait_free_storage_;
Storage &get_storage(const KeyT &key) {
const Storage &get_storage(const KeyT &key) const {
if (wait_free_storage_ == nullptr) {
return default_map_;
}
@ -35,6 +35,10 @@ class WaitFreeHashMap {
return wait_free_storage_->maps_[randomize_hash(HashT()(key)) & (MAX_STORAGE_COUNT - 1)];
}
Storage &get_storage(const KeyT &key) {
return const_cast<Storage &>(static_cast<const WaitFreeHashMap *>(this)->get_storage(key));
}
public:
void set(const KeyT &key, ValueT value) {
auto &storage = get_storage(key);
@ -49,8 +53,8 @@ class WaitFreeHashMap {
}
}
ValueT get(const KeyT &key) {
auto &storage = get_storage(key);
ValueT get(const KeyT &key) const {
const auto &storage = get_storage(key);
auto it = storage.find(key);
if (it == storage.end()) {
return {};
@ -58,6 +62,27 @@ class WaitFreeHashMap {
return it->second;
}
// specialization for WaitFreeHashMap<..., unique_ptr<T>>
template <typename ReturnT = decltype(ValueT().get())>
ReturnT get_pointer(const KeyT &key) {
auto &storage = get_storage(key);
auto it = storage.find(key);
if (it == storage.end()) {
return nullptr;
}
return it->second.get();
}
template <typename ReturnT = decltype(static_cast<const ValueT &>(ValueT()).get())>
ReturnT get_pointer(const KeyT &key) const {
auto &storage = get_storage(key);
auto it = storage.find(key);
if (it == storage.end()) {
return nullptr;
}
return it->second.get();
}
size_t erase(const KeyT &key) {
return get_storage(key).erase(key);
}