Make KeyWeakMap a template class
This commit is contained in:
parent
e6c0b1fe0c
commit
ea93553225
4 changed files with 101 additions and 105 deletions
|
@ -4,71 +4,8 @@
|
||||||
|
|
||||||
#include "atom/common/id_weak_map.h"
|
#include "atom/common/id_weak_map.h"
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "native_mate/converter.h"
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
void OnObjectGC(const v8::WeakCallbackInfo<KeyWeakMap::KeyObject>& 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<v8::Object> object) {
|
|
||||||
auto value = make_linked_ptr(new v8::Global<v8::Object>(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<v8::Object> KeyWeakMap::Get(v8::Isolate* isolate, int32_t id) {
|
|
||||||
auto iter = map_.find(id);
|
|
||||||
if (iter == map_.end())
|
|
||||||
return v8::MaybeLocal<v8::Object>();
|
|
||||||
else
|
|
||||||
return v8::Local<v8::Object>::New(isolate, *(iter->second.second));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool KeyWeakMap::Has(int32_t id) const {
|
|
||||||
return map_.find(id) != map_.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<v8::Local<v8::Object>> KeyWeakMap::Values(v8::Isolate* isolate) {
|
|
||||||
std::vector<v8::Local<v8::Object>> keys;
|
|
||||||
keys.reserve(map_.size());
|
|
||||||
for (const auto& iter : map_) {
|
|
||||||
const auto& value = *(iter.second.second);
|
|
||||||
keys.emplace_back(v8::Local<v8::Object>::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) {
|
IDWeakMap::IDWeakMap() : next_id_(0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,52 +5,12 @@
|
||||||
#ifndef ATOM_COMMON_ID_WEAK_MAP_H_
|
#ifndef ATOM_COMMON_ID_WEAK_MAP_H_
|
||||||
#define ATOM_COMMON_ID_WEAK_MAP_H_
|
#define ATOM_COMMON_ID_WEAK_MAP_H_
|
||||||
|
|
||||||
#include <unordered_map>
|
#include "atom/common/key_weak_map.h"
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "base/memory/linked_ptr.h"
|
|
||||||
#include "v8/include/v8.h"
|
|
||||||
|
|
||||||
namespace atom {
|
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<v8::Object> object);
|
|
||||||
|
|
||||||
// Gets the object from WeakMap by its |id|.
|
|
||||||
v8::MaybeLocal<v8::Object> 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<v8::Local<v8::Object>> 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<KeyObject, linked_ptr<v8::Global<v8::Object>>>> map_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(KeyWeakMap);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Provides key increments service in addition to KeyWeakMap.
|
// Provides key increments service in addition to KeyWeakMap.
|
||||||
class IDWeakMap : public KeyWeakMap {
|
class IDWeakMap : public KeyWeakMap<int32_t> {
|
||||||
public:
|
public:
|
||||||
IDWeakMap();
|
IDWeakMap();
|
||||||
~IDWeakMap() override;
|
~IDWeakMap() override;
|
||||||
|
|
98
atom/common/key_weak_map.h
Normal file
98
atom/common/key_weak_map.h
Normal file
|
@ -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 <unordered_map>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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<typename K>
|
||||||
|
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<v8::Object> object) {
|
||||||
|
auto value = make_linked_ptr(new v8::Global<v8::Object>(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<v8::Object> Get(v8::Isolate* isolate, const K& key) {
|
||||||
|
auto iter = map_.find(key);
|
||||||
|
if (iter == map_.end())
|
||||||
|
return v8::MaybeLocal<v8::Object>();
|
||||||
|
else
|
||||||
|
return v8::Local<v8::Object>::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<v8::Local<v8::Object>> Values(v8::Isolate* isolate) {
|
||||||
|
std::vector<v8::Local<v8::Object>> keys;
|
||||||
|
keys.reserve(map_.size());
|
||||||
|
for (const auto& iter : map_) {
|
||||||
|
const auto& value = *(iter.second.second);
|
||||||
|
keys.emplace_back(v8::Local<v8::Object>::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<KeyWeakMap<K>::KeyObject>& data) {
|
||||||
|
KeyWeakMap<K>::KeyObject* key_object = data.GetParameter();
|
||||||
|
key_object->self->Remove(key_object->key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map of stored objects.
|
||||||
|
std::unordered_map<
|
||||||
|
K, std::pair<KeyObject, linked_ptr<v8::Global<v8::Object>>>> map_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(KeyWeakMap);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_COMMON_KEY_WEAK_MAP_H_
|
|
@ -340,6 +340,7 @@
|
||||||
'atom/common/google_api_key.h',
|
'atom/common/google_api_key.h',
|
||||||
'atom/common/id_weak_map.cc',
|
'atom/common/id_weak_map.cc',
|
||||||
'atom/common/id_weak_map.h',
|
'atom/common/id_weak_map.h',
|
||||||
|
'atom/common/key_weak_map.h',
|
||||||
'atom/common/keyboard_util.cc',
|
'atom/common/keyboard_util.cc',
|
||||||
'atom/common/keyboard_util.h',
|
'atom/common/keyboard_util.h',
|
||||||
'atom/common/mouse_util.cc',
|
'atom/common/mouse_util.cc',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue