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_;