electron/atom/common/id_weak_map.cc

89 lines
2 KiB
C++
Raw Normal View History

2015-06-24 05:22:09 +00:00
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/id_weak_map.h"
#include <utility>
#include "native_mate/converter.h"
namespace atom {
2015-08-27 08:41:51 +00:00
namespace {
struct ObjectKey {
ObjectKey(int id, IDWeakMap* map) : id(id), map(map) {}
int id;
IDWeakMap* map;
};
void OnObjectGC(const v8::WeakCallbackInfo<ObjectKey>& data) {
ObjectKey* key = data.GetParameter();
key->map->Remove(key->id);
delete key;
}
} // namespace
2015-06-24 05:22:09 +00:00
IDWeakMap::IDWeakMap() : next_id_(0) {
}
IDWeakMap::~IDWeakMap() {
}
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
int32_t id = GetNextID();
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
2015-08-27 08:41:51 +00:00
ObjectKey* key = new ObjectKey(id, this);
global->SetWeak(key, OnObjectGC, v8::WeakCallbackType::kParameter);
map_[id] = global;
2015-06-24 05:22:09 +00:00
return id;
}
v8::MaybeLocal<v8::Object> IDWeakMap::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);
}
bool IDWeakMap::Has(int32_t id) const {
return map_.find(id) != map_.end();
}
std::vector<int32_t> IDWeakMap::Keys() const {
std::vector<int32_t> keys;
keys.reserve(map_.size());
for (const auto& iter : map_)
keys.emplace_back(iter.first);
return keys;
}
2015-06-24 08:54:20 +00:00
std::vector<v8::Local<v8::Object>> IDWeakMap::Values(v8::Isolate* isolate) {
std::vector<v8::Local<v8::Object>> keys;
keys.reserve(map_.size());
for (const auto& iter : map_)
keys.emplace_back(v8::Local<v8::Object>::New(isolate, *iter.second));
return keys;
}
2015-06-24 05:22:09 +00:00
void IDWeakMap::Remove(int32_t id) {
auto iter = map_.find(id);
if (iter == map_.end())
LOG(WARNING) << "Removing unexist object with ID " << id;
else
map_.erase(iter);
}
2015-06-24 06:36:05 +00:00
void IDWeakMap::Clear() {
map_.clear();
}
int32_t IDWeakMap::GetNextID() {
2015-06-24 05:22:09 +00:00
return ++next_id_;
}
} // namespace atom