Add C++ version of IDWeakMap
This commit is contained in:
parent
b8cf9a2788
commit
cd93b9412c
3 changed files with 127 additions and 0 deletions
70
atom/common/id_weak_map.cc
Normal file
70
atom/common/id_weak_map.cc
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
// 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 {
|
||||||
|
|
||||||
|
IDWeakMap::IDWeakMap() : next_id_(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
IDWeakMap::~IDWeakMap() {
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
|
||||||
|
int32_t id = GetNextID();
|
||||||
|
object->SetHiddenValue(mate::StringToSymbol(isolate, "IDWeakMapKey"),
|
||||||
|
mate::Converter<int32_t>::ToV8(isolate, id));
|
||||||
|
|
||||||
|
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
|
||||||
|
global->SetWeak(this, &WeakCallback);
|
||||||
|
map_.emplace(id, global);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
int IDWeakMap::GetNextID() {
|
||||||
|
return ++next_id_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void IDWeakMap::WeakCallback(
|
||||||
|
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data) {
|
||||||
|
int32_t id = data.GetValue()->GetHiddenValue(
|
||||||
|
mate::StringToV8(data.GetIsolate(), "IDWeakMapKey"))->Int32Value();
|
||||||
|
data.GetParameter()->Remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace atom
|
55
atom/common/id_weak_map.h
Normal file
55
atom/common/id_weak_map.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright (c) 2015 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_COMMON_ID_WEAK_MAP_H_
|
||||||
|
#define ATOM_COMMON_ID_WEAK_MAP_H_
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "base/memory/linked_ptr.h"
|
||||||
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
|
||||||
|
class IDWeakMap {
|
||||||
|
public:
|
||||||
|
IDWeakMap();
|
||||||
|
~IDWeakMap();
|
||||||
|
|
||||||
|
// 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|.
|
||||||
|
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 IDs of all available objects.
|
||||||
|
std::vector<int32_t> Keys() const;
|
||||||
|
|
||||||
|
// Remove object with |id| in the WeakMap.
|
||||||
|
void Remove(int32_t key);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Returns next available ID.
|
||||||
|
int GetNextID();
|
||||||
|
|
||||||
|
static void WeakCallback(
|
||||||
|
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data);
|
||||||
|
|
||||||
|
// ID of next stored object.
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_COMMON_ID_WEAK_MAP_H_
|
|
@ -254,6 +254,8 @@
|
||||||
'atom/common/event_emitter_caller.cc',
|
'atom/common/event_emitter_caller.cc',
|
||||||
'atom/common/event_emitter_caller.h',
|
'atom/common/event_emitter_caller.h',
|
||||||
'atom/common/google_api_key.h',
|
'atom/common/google_api_key.h',
|
||||||
|
'atom/common/id_weak_map.cc',
|
||||||
|
'atom/common/id_weak_map.h',
|
||||||
'atom/common/linux/application_info.cc',
|
'atom/common/linux/application_info.cc',
|
||||||
'atom/common/native_mate_converters/accelerator_converter.cc',
|
'atom/common/native_mate_converters/accelerator_converter.cc',
|
||||||
'atom/common/native_mate_converters/accelerator_converter.h',
|
'atom/common/native_mate_converters/accelerator_converter.h',
|
||||||
|
|
Loading…
Reference in a new issue