2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef V8PersistentValueMap_h
32 #define V8PersistentValueMap_h
34 #include "wtf/HashMap.h"
41 * A Traits class for v8::PersistentValueMap that uses wtf/HashMap as a
44 * The parameter is_weak will determine whether the references are 'weak'.
45 * If so, entries will be removed from the map as the weak references are
48 template<class KeyType, class ValueType, bool is_weak>
49 class V8PersistentValueMapTraits {
52 typedef HashMap<KeyType, v8::PersistentContainerValue> Impl;
53 typedef typename Impl::iterator Iterator;
54 static size_t Size(const Impl* impl) { return impl->size(); }
55 static bool Empty(Impl* impl) { return impl->isEmpty(); }
56 static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
57 static Iterator Begin(Impl* impl) { return impl->begin(); }
58 static Iterator End(Impl* impl) { return impl->end(); }
59 static v8::PersistentContainerValue Value(Iterator& iter)
63 static KeyType Key(Iterator& iter) { return iter->key; }
64 static v8::PersistentContainerValue Set(
65 Impl* impl, KeyType key, v8::PersistentContainerValue value)
67 v8::PersistentContainerValue oldValue = Get(impl, key);
68 impl->set(key, value);
71 static v8::PersistentContainerValue Get(const Impl* impl, KeyType key)
73 return impl->get(key);
76 static v8::PersistentContainerValue Remove(Impl* impl, KeyType key)
78 return impl->take(key);
82 static const v8::PersistentContainerCallbackType kCallbackType = is_weak ? v8::kWeak : v8::kNotWeak;
83 typedef v8::PersistentValueMap<KeyType, ValueType, V8PersistentValueMapTraits<KeyType, ValueType, is_weak> > MapType;
85 typedef void WeakCallbackDataType;
87 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType key, const v8::Local<ValueType>& value)
92 static void DisposeCallbackData(WeakCallbackDataType* callbackData)
96 static MapType* MapFromWeakCallbackData(
97 const v8::WeakCallbackData<ValueType, WeakCallbackDataType>& data)
102 static KeyType KeyFromWeakCallbackData(
103 const v8::WeakCallbackData<ValueType, WeakCallbackDataType>& data)
109 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<ValueType> value, KeyType key) { }
113 * A map for safely storing persistent V8 values, based on
114 * v8::PersistentValueMap.
116 * If is_weak is set, values will be held weakly and map entries will be
117 * removed as their values are being collected.
119 template<class KeyType, class ValueType, bool is_weak = true>
120 class V8PersistentValueMap : public v8::PersistentValueMap<KeyType, ValueType, V8PersistentValueMapTraits<KeyType, ValueType, is_weak> > {
122 typedef V8PersistentValueMapTraits<KeyType, ValueType, is_weak> Traits;
123 explicit V8PersistentValueMap(v8::Isolate* isolate) : v8::PersistentValueMap<KeyType, ValueType, Traits>(isolate) { }
126 } // namespace WebCore
128 #endif // V8PersistentValueMap_h