From 7cea9929264a8fcd569a55a69aa0bfe785be67ab Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 26 Aug 2024 13:19:05 -0500 Subject: [PATCH] refactor: simplify KeyWeakMap impl (#43461) --- shell/common/key_weak_map.h | 48 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/shell/common/key_weak_map.h b/shell/common/key_weak_map.h index cb8b68a4f2bc..b064ed627d1f 100644 --- a/shell/common/key_weak_map.h +++ b/shell/common/key_weak_map.h @@ -6,9 +6,9 @@ #define ELECTRON_SHELL_COMMON_KEY_WEAK_MAP_H_ #include -#include #include +#include "base/containers/to_vector.h" #include "base/memory/raw_ptr.h" #include "v8/include/v8-forward.h" @@ -18,10 +18,10 @@ namespace electron { template class KeyWeakMap { public: - KeyWeakMap() {} + KeyWeakMap() = default; ~KeyWeakMap() { for (auto& p : map_) - p.second.second.ClearWeak(); + p.second.value.ClearWeak(); } // disable copy @@ -29,55 +29,47 @@ class KeyWeakMap { KeyWeakMap& operator=(const KeyWeakMap&) = delete; // Sets the object to WeakMap with the given |key|. - void Set(v8::Isolate* isolate, const K& key, v8::Local object) { - KeyObject key_object = {key, this}; - auto& p = map_[key] = - std::make_pair(key_object, v8::Global(isolate, object)); - p.second.SetWeak(&(p.first), OnObjectGC, v8::WeakCallbackType::kParameter); + void Set(v8::Isolate* isolate, const K& key, v8::Local value) { + auto& mapped = map_[key] = + Mapped{this, key, v8::Global(isolate, value)}; + mapped.value.SetWeak(&mapped, OnObjectGC, v8::WeakCallbackType::kParameter); } // Gets the object from WeakMap by its |key|. v8::MaybeLocal Get(v8::Isolate* isolate, const K& key) { if (auto iter = map_.find(key); iter != map_.end()) - return v8::Local::New(isolate, iter->second.second); + return v8::Local::New(isolate, iter->second.value); return {}; } // Returns all objects. std::vector> Values(v8::Isolate* isolate) const { - std::vector> values; - values.reserve(map_.size()); - for (const auto& it : map_) - values.emplace_back( - v8::Local::New(isolate, it.second.second)); - return values; + return base::ToVector(map_, [isolate](const auto& iter) { + return v8::Local::New(isolate, iter.second.value); + }); } // Remove object with |key| in the WeakMap. void Remove(const K& key) { - auto iter = map_.find(key); - if (iter == map_.end()) - return; - - iter->second.second.ClearWeak(); - map_.erase(iter); + if (auto item = map_.extract(key)) + item.mapped().value.ClearWeak(); } private: // Records the key and self, used by SetWeak. - struct KeyObject { - K key; + struct Mapped { raw_ptr self; + K key; + v8::Global value; }; - static void OnObjectGC( - const v8::WeakCallbackInfo::KeyObject>& data) { - KeyWeakMap::KeyObject* key_object = data.GetParameter(); - key_object->self->Remove(key_object->key); + static void OnObjectGC(const v8::WeakCallbackInfo& data) { + auto* mapped = data.GetParameter(); + mapped->self->Remove(mapped->key); } // Map of stored objects. - std::unordered_map>> map_; + std::unordered_map map_; }; } // namespace electron