electron/atom/common/api/atom_api_id_weak_map.h

56 lines
1.4 KiB
C
Raw Normal View History

2013-04-25 13:46:04 +00:00
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Copyright (c) 2012 Intel Corp. All rights reserved.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2013-04-25 13:46:04 +00:00
// found in the LICENSE file.
2013-04-29 12:40:59 +00:00
#ifndef ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
#define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
2013-04-25 13:46:04 +00:00
#include <map>
#include <vector>
2013-04-25 13:46:04 +00:00
2014-03-16 01:37:04 +00:00
#include "base/basictypes.h"
#include "native_mate/scoped_persistent.h"
#include "native_mate/wrappable.h"
2013-04-25 13:46:04 +00:00
namespace atom {
namespace api {
2014-03-16 01:37:04 +00:00
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
2014-04-16 03:58:48 +00:00
class IDWeakMap : public mate::Wrappable {
2013-04-25 13:46:04 +00:00
public:
IDWeakMap();
2014-04-16 03:58:48 +00:00
static void BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype);
2013-04-25 13:46:04 +00:00
private:
virtual ~IDWeakMap();
int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
v8::Handle<v8::Value> Get(int32_t key);
bool Has(int32_t key) const;
std::vector<int32_t> Keys() const;
void Remove(int32_t key);
int GetNextID();
2013-04-25 13:46:04 +00:00
2014-04-16 03:58:48 +00:00
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self);
int32_t next_id_;
typedef scoped_refptr<mate::RefCountedPersistent<v8::Object> >
RefCountedV8Object;
std::map<int32_t, RefCountedV8Object> map_;
2013-04-25 13:46:04 +00:00
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
};
} // namespace api
} // namespace atom
2013-04-29 12:40:59 +00:00
#endif // ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_