22 static const uintptr_t kPersistentContainerNotFound = 0;
38 template<
typename K,
typename V>
45 static bool Empty(
Impl* impl) {
return impl->empty(); }
46 static size_t
Size(
Impl* impl) {
return impl->size(); }
54 std::pair<
Iterator,
bool> res = impl->insert(std::make_pair(key, value));
57 old_value = res.first->second;
58 res.first->second = value;
64 if (it == impl->end())
return kPersistentContainerNotFound;
69 if (it == impl->end())
return kPersistentContainerNotFound;
85 template<
typename K,
typename V>
111 template <
typename K,
typename V>
114 template <
typename T>
115 struct RemovePointer;
143 template <
typename T>
144 struct RemovePointer<T*> {
160 template <
typename K,
typename V,
typename Traits>
168 size_t
Size() {
return Traits::Size(&impl_); }
179 return Local<V>::New(isolate_,
FromVal(Traits::Get(&impl_, key)
));
186 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
195 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
204 reinterpret_cast<
internal::Object**>(parent.val_),
228 return Release(Traits::Remove(&impl_, key)
).Pass();
236 typedef typename Traits::Iterator It;
239 while (!Traits::Empty(&impl_)) {
240 typename Traits::Impl impl;
241 Traits::Swap(impl_, impl);
242 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
243 Traits::Dispose(isolate_,
Release(Traits::Value(i)
).Pass(),
257 : value_(other.value_) { }
263 return value_ == kPersistentContainerNotFound;
267 return SetReturnValueFromVal(&returnValue, value_);
270 value_ = kPersistentContainerNotFound;
273 value_ = other.value_;
314 return reinterpret_cast<V*>(v);
318 V* v = persistent->val_;
319 persistent->val_ = 0;
335 if (Traits::kCallbackType !=
kNotWeak && p.IsWeak()) {
336 Traits::DisposeCallbackData(
337 p.
template ClearWeak<
typename Traits::WeakCallbackDataType>());
344 p.val_ =
FromVal(Traits::Remove(&impl_, key)
);
354 bool hasValue = value != kPersistentContainerNotFound;
356 returnValue->SetInternal(
363 typename Traits::Impl impl_;
367 template <
typename K,
typename V,
typename Traits>
383 Global<V> persistent(
this->isolate(), value);
399 if (Traits::kCallbackType !=
kNotWeak) {
400 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
401 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
402 Traits::WeakCallbackParameter(
this, key, value), WeakCallback);
405 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
406 return this->Release(old_value).Pass();
415 *reference =
this->Leak(&value);
420 static void WeakCallback(
422 if (Traits::kCallbackType !=
kNotWeak) {
424 Traits::MapFromWeakCallbackInfo(data);
425 K key = Traits::KeyFromWeakCallbackInfo(data);
426 Traits::Dispose(data.GetIsolate(),
427 persistentValueMap->Remove(key).Pass(), key);
428 Traits::DisposeCallbackData(data.GetParameter());
434 template <
typename K,
typename V,
typename Traits>
450 Global<V> persistent(
this->isolate(), value);
466 if (Traits::kCallbackType !=
kNotWeak) {
471 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
472 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
473 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
477 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
478 return this->Release(old_value).Pass();
487 *reference =
this->Leak(&value);
492 static void OnWeakCallback(
494 if (Traits::kCallbackType !=
kNotWeak) {
495 auto map = Traits::MapFromWeakCallbackInfo(data);
496 K key = Traits::KeyFromWeakCallbackInfo(data);
497 map->RemoveWeak(key);
498 Traits::OnWeakCallback(data);
499 data.SetSecondPassCallback(SecondWeakCallback);
503 static void SecondWeakCallback(
505 Traits::DisposeWeak(data);
517 template<
typename K,
typename V,
533 template <
typename K,
typename V,
547 impl->push_back(value);
550 return impl->empty();
556 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
559 impl->reserve(capacity);
590 Global<V> persistent(isolate_, value);
591 Traits::Append(&impl_, ClearAndLeak(&persistent));
598 Traits::Append(&impl_, ClearAndLeak(&persistent));
605 return Traits::IsEmpty(&impl_);
612 return Traits::Size(&impl_);
619 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
626 size_t length = Traits::Size(&impl_);
627 for (size_t i = 0; i < length; i++) {
629 p.val_ = FromVal(Traits::Get(&impl_, i));
631 Traits::Clear(&impl_);
639 Traits::ReserveCapacity(&impl_, capacity);
644 V* v = persistent->val_;
645 persistent->val_ = 0;
650 return reinterpret_cast<V*>(v);
654 typename Traits::Impl impl_;