25 template <
typename K,
typename V,
typename Traits>
29 static const uintptr_t kPersistentContainerNotFound = 0;
43 template<
typename K,
typename V>
50 static bool Empty(
Impl* impl) {
return impl->empty(); }
51 static size_t
Size(
Impl* impl) {
return impl->size(); }
59 std::pair<
Iterator,
bool> res = impl->insert(std::make_pair(key, value));
62 old_value = res.first->second;
63 res.first->second = value;
69 if (it == impl->end())
return kPersistentContainerNotFound;
74 if (it == impl->end())
return kPersistentContainerNotFound;
90 template<
typename K,
typename V>
116 template <
typename K,
typename V>
119 template <
typename T>
120 struct RemovePointer;
148 template <
typename T>
149 struct RemovePointer<T*> {
165 template <
typename K,
typename V,
typename Traits>
173 size_t
Size() {
return Traits::Size(&impl_); }
184 return Local<V>::New(isolate_,
FromVal(Traits::Get(&impl_, key)
));
191 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
200 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
207 return Release(Traits::Remove(&impl_, key)
).Pass();
215 typedef typename Traits::Iterator It;
218 while (!Traits::Empty(&impl_)) {
219 typename Traits::Impl impl;
220 Traits::Swap(impl_, impl);
221 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
222 Traits::Dispose(isolate_,
Release(Traits::Value(i)
).Pass(),
236 : value_(other.value_) { }
242 return value_ == kPersistentContainerNotFound;
246 return SetReturnValueFromVal(&returnValue, value_);
249 value_ = kPersistentContainerNotFound;
252 value_ = other.value_;
257 friend class PersistentValueMap<K, V, Traits>;
258 friend class GlobalValueMap<K, V, Traits>;
286 : isolate_(isolate), label_(
nullptr) {}
288 : isolate_(isolate), label_(label) {}
296 return reinterpret_cast<V*>(v);
300 V* v = persistent->val_;
301 persistent->val_ =
nullptr;
317 if (Traits::kCallbackType !=
kNotWeak && p.IsWeak()) {
318 Traits::DisposeCallbackData(
319 p.
template ClearWeak<
typename Traits::WeakCallbackDataType>());
326 p.val_ =
FromVal(Traits::Remove(&impl_, key)
);
331 persistent->AnnotateStrongRetainer(label_);
340 bool hasValue = value != kPersistentContainerNotFound;
342 returnValue->SetInternal(
349 typename Traits::Impl impl_;
353 template <
typename K,
typename V,
typename Traits>
371 Global<V> persistent(
this->isolate(), value);
387 if (Traits::kCallbackType ==
kNotWeak) {
388 this->AnnotateStrongRetainer(persistent);
394 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
395 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
396 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
400 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
401 return this->Release(old_value).Pass();
410 *reference =
this->Leak(&value);
415 static void WeakCallback(
417 if (Traits::kCallbackType !=
kNotWeak) {
418 PersistentValueMap<K, V, Traits>* persistentValueMap =
419 Traits::MapFromWeakCallbackInfo(data);
420 K key = Traits::KeyFromWeakCallbackInfo(data);
421 Traits::Dispose(data.GetIsolate(),
422 persistentValueMap->Remove(key).Pass(), key);
423 Traits::DisposeCallbackData(data.GetParameter());
429 template <
typename K,
typename V,
typename Traits>
447 Global<V> persistent(
this->isolate(), value);
463 if (Traits::kCallbackType ==
kNotWeak) {
464 this->AnnotateStrongRetainer(persistent);
470 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
471 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
472 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
476 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
477 return this->Release(old_value).Pass();
486 *reference =
this->Leak(&value);
491 static void OnWeakCallback(
493 if (Traits::kCallbackType !=
kNotWeak) {
494 auto map = Traits::MapFromWeakCallbackInfo(data);
495 K key = Traits::KeyFromWeakCallbackInfo(data);
496 map->RemoveWeak(key);
497 Traits::OnWeakCallback(data);
498 data.SetSecondPassCallback(SecondWeakCallback);
502 static void SecondWeakCallback(
504 Traits::DisposeWeak(data);
516 template<
typename K,
typename V,
521 : PersistentValueMap<K, V, Traits>(isolate) {}
532 template <
typename K,
typename V,
537 : GlobalValueMap<K, V, Traits>(isolate) {}
546 impl->push_back(value);
549 return impl->empty();
555 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
558 impl->reserve(capacity);
589 Global<V> persistent(isolate_, value);
590 Traits::Append(&impl_, ClearAndLeak(&persistent));
597 Traits::Append(&impl_, ClearAndLeak(&persistent));
604 return Traits::IsEmpty(&impl_);
611 return Traits::Size(&impl_);
618 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
625 size_t length = Traits::Size(&impl_);
626 for (size_t i = 0; i < length; i++) {
628 p.val_ = FromVal(Traits::Get(&impl_, i));
630 Traits::Clear(&impl_);
638 Traits::ReserveCapacity(&impl_, capacity);
643 V* v = persistent->val_;
644 persistent->val_ =
nullptr;
649 return reinterpret_cast<V*>(v);
653 typename Traits::Impl impl_;