23 static const uintptr_t kPersistentContainerNotFound = 0;
39 template<
typename K,
typename V>
46 static bool Empty(
Impl* impl) {
return impl->empty(); }
47 static size_t
Size(
Impl* impl) {
return impl->size(); }
55 std::pair<
Iterator,
bool> res = impl->insert(std::make_pair(key, value));
58 old_value = res.first->second;
59 res.first->second = value;
65 if (it == impl->end())
return kPersistentContainerNotFound;
70 if (it == impl->end())
return kPersistentContainerNotFound;
86 template<
typename K,
typename V>
112 template <
typename K,
typename V>
115 template <
typename T>
116 struct RemovePointer;
144 template <
typename T>
145 struct RemovePointer<T*> {
161 template <
typename K,
typename V,
typename Traits>
169 size_t
Size() {
return Traits::Size(&impl_); }
180 return Local<V>::New(isolate_,
FromVal(Traits::Get(&impl_, key)
));
187 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
196 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
205 V8::RegisterExternallyReferencedObject(
214 return Release(Traits::Remove(&impl_, key)
).Pass();
222 typedef typename Traits::Iterator It;
225 while (!Traits::Empty(&impl_)) {
226 typename Traits::Impl impl;
227 Traits::Swap(impl_, impl);
228 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
229 Traits::Dispose(isolate_,
Release(Traits::Value(i)
).Pass(),
243 : value_(other.value_) { }
249 return value_ == kPersistentContainerNotFound;
253 return SetReturnValueFromVal(&returnValue, value_);
256 value_ = kPersistentContainerNotFound;
259 value_ = other.value_;
300 return reinterpret_cast<V*>(v);
304 V* v = persistent->val_;
305 persistent->val_ = 0;
321 if (Traits::kCallbackType !=
kNotWeak && p.IsWeak()) {
322 Traits::DisposeCallbackData(
323 p.
template ClearWeak<
typename Traits::WeakCallbackDataType>());
330 p.val_ =
FromVal(Traits::Remove(&impl_, key)
);
340 bool hasValue = value != kPersistentContainerNotFound;
342 returnValue->SetInternal(
349 typename Traits::Impl impl_;
353 template <
typename K,
typename V,
typename Traits>
369 Global<V> persistent(
this->isolate(), value);
385 if (Traits::kCallbackType !=
kNotWeak) {
390 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
391 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
392 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
396 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
397 return this->Release(old_value).Pass();
406 *reference =
this->Leak(&value);
411 static void WeakCallback(
413 if (Traits::kCallbackType !=
kNotWeak) {
415 Traits::MapFromWeakCallbackInfo(data);
416 K key = Traits::KeyFromWeakCallbackInfo(data);
417 Traits::Dispose(data.GetIsolate(),
418 persistentValueMap->Remove(key).Pass(), key);
419 Traits::DisposeCallbackData(data.GetParameter());
425 template <
typename K,
typename V,
typename Traits>
441 Global<V> persistent(
this->isolate(), value);
457 if (Traits::kCallbackType !=
kNotWeak) {
462 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
463 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
464 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
468 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
469 return this->Release(old_value).Pass();
478 *reference =
this->Leak(&value);
483 static void OnWeakCallback(
485 if (Traits::kCallbackType !=
kNotWeak) {
486 auto map = Traits::MapFromWeakCallbackInfo(data);
487 K key = Traits::KeyFromWeakCallbackInfo(data);
488 map->RemoveWeak(key);
489 Traits::OnWeakCallback(data);
490 data.SetSecondPassCallback(SecondWeakCallback);
494 static void SecondWeakCallback(
496 Traits::DisposeWeak(data);
508 template<
typename K,
typename V,
524 template <
typename K,
typename V,
538 impl->push_back(value);
541 return impl->empty();
547 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
550 impl->reserve(capacity);
581 Global<V> persistent(isolate_, value);
582 Traits::Append(&impl_, ClearAndLeak(&persistent));
589 Traits::Append(&impl_, ClearAndLeak(&persistent));
596 return Traits::IsEmpty(&impl_);
603 return Traits::Size(&impl_);
610 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
617 size_t length = Traits::Size(&impl_);
618 for (size_t i = 0; i < length; i++) {
620 p.val_ = FromVal(Traits::Get(&impl_, i));
622 Traits::Clear(&impl_);
630 Traits::ReserveCapacity(&impl_, capacity);
635 V* v = persistent->val_;
636 persistent->val_ = 0;
641 return reinterpret_cast<V*>(v);
645 typename Traits::Impl impl_;