25template <
typename K,
typename V,
typename Traits>
29static const uintptr_t kPersistentContainerNotFound = 0;
43template<
typename K,
typename V>
47 typedef std::map<K, PersistentContainerValue>
Impl;
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;
90template<
typename K,
typename V>
116template <
typename K,
typename V>
119 template <
typename T>
120 struct RemovePointer;
148 template <
typename T>
149 struct RemovePointer<T*> {
165template <
typename K,
typename V,
typename Traits>
173 size_t Size() {
return Traits::Size(&impl_); }
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_;
286 : isolate_(
isolate), label_(nullptr) {}
288 : isolate_(
isolate), label_(label) {}
293 typename Traits::Impl*
impl() {
return &impl_; }
296 return reinterpret_cast<V*
>(v);
300 V* v = persistent->val_;
301 persistent->val_ =
nullptr;
318 Traits::DisposeCallbackData(
319 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
326 p.val_ =
FromVal(Traits::Remove(&impl_, key));
340 bool hasValue = value != kPersistentContainerNotFound;
342 returnValue->SetInternal(
349 typename Traits::Impl impl_;
353template <
typename K,
typename V,
typename Traits>
387 if (Traits::kCallbackType ==
kNotWeak) {
395 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
396 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
401 return this->
Release(old_value).Pass();
410 *reference = this->
Leak(&value);
415 static void WeakCallback(
417 if (Traits::kCallbackType !=
kNotWeak) {
419 Traits::MapFromWeakCallbackInfo(data);
420 K key = Traits::KeyFromWeakCallbackInfo(data);
422 persistentValueMap->
Remove(key).Pass(), key);
429template <
typename K,
typename V,
typename Traits>
463 if (Traits::kCallbackType ==
kNotWeak) {
471 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
472 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
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);
502 static void SecondWeakCallback(
503 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
504 Traits::DisposeWeak(data);
516template<
typename K,
typename V,
517 typename Traits = DefaultPersistentValueMapTraits<K, V> >
532template <
typename K,
typename V,
533 typename Traits = DefaultGlobalMapTraits<K, V> >
543 typedef std::vector<PersistentContainerValue>
Impl;
546 impl->push_back(value);
549 return impl->empty();
555 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
558 impl->reserve(capacity);
576template<
typename V,
typename Traits = DefaultPersistentValueVectorTraits>
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_;
static const PersistentContainerCallbackType kCallbackType
static void Dispose(Isolate *isolate, Global< V > value, K key)
static void DisposeWeak(const WeakCallbackInfo< WeakCallbackDataType > &data)
void WeakCallbackDataType
static void OnWeakCallback(const WeakCallbackInfo< WeakCallbackDataType > &data)
GlobalValueMap< K, V, DefaultGlobalMapTraits< K, V > > MapType
static void DisposeCallbackData(WeakCallbackDataType *data)
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static const PersistentContainerCallbackType kCallbackType
static void Dispose(Isolate *isolate, Global< V > value, K key)
PersistentValueMap< K, V, DefaultPersistentValueMapTraits< K, V > > MapType
void WeakCallbackDataType
static void DisposeCallbackData(WeakCallbackDataType *data)
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static void ReserveCapacity(Impl *impl, size_t capacity)
static bool IsEmpty(const Impl *impl)
static PersistentContainerValue Get(const Impl *impl, size_t i)
static size_t Size(const Impl *impl)
static void Append(Impl *impl, PersistentContainerValue value)
static void Clear(Impl *impl)
std::vector< PersistentContainerValue > Impl
GlobalValueMap(Isolate *isolate)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > Set(const K &key, Local< V > value)
GlobalValueMap(Isolate *isolate, const char *label)
Global< V > Set(const K &key, Global< V > value)
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
Global< V > SetUnique(const K &key, Global< V > *persistent)
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
V8_INLINE bool IsWeak() const
V8_INLINE void AnnotateStrongRetainer(const char *label)
Local< V > NewLocal(Isolate *isolate) const
bool SetReturnValue(ReturnValue< T > returnValue)
void operator=(const PersistentValueReference &other)
PersistentValueReference(const PersistentValueReference &other)
PersistentValueReference()
static Global< V > Release(PersistentContainerValue v)
static PersistentContainerValue Leak(Global< V > *persistent)
~PersistentValueMapBase()
Global< V > Remove(const K &key)
PersistentValueMapBase(Isolate *isolate, const char *label)
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
void RemoveWeak(const K &key)
static V * FromVal(PersistentContainerValue v)
PersistentValueReference GetReference(const K &key)
bool Contains(const K &key)
static PersistentContainerValue ClearAndLeak(Global< V > *persistent)
PersistentValueMapBase(Isolate *isolate)
void AnnotateStrongRetainer(Global< V > *persistent)
Local< V > Get(const K &key)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > Set(const K &key, Local< V > value)
PersistentValueMap(Isolate *isolate, const char *label)
PersistentValueMap(Isolate *isolate)
Global< V > Set(const K &key, Global< V > value)
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
Global< V > SetUnique(const K &key, Global< V > *persistent)
void Append(Local< V > value)
Local< V > Get(size_t index) const
PersistentValueVector(Isolate *isolate)
void ReserveCapacity(size_t capacity)
void Append(Global< V > persistent)
StdGlobalValueMap(Isolate *isolate)
static PersistentContainerValue Set(Impl *impl, K key, PersistentContainerValue value)
static Iterator End(Impl *impl)
static PersistentContainerValue Value(Iterator it)
static void Swap(Impl &a, Impl &b)
static K Key(Iterator it)
static PersistentContainerValue Remove(Impl *impl, K key)
static PersistentContainerValue Get(Impl *impl, K key)
static size_t Size(Impl *impl)
static bool Empty(Impl *impl)
std::map< K, PersistentContainerValue > Impl
static Iterator Begin(Impl *impl)
StdPersistentValueMap(Isolate *isolate)
V8_INLINE T * GetParameter() const
void SetSecondPassCallback(Callback callback) const
V8_INLINE Isolate * GetIsolate() const
PersistentContainerCallbackType
@ kWeakWithInternalFields
uintptr_t PersistentContainerValue