From 79c1ad85f9b8a91269066294fb922dae4dcfd7c8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 11 May 2016 13:40:33 +0900 Subject: [PATCH] Add KeyWeakMap without Add method --- atom/common/id_weak_map.cc | 36 +++++++++++++++++++++--------------- atom/common/id_weak_map.h | 28 +++++++++++++++++++--------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/atom/common/id_weak_map.cc b/atom/common/id_weak_map.cc index f7c3f98ea3d3..2067585eb8f5 100644 --- a/atom/common/id_weak_map.cc +++ b/atom/common/id_weak_map.cc @@ -13,9 +13,9 @@ namespace atom { namespace { struct ObjectKey { - ObjectKey(int id, IDWeakMap* map) : id(id), map(map) {} + ObjectKey(int id, KeyWeakMap* map) : id(id), map(map) {} int id; - IDWeakMap* map; + KeyWeakMap* map; }; void OnObjectGC(const v8::WeakCallbackInfo& data) { @@ -26,13 +26,13 @@ void OnObjectGC(const v8::WeakCallbackInfo& data) { } // namespace -IDWeakMap::IDWeakMap() : next_id_(0) { +KeyWeakMap::KeyWeakMap() { } -IDWeakMap::~IDWeakMap() { +KeyWeakMap::~KeyWeakMap() { } -void IDWeakMap::Set(v8::Isolate* isolate, +void KeyWeakMap::Set(v8::Isolate* isolate, int32_t id, v8::Local object) { auto global = make_linked_ptr(new v8::Global(isolate, object)); @@ -41,13 +41,7 @@ void IDWeakMap::Set(v8::Isolate* isolate, map_[id] = global; } -int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local object) { - int32_t id = GetNextID(); - Set(isolate, id, object); - return id; -} - -v8::MaybeLocal IDWeakMap::Get(v8::Isolate* isolate, int32_t id) { +v8::MaybeLocal KeyWeakMap::Get(v8::Isolate* isolate, int32_t id) { auto iter = map_.find(id); if (iter == map_.end()) return v8::MaybeLocal(); @@ -55,11 +49,11 @@ v8::MaybeLocal IDWeakMap::Get(v8::Isolate* isolate, int32_t id) { return v8::Local::New(isolate, *iter->second); } -bool IDWeakMap::Has(int32_t id) const { +bool KeyWeakMap::Has(int32_t id) const { return map_.find(id) != map_.end(); } -std::vector> IDWeakMap::Values(v8::Isolate* isolate) { +std::vector> KeyWeakMap::Values(v8::Isolate* isolate) { std::vector> keys; keys.reserve(map_.size()); for (const auto& iter : map_) @@ -67,7 +61,7 @@ std::vector> IDWeakMap::Values(v8::Isolate* isolate) { return keys; } -void IDWeakMap::Remove(int32_t id) { +void KeyWeakMap::Remove(int32_t id) { auto iter = map_.find(id); if (iter == map_.end()) LOG(WARNING) << "Removing unexist object with ID " << id; @@ -75,6 +69,18 @@ void IDWeakMap::Remove(int32_t id) { map_.erase(iter); } +IDWeakMap::IDWeakMap() : next_id_(0) { +} + +IDWeakMap::~IDWeakMap() { +} + +int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local object) { + int32_t id = GetNextID(); + Set(isolate, id, object); + return id; +} + int32_t IDWeakMap::GetNextID() { return ++next_id_; } diff --git a/atom/common/id_weak_map.h b/atom/common/id_weak_map.h index f92270573fa6..a3c49b2346c7 100644 --- a/atom/common/id_weak_map.h +++ b/atom/common/id_weak_map.h @@ -14,17 +14,14 @@ namespace atom { // Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. -class IDWeakMap { +class KeyWeakMap { public: - IDWeakMap(); - ~IDWeakMap(); + KeyWeakMap(); + virtual ~KeyWeakMap(); // Sets the object to WeakMap with the given |id|. void Set(v8::Isolate* isolate, int32_t id, v8::Local object); - // Adds |object| to WeakMap and returns its allocated |id|. - int32_t Add(v8::Isolate* isolate, v8::Local object); - // Gets the object from WeakMap by its |id|. v8::MaybeLocal Get(v8::Isolate* isolate, int32_t id); @@ -37,6 +34,22 @@ class IDWeakMap { // Remove object with |id| in the WeakMap. void Remove(int32_t key); + private: + // Map of stored objects. + std::unordered_map>> map_; + + DISALLOW_COPY_AND_ASSIGN(KeyWeakMap); +}; + +// Provides key increments service in addition to KeyWeakMap. +class IDWeakMap : public KeyWeakMap { + public: + IDWeakMap(); + ~IDWeakMap() override; + + // Adds |object| to WeakMap and returns its allocated |id|. + int32_t Add(v8::Isolate* isolate, v8::Local object); + private: // Returns next available ID. int32_t GetNextID(); @@ -44,9 +57,6 @@ class IDWeakMap { // ID of next stored object. int32_t next_id_; - // Map of stored objects. - std::unordered_map>> map_; - DISALLOW_COPY_AND_ASSIGN(IDWeakMap); };