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 reinterpret_cast<
internal::Object**>(parent.val_),
215 V8::RegisterExternallyReferencedObject(
224 return Release(Traits::Remove(&impl_, key)
).Pass();
232 typedef typename Traits::Iterator It;
235 while (!Traits::Empty(&impl_)) {
236 typename Traits::Impl impl;
237 Traits::Swap(impl_, impl);
238 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
239 Traits::Dispose(isolate_,
Release(Traits::Value(i)
).Pass(),
253 : value_(other.value_) { }
259 return value_ == kPersistentContainerNotFound;
263 return SetReturnValueFromVal(&returnValue, value_);
266 value_ = kPersistentContainerNotFound;
269 value_ = other.value_;
310 return reinterpret_cast<V*>(v);
314 V* v = persistent->val_;
315 persistent->val_ = 0;
331 if (Traits::kCallbackType !=
kNotWeak && p.IsWeak()) {
332 Traits::DisposeCallbackData(
333 p.
template ClearWeak<
typename Traits::WeakCallbackDataType>());
340 p.val_ =
FromVal(Traits::Remove(&impl_, key)
);
350 bool hasValue = value != kPersistentContainerNotFound;
352 returnValue->SetInternal(
359 typename Traits::Impl impl_;
363 template <
typename K,
typename V,
typename Traits>
379 Global<V> persistent(
this->isolate(), value);
395 if (Traits::kCallbackType !=
kNotWeak) {
396 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
397 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
398 Traits::WeakCallbackParameter(
this, key, value), WeakCallback);
401 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
402 return this->Release(old_value).Pass();
411 *reference =
this->Leak(&value);
416 static void WeakCallback(
418 if (Traits::kCallbackType !=
kNotWeak) {
420 Traits::MapFromWeakCallbackInfo(data);
421 K key = Traits::KeyFromWeakCallbackInfo(data);
422 Traits::Dispose(data.GetIsolate(),
423 persistentValueMap->Remove(key).Pass(), key);
424 Traits::DisposeCallbackData(data.GetParameter());
430 template <
typename K,
typename V,
typename Traits>
446 Global<V> persistent(
this->isolate(), value);
462 if (Traits::kCallbackType !=
kNotWeak) {
467 Local<V> value(
Local<V>::New(
this->isolate(), *persistent));
468 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
469 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
473 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
474 return this->Release(old_value).Pass();
483 *reference =
this->Leak(&value);
488 static void OnWeakCallback(
490 if (Traits::kCallbackType !=
kNotWeak) {
491 auto map = Traits::MapFromWeakCallbackInfo(data);
492 K key = Traits::KeyFromWeakCallbackInfo(data);
493 map->RemoveWeak(key);
494 Traits::OnWeakCallback(data);
495 data.SetSecondPassCallback(SecondWeakCallback);
499 static void SecondWeakCallback(
501 Traits::DisposeWeak(data);
513 template<
typename K,
typename V,
529 template <
typename K,
typename V,
543 impl->push_back(value);
546 return impl->empty();
552 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
555 impl->reserve(capacity);
586 Global<V> persistent(isolate_, value);
587 Traits::Append(&impl_, ClearAndLeak(&persistent));
594 Traits::Append(&impl_, ClearAndLeak(&persistent));
601 return Traits::IsEmpty(&impl_);
608 return Traits::Size(&impl_);
615 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
622 size_t length = Traits::Size(&impl_);
623 for (size_t i = 0; i < length; i++) {
625 p.val_ = FromVal(Traits::Get(&impl_, i));
627 Traits::Clear(&impl_);
635 Traits::ReserveCapacity(&impl_, capacity);
640 V* v = persistent->val_;
641 persistent->val_ = 0;
646 return reinterpret_cast<V*>(v);
650 typename Traits::Impl impl_;