2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2013-04-25 13:46:04 +00:00
|
|
|
// 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>
|
2014-04-16 01:54:01 +00:00
|
|
|
#include <vector>
|
2013-04-25 13:46:04 +00:00
|
|
|
|
2014-03-16 01:37:04 +00:00
|
|
|
#include "base/basictypes.h"
|
2014-04-16 07:14:44 +00:00
|
|
|
#include "native_mate/scoped_persistent.h"
|
2014-04-16 01:54:01 +00:00
|
|
|
#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:
|
2014-04-16 01:54:01 +00:00
|
|
|
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();
|
|
|
|
|
2014-04-16 01:54:01 +00:00
|
|
|
int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Handle<v8::Value> Get(v8::Isolate* isolate, int32_t key);
|
2014-04-16 01:54:01 +00:00
|
|
|
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-06-28 14:33:00 +00:00
|
|
|
static void WeakCallback(
|
|
|
|
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data);
|
2014-04-16 03:58:48 +00:00
|
|
|
|
2014-04-16 01:54:01 +00:00
|
|
|
int32_t next_id_;
|
2014-04-16 07:14:44 +00:00
|
|
|
|
|
|
|
typedef scoped_refptr<mate::RefCountedPersistent<v8::Object> >
|
|
|
|
RefCountedV8Object;
|
2014-04-16 01:54:01 +00:00
|
|
|
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_
|