From ea93553225e4a7c36cfb1dd27d991c7db95720eb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 11 May 2016 14:46:32 +0900 Subject: [PATCH] Make KeyWeakMap a template class --- atom/common/id_weak_map.cc | 63 ------------------------ atom/common/id_weak_map.h | 44 +---------------- atom/common/key_weak_map.h | 98 ++++++++++++++++++++++++++++++++++++++ filenames.gypi | 1 + 4 files changed, 101 insertions(+), 105 deletions(-) create mode 100644 atom/common/key_weak_map.h diff --git a/atom/common/id_weak_map.cc b/atom/common/id_weak_map.cc index c925f1f055c..9c14c99d495 100644 --- a/atom/common/id_weak_map.cc +++ b/atom/common/id_weak_map.cc @@ -4,71 +4,8 @@ #include "atom/common/id_weak_map.h" -#include - -#include "native_mate/converter.h" - namespace atom { -namespace { - -void OnObjectGC(const v8::WeakCallbackInfo& data) { - KeyWeakMap::KeyObject* key_object = data.GetParameter(); - key_object->self->Remove(key_object->key); -} - -} // namespace - -KeyWeakMap::KeyWeakMap() { -} - -KeyWeakMap::~KeyWeakMap() { - for (const auto& p : map_) - p.second.second->ClearWeak(); -} - -void KeyWeakMap::Set(v8::Isolate* isolate, - int32_t key, - v8::Local object) { - auto value = make_linked_ptr(new v8::Global(isolate, object)); - KeyObject key_object = {key, this}; - auto& p = map_[key] = std::make_pair(key_object, value); - value->SetWeak(&(p.first), OnObjectGC, v8::WeakCallbackType::kParameter); -} - -v8::MaybeLocal KeyWeakMap::Get(v8::Isolate* isolate, int32_t id) { - auto iter = map_.find(id); - if (iter == map_.end()) - return v8::MaybeLocal(); - else - return v8::Local::New(isolate, *(iter->second.second)); -} - -bool KeyWeakMap::Has(int32_t id) const { - return map_.find(id) != map_.end(); -} - -std::vector> KeyWeakMap::Values(v8::Isolate* isolate) { - std::vector> keys; - keys.reserve(map_.size()); - for (const auto& iter : map_) { - const auto& value = *(iter.second.second); - keys.emplace_back(v8::Local::New(isolate, value)); - } - return keys; -} - -void KeyWeakMap::Remove(int32_t id) { - auto iter = map_.find(id); - if (iter == map_.end()) { - LOG(WARNING) << "Removing unexist object with ID " << id; - return; - } - - iter->second.second->ClearWeak(); - map_.erase(iter); -} - IDWeakMap::IDWeakMap() : next_id_(0) { } diff --git a/atom/common/id_weak_map.h b/atom/common/id_weak_map.h index f2014b1cd7f..c5cf89b074c 100644 --- a/atom/common/id_weak_map.h +++ b/atom/common/id_weak_map.h @@ -5,52 +5,12 @@ #ifndef ATOM_COMMON_ID_WEAK_MAP_H_ #define ATOM_COMMON_ID_WEAK_MAP_H_ -#include -#include - -#include "base/memory/linked_ptr.h" -#include "v8/include/v8.h" +#include "atom/common/key_weak_map.h" namespace atom { -// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. -class KeyWeakMap { - public: - // Records the key and self, used by SetWeak. - struct KeyObject { - int32_t key; - KeyWeakMap* self; - }; - - KeyWeakMap(); - virtual ~KeyWeakMap(); - - // Sets the object to WeakMap with the given |id|. - void Set(v8::Isolate* isolate, int32_t id, v8::Local object); - - // Gets the object from WeakMap by its |id|. - v8::MaybeLocal Get(v8::Isolate* isolate, int32_t id); - - // Whethere there is an object with |id| in this WeakMap. - bool Has(int32_t id) const; - - // Returns all objects. - std::vector> Values(v8::Isolate* isolate); - - // Remove object with |id| in the WeakMap. - void Remove(int32_t key); - - private: - // Map of stored objects. - std::unordered_map< - int32_t, - std::pair>>> map_; - - DISALLOW_COPY_AND_ASSIGN(KeyWeakMap); -}; - // Provides key increments service in addition to KeyWeakMap. -class IDWeakMap : public KeyWeakMap { +class IDWeakMap : public KeyWeakMap { public: IDWeakMap(); ~IDWeakMap() override; diff --git a/atom/common/key_weak_map.h b/atom/common/key_weak_map.h new file mode 100644 index 00000000000..2d13c8170ad --- /dev/null +++ b/atom/common/key_weak_map.h @@ -0,0 +1,98 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_COMMON_KEY_WEAK_MAP_H_ +#define ATOM_COMMON_KEY_WEAK_MAP_H_ + +#include +#include +#include + +#include "base/memory/linked_ptr.h" +#include "v8/include/v8.h" + +namespace atom { + +namespace internal { + +} // namespace internal + +// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. +template +class KeyWeakMap { + public: + // Records the key and self, used by SetWeak. + struct KeyObject { + K key; + KeyWeakMap* self; + }; + + KeyWeakMap() {} + virtual ~KeyWeakMap() { + for (const auto& p : map_) + p.second.second->ClearWeak(); + } + + // Sets the object to WeakMap with the given |key|. + void Set(v8::Isolate* isolate, const K& key, v8::Local object) { + auto value = make_linked_ptr(new v8::Global(isolate, object)); + KeyObject key_object = {key, this}; + auto& p = map_[key] = std::make_pair(key_object, value); + value->SetWeak(&(p.first), OnObjectGC, v8::WeakCallbackType::kParameter); + } + + // Gets the object from WeakMap by its |key|. + v8::MaybeLocal Get(v8::Isolate* isolate, const K& key) { + auto iter = map_.find(key); + if (iter == map_.end()) + return v8::MaybeLocal(); + else + return v8::Local::New(isolate, *(iter->second.second)); + } + + // Whethere there is an object with |key| in this WeakMap. + bool Has(const K& key) const { + return map_.find(key) != map_.end(); + } + + // Returns all objects. + std::vector> Values(v8::Isolate* isolate) { + std::vector> keys; + keys.reserve(map_.size()); + for (const auto& iter : map_) { + const auto& value = *(iter.second.second); + keys.emplace_back(v8::Local::New(isolate, value)); + } + return keys; + } + + // Remove object with |key| in the WeakMap. + void Remove(const K& key) { + auto iter = map_.find(key); + if (iter == map_.end()) { + LOG(WARNING) << "Removing unexist object with ID " << key; + return; + } + + iter->second.second->ClearWeak(); + map_.erase(iter); + } + + private: + static void OnObjectGC( + const v8::WeakCallbackInfo::KeyObject>& data) { + KeyWeakMap::KeyObject* key_object = data.GetParameter(); + key_object->self->Remove(key_object->key); + } + + // Map of stored objects. + std::unordered_map< + K, std::pair>>> map_; + + DISALLOW_COPY_AND_ASSIGN(KeyWeakMap); +}; + +} // namespace atom + +#endif // ATOM_COMMON_KEY_WEAK_MAP_H_ diff --git a/filenames.gypi b/filenames.gypi index e4321bccc18..49832f43a58 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -340,6 +340,7 @@ 'atom/common/google_api_key.h', 'atom/common/id_weak_map.cc', 'atom/common/id_weak_map.h', + 'atom/common/key_weak_map.h', 'atom/common/keyboard_util.cc', 'atom/common/keyboard_util.h', 'atom/common/mouse_util.cc',