23 static const uintptr_t kPersistentContainerNotFound = 0;
37 template<
typename K,
typename V>
44 static bool Empty(
Impl* impl) {
return impl->empty(); }
45 static size_t
Size(
Impl* impl) {
return impl->size(); }
53 std::pair<
Iterator,
bool> res = impl->insert(std::make_pair(key, value));
56 old_value = res.first->second;
57 res.first->second = value;
63 if (it == impl->end())
return kPersistentContainerNotFound;
68 if (it == impl->end())
return kPersistentContainerNotFound;
84 template<
typename K,
typename V>
110 template <
typename K,
typename V>
113 template <
typename T>
114 struct RemovePointer;
142 template <
typename T>
143 struct RemovePointer<T*> {
159 template <
typename K,
typename V,
typename Traits>
167 size_t
Size() {
return Traits::Size(&impl_); }
178 return Local<V>::New(isolate_,
FromVal(Traits::Get(&impl_, key)
));
185 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
194 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
201 return Release(Traits::Remove(&impl_, key)
).Pass();
209 typedef typename Traits::Iterator It;
212 while (!Traits::Empty(&impl_)) {
213 typename Traits::Impl impl;
214 Traits::Swap(impl_, impl);
215 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
216 Traits::Dispose(isolate_,
Release(Traits::Value(i)
).Pass(),
230 : value_(other.value_) { }
236 return value_ == kPersistentContainerNotFound;
240 return SetReturnValueFromVal(&returnValue, value_);
243 value_ = kPersistentContainerNotFound;
246 value_ = other.value_;
280 : isolate_(isolate), label_(
nullptr) {}
282 : isolate_(isolate), label_(label) {}
290 return reinterpret_cast<V*>(v);
294 V* v = persistent->val_;
295 persistent->val_ =
nullptr;
311 if (Traits::kCallbackType !=
kNotWeak && p.IsWeak()) {
312 Traits::DisposeCallbackData(
313 p.
template ClearWeak<
typename Traits::WeakCallbackDataType>());
320 p.val_ =
FromVal(Traits::Remove(&impl_, key)
);
325 persistent->AnnotateStrongRetainer(label_);
334 bool hasValue = value != kPersistentContainerNotFound;
336 returnValue->SetInternal(
343 typename Traits::Impl impl_;
347 template <
typename K,
typename V,
typename Traits>
365 Global<V> persistent(
this->isolate(), value);
381 if (Traits::kCallbackType ==
kNotWeak) {
382 this->AnnotateStrongRetainer(persistent);
388 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
389 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
390 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
394 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
395 return this->Release(old_value).Pass();
404 *reference =
this->Leak(&value);
409 static void WeakCallback(
411 if (Traits::kCallbackType !=
kNotWeak) {
413 Traits::MapFromWeakCallbackInfo(data);
414 K key = Traits::KeyFromWeakCallbackInfo(data);
415 Traits::Dispose(data.GetIsolate(),
416 persistentValueMap->Remove(key).Pass(), key);
417 Traits::DisposeCallbackData(data.GetParameter());
423 template <
typename K,
typename V,
typename Traits>
441 Global<V> persistent(
this->isolate(), value);
457 if (Traits::kCallbackType ==
kNotWeak) {
458 this->AnnotateStrongRetainer(persistent);
464 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
465 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
466 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
470 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
471 return this->Release(old_value).Pass();
480 *reference =
this->Leak(&value);
485 static void OnWeakCallback(
487 if (Traits::kCallbackType !=
kNotWeak) {
488 auto map = Traits::MapFromWeakCallbackInfo(data);
489 K key = Traits::KeyFromWeakCallbackInfo(data);
490 map->RemoveWeak(key);
491 Traits::OnWeakCallback(data);
492 data.SetSecondPassCallback(SecondWeakCallback);
496 static void SecondWeakCallback(
498 Traits::DisposeWeak(data);
510 template<
typename K,
typename V,
526 template <
typename K,
typename V,
540 impl->push_back(value);
543 return impl->empty();
549 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
552 impl->reserve(capacity);
583 Global<V> persistent(isolate_, value);
584 Traits::Append(&impl_, ClearAndLeak(&persistent));
591 Traits::Append(&impl_, ClearAndLeak(&persistent));
598 return Traits::IsEmpty(&impl_);
605 return Traits::Size(&impl_);
612 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
619 size_t length = Traits::Size(&impl_);
620 for (size_t i = 0; i < length; i++) {
622 p.val_ = FromVal(Traits::Get(&impl_, i));
624 Traits::Clear(&impl_);
632 Traits::ReserveCapacity(&impl_, capacity);
637 V* v = persistent->val_;
638 persistent->val_ =
nullptr;
643 return reinterpret_cast<V*>(v);
647 typename Traits::Impl impl_;
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
Global< V > Set(const K &key, Global< V > value)
static void Clear(Impl *impl)
PersistentValueVector(Isolate *isolate)
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
~PersistentValueMapBase()
static size_t Size(const Impl *impl)
Global< V > SetUnique(const K &key, Global< V > *persistent)
static void OnWeakCallback(const WeakCallbackInfo< WeakCallbackDataType > &data)
Global< V > Set(const K &key, Local< V > value)
StdGlobalValueMap(Isolate *isolate)
static PersistentContainerValue Remove(Impl *impl, K key)
static PersistentContainerValue Leak(Global< V > *persistent)
static PersistentContainerValue ClearAndLeak(Global< V > *persistent)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > SetUnique(const K &key, Global< V > *persistent)
PersistentValueReference GetReference(const K &key)
static size_t Size(Impl *impl)
static bool IsEmpty(const Impl *impl)
static const PersistentContainerCallbackType kCallbackType
static void Append(Impl *impl, PersistentContainerValue value)
void ReserveCapacity(size_t capacity)
friend class WeakCallbackInfo
std::vector< PersistentContainerValue > Impl
static void ReserveCapacity(Impl *impl, size_t capacity)
static void DisposeWeak(const WeakCallbackInfo< WeakCallbackDataType > &data)
static V * FromVal(PersistentContainerValue v)
friend class PersistentValueVector
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
friend class PersistentValueMapBase
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static void Dispose(Isolate *isolate, Global< V > value, K key)
Global< V > Remove(const K &key)
static Global< V > Release(PersistentContainerValue v)
bool Contains(const K &key)
PersistentValueMap(Isolate *isolate)
GlobalValueMap< K, V, DefaultGlobalMapTraits< K, V > > MapType
friend class PersistentValueMapBase
PersistentContainerCallbackType
void WeakCallbackDataType
PersistentValueMapBase(Isolate *isolate, const char *label)
static bool Empty(Impl *impl)
static PersistentContainerValue Get(const Impl *impl, size_t i)
uintptr_t PersistentContainerValue
static const PersistentContainerCallbackType kCallbackType
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
GlobalValueMap(Isolate *isolate, const char *label)
static K Key(Iterator it)
bool SetReturnValue(ReturnValue< T > returnValue)
static Iterator Begin(Impl *impl)
static Iterator End(Impl *impl)
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
void AnnotateStrongRetainer(Global< V > *persistent)
static void Dispose(Isolate *isolate, Global< V > value, K key)
Local< V > Get(size_t index) const
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
PersistentValueReference(const PersistentValueReference &other)
StdPersistentValueMap(Isolate *isolate)
Local< V > Get(const K &key)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > Set(const K &key, Local< V > value)
static void DisposeCallbackData(WeakCallbackDataType *data)
static PersistentContainerValue Value(Iterator it)
void WeakCallbackDataType
void operator=(const PersistentValueReference &other)
Local< V > NewLocal(Isolate *isolate) const
void RemoveWeak(const K &key)
static PersistentContainerValue Set(Impl *impl, K key, PersistentContainerValue value)
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
PersistentValueMapBase(Isolate *isolate)
std::map< K, PersistentContainerValue > Impl
static void Swap(Impl &a, Impl &b)
void Append(Local< V > value)
GlobalValueMap(Isolate *isolate)
PersistentValueMap< K, V, DefaultPersistentValueMapTraits< K, V > > MapType
void Append(Global< V > persistent)
PersistentValueReference()
PersistentValueMap(Isolate *isolate, const char *label)
Global< V > Set(const K &key, Global< V > value)
static PersistentContainerValue Get(Impl *impl, K key)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static void DisposeCallbackData(WeakCallbackDataType *data)