Add KeyWeakMap without Add method

This commit is contained in:
Cheng Zhao 2016-05-11 13:40:33 +09:00
parent 7f2a46cdd5
commit 79c1ad85f9
2 changed files with 40 additions and 24 deletions

View file

@ -13,9 +13,9 @@ namespace atom {
namespace { namespace {
struct ObjectKey { struct ObjectKey {
ObjectKey(int id, IDWeakMap* map) : id(id), map(map) {} ObjectKey(int id, KeyWeakMap* map) : id(id), map(map) {}
int id; int id;
IDWeakMap* map; KeyWeakMap* map;
}; };
void OnObjectGC(const v8::WeakCallbackInfo<ObjectKey>& data) { void OnObjectGC(const v8::WeakCallbackInfo<ObjectKey>& data) {
@ -26,13 +26,13 @@ void OnObjectGC(const v8::WeakCallbackInfo<ObjectKey>& data) {
} // namespace } // 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, int32_t id,
v8::Local<v8::Object> object) { v8::Local<v8::Object> object) {
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object)); auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
@ -41,13 +41,7 @@ void IDWeakMap::Set(v8::Isolate* isolate,
map_[id] = global; map_[id] = global;
} }
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) { v8::MaybeLocal<v8::Object> KeyWeakMap::Get(v8::Isolate* isolate, int32_t id) {
int32_t id = GetNextID();
Set(isolate, id, object);
return id;
}
v8::MaybeLocal<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
auto iter = map_.find(id); auto iter = map_.find(id);
if (iter == map_.end()) if (iter == map_.end())
return v8::MaybeLocal<v8::Object>(); return v8::MaybeLocal<v8::Object>();
@ -55,11 +49,11 @@ v8::MaybeLocal<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
return v8::Local<v8::Object>::New(isolate, *iter->second); return v8::Local<v8::Object>::New(isolate, *iter->second);
} }
bool IDWeakMap::Has(int32_t id) const { bool KeyWeakMap::Has(int32_t id) const {
return map_.find(id) != map_.end(); return map_.find(id) != map_.end();
} }
std::vector<v8::Local<v8::Object>> IDWeakMap::Values(v8::Isolate* isolate) { std::vector<v8::Local<v8::Object>> KeyWeakMap::Values(v8::Isolate* isolate) {
std::vector<v8::Local<v8::Object>> keys; std::vector<v8::Local<v8::Object>> keys;
keys.reserve(map_.size()); keys.reserve(map_.size());
for (const auto& iter : map_) for (const auto& iter : map_)
@ -67,7 +61,7 @@ std::vector<v8::Local<v8::Object>> IDWeakMap::Values(v8::Isolate* isolate) {
return keys; return keys;
} }
void IDWeakMap::Remove(int32_t id) { void KeyWeakMap::Remove(int32_t id) {
auto iter = map_.find(id); auto iter = map_.find(id);
if (iter == map_.end()) if (iter == map_.end())
LOG(WARNING) << "Removing unexist object with ID " << id; LOG(WARNING) << "Removing unexist object with ID " << id;
@ -75,6 +69,18 @@ void IDWeakMap::Remove(int32_t id) {
map_.erase(iter); map_.erase(iter);
} }
IDWeakMap::IDWeakMap() : next_id_(0) {
}
IDWeakMap::~IDWeakMap() {
}
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
int32_t id = GetNextID();
Set(isolate, id, object);
return id;
}
int32_t IDWeakMap::GetNextID() { int32_t IDWeakMap::GetNextID() {
return ++next_id_; return ++next_id_;
} }

View file

@ -14,17 +14,14 @@
namespace atom { namespace atom {
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. // Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
class IDWeakMap { class KeyWeakMap {
public: public:
IDWeakMap(); KeyWeakMap();
~IDWeakMap(); virtual ~KeyWeakMap();
// Sets the object to WeakMap with the given |id|. // Sets the object to WeakMap with the given |id|.
void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object); void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object);
// Adds |object| to WeakMap and returns its allocated |id|.
int32_t Add(v8::Isolate* isolate, v8::Local<v8::Object> object);
// Gets the object from WeakMap by its |id|. // Gets the object from WeakMap by its |id|.
v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, int32_t id); v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, int32_t id);
@ -37,6 +34,22 @@ class IDWeakMap {
// Remove object with |id| in the WeakMap. // Remove object with |id| in the WeakMap.
void Remove(int32_t key); void Remove(int32_t key);
private:
// Map of stored objects.
std::unordered_map<int32_t, linked_ptr<v8::Global<v8::Object>>> 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<v8::Object> object);
private: private:
// Returns next available ID. // Returns next available ID.
int32_t GetNextID(); int32_t GetNextID();
@ -44,9 +57,6 @@ class IDWeakMap {
// ID of next stored object. // ID of next stored object.
int32_t next_id_; int32_t next_id_;
// Map of stored objects.
std::unordered_map<int32_t, linked_ptr<v8::Global<v8::Object>>> map_;
DISALLOW_COPY_AND_ASSIGN(IDWeakMap); DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
}; };