Enable storing TrackableObject in other C++ class
This commit is contained in:
parent
7f0658efa7
commit
552a12d2ee
2 changed files with 45 additions and 1 deletions
|
@ -4,8 +4,28 @@
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
|
|
||||||
|
#include "base/supports_user_data.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const char* kTrackedObjectKey = "TrackedObjectKey";
|
||||||
|
|
||||||
|
class IDUserData : public base::SupportsUserData::Data {
|
||||||
|
public:
|
||||||
|
explicit IDUserData(int32_t id) : id_(id) {}
|
||||||
|
|
||||||
|
operator int32_t() const { return id_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int32_t id_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(IDUserData);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
atom::IDWeakMap TrackableObject::weak_map_;
|
atom::IDWeakMap TrackableObject::weak_map_;
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
@ -20,6 +40,15 @@ TrackableObject* TrackableObject::FromWeakMapID(v8::Isolate* isolate,
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
TrackableObject* TrackableObject::FromWrappedClass(
|
||||||
|
v8::Isolate* isolate, base::SupportsUserData* wrapped) {
|
||||||
|
auto id = static_cast<IDUserData*>(wrapped->GetUserData(kTrackedObjectKey));
|
||||||
|
if (!id)
|
||||||
|
return nullptr;
|
||||||
|
return FromWeakMapID(isolate, *id);
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void TrackableObject::ReleaseAllWeakReferences() {
|
void TrackableObject::ReleaseAllWeakReferences() {
|
||||||
weak_map_.Clear();
|
weak_map_.Clear();
|
||||||
|
@ -36,4 +65,8 @@ void TrackableObject::AfterInit(v8::Isolate* isolate) {
|
||||||
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
|
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TrackableObject::Attach(base::SupportsUserData* wrapped) {
|
||||||
|
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
|
@ -8,15 +8,23 @@
|
||||||
#include "atom/browser/api/event_emitter.h"
|
#include "atom/browser/api/event_emitter.h"
|
||||||
#include "atom/common/id_weak_map.h"
|
#include "atom/common/id_weak_map.h"
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
class SupportsUserData;
|
||||||
|
}
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
// All instances of TrackableObject will be kept in a weak map and can be got
|
// All instances of TrackableObject will be kept in a weak map and can be got
|
||||||
// from its ID.
|
// from its ID.
|
||||||
class TrackableObject : public mate::EventEmitter {
|
class TrackableObject : public mate::EventEmitter {
|
||||||
public:
|
public:
|
||||||
// Find out the TrackableObject from its ID in weak map:
|
// Find out the TrackableObject from its ID in weak map.
|
||||||
static TrackableObject* FromWeakMapID(v8::Isolate* isolate, int32_t id);
|
static TrackableObject* FromWeakMapID(v8::Isolate* isolate, int32_t id);
|
||||||
|
|
||||||
|
// Find out the TrackableObject from the class it wraps.
|
||||||
|
static TrackableObject* FromWrappedClass(
|
||||||
|
v8::Isolate* isolate, base::SupportsUserData* wrapped);
|
||||||
|
|
||||||
// Releases all weak references in weak map, called when app is terminating.
|
// Releases all weak references in weak map, called when app is terminating.
|
||||||
static void ReleaseAllWeakReferences();
|
static void ReleaseAllWeakReferences();
|
||||||
|
|
||||||
|
@ -30,6 +38,9 @@ class TrackableObject : public mate::EventEmitter {
|
||||||
TrackableObject();
|
TrackableObject();
|
||||||
~TrackableObject() override;
|
~TrackableObject() override;
|
||||||
|
|
||||||
|
// Wrap TrackableObject into a class that SupportsUserData.
|
||||||
|
void Attach(base::SupportsUserData* wrapped);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static atom::IDWeakMap weak_map_;
|
static atom::IDWeakMap weak_map_;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue