V8 Project
v8::PersistentValueMap< K, V, Traits > Class Template Reference

A map wrapper that allows using UniquePersistent as a mapped value. More...

#include <v8-util.h>

+ Inheritance diagram for v8::PersistentValueMap< K, V, Traits >:
+ Collaboration diagram for v8::PersistentValueMap< K, V, Traits >:

Classes

class  PersistentValueReference
 Helper class for GetReference/SetWithReference. More...
 

Public Member Functions

 PersistentValueMap (Isolate *isolate)
 
 ~PersistentValueMap ()
 
IsolateGetIsolate ()
 
size_t Size ()
 Return size of the map. More...
 
bool IsWeak ()
 Return whether the map holds weak persistents. More...
 
Local< VGet (const K &key)
 Get value stored in map. More...
 
bool Contains (const K &key)
 Check whether a value is contained in the map. More...
 
bool SetReturnValue (const K &key, ReturnValue< Value > returnValue)
 Get value stored in map and set it in returnValue. More...
 
void SetReference (const K &key, const Persistent< Object > &parent)
 Call Isolate::SetReference with the given parent and the map value. More...
 
UniquePersistent< VSet (const K &key, Local< V > value)
 Put value into map. More...
 
UniquePersistent< VSet (const K &key, UniquePersistent< V > value)
 Put value into map, like Set(const K&, Local<V>). More...
 
UniquePersistent< VRemove (const K &key)
 Return value for key and remove it from the map. More...
 
void Clear ()
 Traverses the map repeatedly, in case side effects of disposal cause insertions. More...
 
PersistentValueReference GetReference (const K &key)
 Get a reference to a map value. More...
 
UniquePersistent< VSet (const K &key, UniquePersistent< V > value, PersistentValueReference *reference)
 Put a value into the map and update the reference. More...
 

Private Member Functions

 PersistentValueMap (PersistentValueMap &)
 
void operator= (PersistentValueMap &)
 
UniquePersistent< VSetUnique (const K &key, UniquePersistent< V > *persistent)
 Put the value into the map, and set the 'weak' callback when demanded by the Traits class. More...
 

Static Private Member Functions

static void WeakCallback (const WeakCallbackData< V, typename Traits::WeakCallbackDataType > &data)
 
static VFromVal (PersistentContainerValue v)
 
static bool SetReturnValueFromVal (ReturnValue< Value > *returnValue, PersistentContainerValue value)
 
static PersistentContainerValue ClearAndLeak (UniquePersistent< V > *persistent)
 
static PersistentContainerValue Leak (UniquePersistent< V > *persistent)
 
static UniquePersistent< VRelease (PersistentContainerValue v)
 Return a container value as UniquePersistent and make sure the weak callback is properly disposed of. More...
 

Private Attributes

Isolateisolate_
 
Traits::Impl impl_
 

Detailed Description

template<typename K, typename V, typename Traits>
class v8::PersistentValueMap< K, V, Traits >

A map wrapper that allows using UniquePersistent as a mapped value.

C++11 embedders don't need this class, as they can use UniquePersistent directly in std containers.

The map relies on a backing map, whose type and accessors are described by the Traits class. The backing map will handle values of type PersistentContainerValue, with all conversion into and out of V8 handles being transparently handled by this class.

Definition at line 119 of file v8-util.h.

Constructor & Destructor Documentation

◆ PersistentValueMap() [1/2]

template<typename K , typename V , typename Traits >
v8::PersistentValueMap< K, V, Traits >::PersistentValueMap ( Isolate isolate)
inlineexplicit

Definition at line 121 of file v8-util.h.

121 : isolate_(isolate) {}
Isolate * isolate_
Definition: v8-util.h:348

◆ ~PersistentValueMap()

template<typename K , typename V , typename Traits >
v8::PersistentValueMap< K, V, Traits >::~PersistentValueMap ( )
inline

Definition at line 123 of file v8-util.h.

123 { Clear(); }
void Clear()
Traverses the map repeatedly, in case side effects of disposal cause insertions.
Definition: v8-util.h:198

References v8::PersistentValueMap< K, V, Traits >::Clear().

+ Here is the call graph for this function:

◆ PersistentValueMap() [2/2]

template<typename K , typename V , typename Traits >
v8::PersistentValueMap< K, V, Traits >::PersistentValueMap ( PersistentValueMap< K, V, Traits > &  )
private

Member Function Documentation

◆ Clear()

template<typename K , typename V , typename Traits >
void v8::PersistentValueMap< K, V, Traits >::Clear ( )
inline

Traverses the map repeatedly, in case side effects of disposal cause insertions.

Definition at line 198 of file v8-util.h.

198  {
199  typedef typename Traits::Iterator It;
200  HandleScope handle_scope(isolate_);
201  // TODO(dcarney): figure out if this swap and loop is necessary.
202  while (!Traits::Empty(&impl_)) {
203  typename Traits::Impl impl;
204  Traits::Swap(impl_, impl);
205  for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
206  Traits::Dispose(isolate_, Release(Traits::Value(i)).Pass(),
207  Traits::Key(i));
208  }
209  }
210  }
Traits::Impl impl_
Definition: v8-util.h:349
static UniquePersistent< V > Release(PersistentContainerValue v)
Return a container value as UniquePersistent and make sure the weak callback is properly disposed of.
Definition: v8-util.h:338

References v8::PersistentValueMap< K, V, Traits >::impl_, v8::PersistentValueMap< K, V, Traits >::isolate_, and v8::PersistentValueMap< K, V, Traits >::Release().

Referenced by v8::PersistentValueMap< K, V, Traits >::~PersistentValueMap().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ClearAndLeak()

template<typename K , typename V , typename Traits >
static PersistentContainerValue v8::PersistentValueMap< K, V, Traits >::ClearAndLeak ( UniquePersistent< V > *  persistent)
inlinestaticprivate

Definition at line 321 of file v8-util.h.

322  {
323  V* v = persistent->val_;
324  persistent->val_ = 0;
325  return reinterpret_cast<PersistentContainerValue>(v);
326  }
uintptr_t PersistentContainerValue
Definition: v8-util.h:21

References v8::PersistentBase< T >::val_.

Referenced by v8::PersistentValueMap< K, V, Traits >::SetUnique().

+ Here is the caller graph for this function:

◆ Contains()

template<typename K , typename V , typename Traits >
bool v8::PersistentValueMap< K, V, Traits >::Contains ( const K &  key)
inline

Check whether a value is contained in the map.

Definition at line 147 of file v8-util.h.

147  {
148  return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
149  }
static const uintptr_t kPersistentContainerNotFound
Definition: v8-util.h:22

References v8::PersistentValueMap< K, V, Traits >::impl_, and v8::kPersistentContainerNotFound.

◆ FromVal()

template<typename K , typename V , typename Traits >
static V* v8::PersistentValueMap< K, V, Traits >::FromVal ( PersistentContainerValue  v)
inlinestaticprivate

Definition at line 307 of file v8-util.h.

307  {
308  return reinterpret_cast<V*>(v);
309  }

Referenced by v8::PersistentValueMap< K, V, Traits >::Get(), v8::PersistentValueMap< K, V, Traits >::PersistentValueReference::NewLocal(), v8::PersistentValueMap< K, V, Traits >::Release(), v8::PersistentValueMap< K, V, Traits >::SetReference(), and v8::PersistentValueMap< K, V, Traits >::SetReturnValueFromVal().

+ Here is the caller graph for this function:

◆ Get()

template<typename K , typename V , typename Traits >
Local<V> v8::PersistentValueMap< K, V, Traits >::Get ( const K &  key)
inline

Get value stored in map.

Definition at line 140 of file v8-util.h.

140  {
141  return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
142  }
static Local< T > New(Isolate *isolate, Handle< T > that)
Create a local handle for the content of another handle.
Definition: v8.h:5987
static V * FromVal(PersistentContainerValue v)
Definition: v8-util.h:307

References v8::PersistentValueMap< K, V, Traits >::FromVal(), v8::PersistentValueMap< K, V, Traits >::impl_, v8::PersistentValueMap< K, V, Traits >::isolate_, and v8::Local< T >::New().

+ Here is the call graph for this function:

◆ GetIsolate()

template<typename K , typename V , typename Traits >
Isolate* v8::PersistentValueMap< K, V, Traits >::GetIsolate ( )
inline

Definition at line 125 of file v8-util.h.

125 { return isolate_; }

References v8::PersistentValueMap< K, V, Traits >::isolate_.

Referenced by v8::PersistentValueMap< K, V, Traits >::SetReference().

+ Here is the caller graph for this function:

◆ GetReference()

template<typename K , typename V , typename Traits >
PersistentValueReference v8::PersistentValueMap< K, V, Traits >::GetReference ( const K &  key)
inline

Get a reference to a map value.

This enables fast, repeated access to a value stored in the map while the map remains unchanged.

Careful: This is potentially unsafe, so please use with care. The value will become invalid if the value for this key changes in the underlying map, as a result of Set or Remove for the same key; as a result of the weak callback for the same key; or as a result of calling Clear() or destruction of the map.

Definition at line 262 of file v8-util.h.

262  {
263  return PersistentValueReference(Traits::Get(&impl_, key));
264  }

References v8::PersistentValueMap< K, V, Traits >::impl_.

◆ IsWeak()

template<typename K , typename V , typename Traits >
bool v8::PersistentValueMap< K, V, Traits >::IsWeak ( )
inline

Return whether the map holds weak persistents.

Definition at line 135 of file v8-util.h.

135 { return Traits::kCallbackType != kNotWeak; }
@ kNotWeak
Definition: v8-util.h:24

References v8::kNotWeak.

◆ Leak()

template<typename K , typename V , typename Traits >
static PersistentContainerValue v8::PersistentValueMap< K, V, Traits >::Leak ( UniquePersistent< V > *  persistent)
inlinestaticprivate

Definition at line 328 of file v8-util.h.

329  {
330  return reinterpret_cast<PersistentContainerValue>(persistent->val_);
331  }

References v8::PersistentBase< T >::val_.

Referenced by v8::PersistentValueMap< K, V, Traits >::Set().

+ Here is the caller graph for this function:

◆ operator=()

template<typename K , typename V , typename Traits >
void v8::PersistentValueMap< K, V, Traits >::operator= ( PersistentValueMap< K, V, Traits > &  )
private

◆ Release()

template<typename K , typename V , typename Traits >
static UniquePersistent<V> v8::PersistentValueMap< K, V, Traits >::Release ( PersistentContainerValue  v)
inlinestaticprivate

Return a container value as UniquePersistent and make sure the weak callback is properly disposed of.

All remove functionality should go through this.

Definition at line 338 of file v8-util.h.

338  {
339  UniquePersistent<V> p;
340  p.val_ = FromVal(v);
341  if (Traits::kCallbackType != kNotWeak && p.IsWeak()) {
342  Traits::DisposeCallbackData(
343  p.template ClearWeak<typename Traits::WeakCallbackDataType>());
344  }
345  return p.Pass();
346  }

References v8::PersistentValueMap< K, V, Traits >::FromVal(), v8::PersistentBase< T >::IsWeak(), v8::kNotWeak, v8::UniquePersistent< T >::Pass(), and v8::PersistentBase< T >::val_.

Referenced by v8::PersistentValueMap< K, V, Traits >::Clear(), v8::PersistentValueMap< K, V, Traits >::Remove(), and v8::PersistentValueMap< K, V, Traits >::SetUnique().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Remove()

template<typename K , typename V , typename Traits >
UniquePersistent<V> v8::PersistentValueMap< K, V, Traits >::Remove ( const K &  key)
inline

Return value for key and remove it from the map.

Definition at line 190 of file v8-util.h.

190  {
191  return Release(Traits::Remove(&impl_, key)).Pass();
192  }

References v8::PersistentValueMap< K, V, Traits >::impl_, and v8::PersistentValueMap< K, V, Traits >::Release().

Referenced by v8::PersistentValueMap< K, V, Traits >::WeakCallback().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Set() [1/3]

template<typename K , typename V , typename Traits >
UniquePersistent<V> v8::PersistentValueMap< K, V, Traits >::Set ( const K &  key,
Local< V value 
)
inline

Put value into map.

Depending on Traits::kIsWeak, the value will be held by the map strongly or weakly. Returns old value as UniquePersistent.

Definition at line 175 of file v8-util.h.

175  {
176  UniquePersistent<V> persistent(isolate_, value);
177  return SetUnique(key, &persistent);
178  }
UniquePersistent< V > SetUnique(const K &key, UniquePersistent< V > *persistent)
Put the value into the map, and set the 'weak' callback when demanded by the Traits class.
Definition: v8-util.h:284

References v8::PersistentValueMap< K, V, Traits >::isolate_, and v8::PersistentValueMap< K, V, Traits >::SetUnique().

+ Here is the call graph for this function:

◆ Set() [2/3]

template<typename K , typename V , typename Traits >
UniquePersistent<V> v8::PersistentValueMap< K, V, Traits >::Set ( const K &  key,
UniquePersistent< V value 
)
inline

Put value into map, like Set(const K&, Local<V>).

Definition at line 183 of file v8-util.h.

183  {
184  return SetUnique(key, &value);
185  }

References v8::PersistentValueMap< K, V, Traits >::SetUnique().

+ Here is the call graph for this function:

◆ Set() [3/3]

template<typename K , typename V , typename Traits >
UniquePersistent<V> v8::PersistentValueMap< K, V, Traits >::Set ( const K &  key,
UniquePersistent< V value,
PersistentValueReference reference 
)
inline

Put a value into the map and update the reference.

Restrictions of GetReference apply here as well.

Definition at line 270 of file v8-util.h.

271  {
272  *reference = Leak(&value);
273  return SetUnique(key, &value);
274  }
static PersistentContainerValue Leak(UniquePersistent< V > *persistent)
Definition: v8-util.h:328

References v8::PersistentValueMap< K, V, Traits >::Leak(), and v8::PersistentValueMap< K, V, Traits >::SetUnique().

+ Here is the call graph for this function:

◆ SetReference()

template<typename K , typename V , typename Traits >
void v8::PersistentValueMap< K, V, Traits >::SetReference ( const K &  key,
const Persistent< Object > &  parent 
)
inline

Call Isolate::SetReference with the given parent and the map value.

Definition at line 163 of file v8-util.h.

164  {
166  reinterpret_cast<internal::Object**>(parent.val_),
167  reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))));
168  }
void SetReference(const Persistent< T > &parent, const Persistent< S > &child)
Allows the host application to declare implicit references from an object to another object.
Definition: v8.h:6930
Isolate * GetIsolate()
Definition: v8-util.h:125
kSerializedDataOffset Object
Definition: objects-inl.h:5322

References v8::PersistentValueMap< K, V, Traits >::FromVal(), v8::PersistentValueMap< K, V, Traits >::GetIsolate(), v8::PersistentValueMap< K, V, Traits >::impl_, v8::Isolate::SetReference(), and v8::PersistentBase< T >::val_.

+ Here is the call graph for this function:

◆ SetReturnValue()

template<typename K , typename V , typename Traits >
bool v8::PersistentValueMap< K, V, Traits >::SetReturnValue ( const K &  key,
ReturnValue< Value returnValue 
)
inline

Get value stored in map and set it in returnValue.

Return true if a value was found.

Definition at line 155 of file v8-util.h.

156  {
157  return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
158  }
static bool SetReturnValueFromVal(ReturnValue< Value > *returnValue, PersistentContainerValue value)
Definition: v8-util.h:311

References v8::PersistentValueMap< K, V, Traits >::impl_, and v8::PersistentValueMap< K, V, Traits >::SetReturnValueFromVal().

+ Here is the call graph for this function:

◆ SetReturnValueFromVal()

template<typename K , typename V , typename Traits >
static bool v8::PersistentValueMap< K, V, Traits >::SetReturnValueFromVal ( ReturnValue< Value > *  returnValue,
PersistentContainerValue  value 
)
inlinestaticprivate

Definition at line 311 of file v8-util.h.

312  {
313  bool hasValue = value != kPersistentContainerNotFound;
314  if (hasValue) {
315  returnValue->SetInternal(
316  *reinterpret_cast<internal::Object**>(FromVal(value)));
317  }
318  return hasValue;
319  }

References v8::PersistentValueMap< K, V, Traits >::FromVal(), v8::kPersistentContainerNotFound, and v8::ReturnValue< T >::SetInternal().

Referenced by v8::PersistentValueMap< K, V, Traits >::SetReturnValue(), and v8::PersistentValueMap< K, V, Traits >::PersistentValueReference::SetReturnValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetUnique()

template<typename K , typename V , typename Traits >
UniquePersistent<V> v8::PersistentValueMap< K, V, Traits >::SetUnique ( const K &  key,
UniquePersistent< V > *  persistent 
)
inlineprivate

Put the value into the map, and set the 'weak' callback when demanded by the Traits class.

Definition at line 284 of file v8-util.h.

284  {
285  if (Traits::kCallbackType != kNotWeak) {
286  Local<V> value(Local<V>::New(isolate_, *persistent));
287  persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
288  Traits::WeakCallbackParameter(this, key, value), WeakCallback);
289  }
290  PersistentContainerValue old_value =
291  Traits::Set(&impl_, key, ClearAndLeak(persistent));
292  return Release(old_value).Pass();
293  }
static void WeakCallback(const WeakCallbackData< V, typename Traits::WeakCallbackDataType > &data)
Definition: v8-util.h:295
static PersistentContainerValue ClearAndLeak(UniquePersistent< V > *persistent)
Definition: v8-util.h:321

References v8::PersistentValueMap< K, V, Traits >::ClearAndLeak(), v8::PersistentValueMap< K, V, Traits >::impl_, v8::PersistentValueMap< K, V, Traits >::isolate_, v8::kNotWeak, v8::PersistentValueMap< K, V, Traits >::Release(), and v8::PersistentValueMap< K, V, Traits >::WeakCallback().

Referenced by v8::PersistentValueMap< K, V, Traits >::Set().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Size()

template<typename K , typename V , typename Traits >
size_t v8::PersistentValueMap< K, V, Traits >::Size ( )
inline

Return size of the map.

Definition at line 130 of file v8-util.h.

130 { return Traits::Size(&impl_); }

References v8::PersistentValueMap< K, V, Traits >::impl_.

◆ WeakCallback()

template<typename K , typename V , typename Traits >
static void v8::PersistentValueMap< K, V, Traits >::WeakCallback ( const WeakCallbackData< V, typename Traits::WeakCallbackDataType > &  data)
inlinestaticprivate

Definition at line 295 of file v8-util.h.

296  {
297  if (Traits::kCallbackType != kNotWeak) {
298  PersistentValueMap<K, V, Traits>* persistentValueMap =
299  Traits::MapFromWeakCallbackData(data);
300  K key = Traits::KeyFromWeakCallbackData(data);
301  Traits::Dispose(data.GetIsolate(),
302  persistentValueMap->Remove(key).Pass(), key);
303  Traits::DisposeCallbackData(data.GetParameter());
304  }
305  }

References v8::WeakCallbackData< T, P >::GetIsolate(), v8::WeakCallbackData< T, P >::GetParameter(), v8::kNotWeak, and v8::PersistentValueMap< K, V, Traits >::Remove().

Referenced by v8::PersistentValueMap< K, V, Traits >::SetUnique().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ impl_

◆ isolate_


The documentation for this class was generated from the following file: