Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 * | 35 * |
| 36 * C++11 embedders can use STL containers with UniquePersistent values, | 36 * C++11 embedders can use STL containers with UniquePersistent values, |
| 37 * but pre-C++11 does not support the required move semantic and hence | 37 * but pre-C++11 does not support the required move semantic and hence |
| 38 * may want these container classes. | 38 * may want these container classes. |
| 39 */ | 39 */ |
| 40 namespace v8 { | 40 namespace v8 { |
| 41 | 41 |
| 42 typedef uintptr_t PersistentContainerValue; | 42 typedef uintptr_t PersistentContainerValue; |
| 43 static const uintptr_t kPersistentContainerNotFound = 0; | 43 static const uintptr_t kPersistentContainerNotFound = 0; |
| 44 | 44 |
| 45 | |
| 46 namespace persistent_value_map_traits { | |
|
dcarney
2014/03/17 12:53:55
no need for a new namespace
vogelheim
2014/03/17 19:20:37
Removed.
| |
| 47 | |
| 48 template<typename K, typename V> | |
| 49 class WeakSTLMapTraits { | |
|
dcarney
2014/03/17 12:53:55
would be better as StdPersistentValueMap which it
vogelheim
2014/03/17 19:20:37
I couldn't really figure this one out. What I've d
| |
| 50 public: | |
| 51 // STL map & related: | |
| 52 typedef std::map<K, v8::PersistentContainerValue> Impl; | |
| 53 typedef typename Impl::iterator Iterator; | |
| 54 | |
| 55 static bool Empty(Impl* impl) { return impl->empty(); } | |
| 56 static size_t Size(Impl* impl) { return impl->size(); } | |
| 57 static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT | |
| 58 static Iterator Begin(Impl* impl) { return impl->begin(); } | |
| 59 static Iterator End(Impl* impl) { return impl->end(); } | |
| 60 static K Key(Iterator it) { return it->first; } | |
| 61 static v8::PersistentContainerValue Value(Iterator it) { return it->second; } | |
| 62 static v8::PersistentContainerValue Set(Impl* impl, K key, | |
| 63 v8::PersistentContainerValue value) { | |
| 64 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value)); | |
| 65 v8::PersistentContainerValue old_value = v8::kPersistentContainerNotFound; | |
| 66 if (!res.second) { | |
| 67 old_value = res.first->second; | |
| 68 res.first->second = value; | |
| 69 } | |
| 70 return old_value; | |
| 71 } | |
| 72 static v8::PersistentContainerValue Get(Impl* impl, K key) { | |
| 73 Iterator it = impl->find(key); | |
| 74 if (it == impl->end()) return v8::kPersistentContainerNotFound; | |
| 75 return it->second; | |
| 76 } | |
| 77 static v8::PersistentContainerValue Remove(Impl* impl, K key) { | |
| 78 Iterator it = impl->find(key); | |
| 79 if (it == impl->end()) return v8::kPersistentContainerNotFound; | |
| 80 v8::PersistentContainerValue value = it->second; | |
| 81 impl->erase(it); | |
| 82 return value; | |
| 83 } | |
| 84 | |
| 85 // Weak callback & friends: | |
| 86 static const bool kIsWeak = false; | |
|
dcarney
2014/03/17 12:53:55
this should be true for a class called WeakSTLMapT
vogelheim
2014/03/17 19:20:37
Yes. But actually, this class was misnamed, since
| |
| 87 typedef void WeakCallbackDataType; | |
| 88 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, | |
| 89 Impl* impl, K key) {} | |
| 90 static WeakCallbackDataType* WeakCallbackParameter(Impl* impl, const K& key, | |
| 91 Local<V> value) { | |
| 92 return NULL; | |
| 93 } | |
| 94 static Impl* ImplFromWeakCallbackData( | |
| 95 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | |
| 96 return NULL; | |
| 97 } | |
| 98 static K KeyFromWeakCallbackData( | |
| 99 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | |
| 100 return K(); | |
| 101 } | |
| 102 static void DisposeCallbackData(WeakCallbackDataType* data) { | |
| 103 } | |
| 104 }; | |
| 105 | |
| 106 } // namespace persistent_value_map_traits | |
| 107 | |
| 45 /** | 108 /** |
| 46 * A map wrapper that allows using UniquePersistent as a mapped value. | 109 * A map wrapper that allows using UniquePersistent as a mapped value. |
| 47 * C++11 embedders don't need this class, as they can use UniquePersistent | 110 * C++11 embedders don't need this class, as they can use UniquePersistent |
| 48 * directly in std containers. | 111 * directly in std containers. |
| 49 * | 112 * |
| 50 * The map relies on a backing map, whose type and accessors are described | 113 * The map relies on a backing map, whose type and accessors are described |
| 51 * by the Traits class. The backing map will handle values of type | 114 * by the Traits class. The backing map will handle values of type |
| 52 * PersistentContainerValue, with all conversion into and out of V8 | 115 * PersistentContainerValue, with all conversion into and out of V8 |
| 53 * handles being transparently handled by this class. | 116 * handles being transparently handled by this class. |
| 54 */ | 117 */ |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) { | 273 const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) { |
| 211 typename Traits::Impl* impl = Traits::ImplFromWeakCallbackData(data); | 274 typename Traits::Impl* impl = Traits::ImplFromWeakCallbackData(data); |
| 212 K key = Traits::KeyFromWeakCallbackData(data); | 275 K key = Traits::KeyFromWeakCallbackData(data); |
| 213 PersistentContainerValue value = Traits::Remove(impl, key); | 276 PersistentContainerValue value = Traits::Remove(impl, key); |
| 214 Traits::Dispose(data.GetIsolate(), Release(value).Pass(), impl, key); | 277 Traits::Dispose(data.GetIsolate(), Release(value).Pass(), impl, key); |
| 215 } | 278 } |
| 216 | 279 |
| 217 } // namespace v8 | 280 } // namespace v8 |
| 218 | 281 |
| 219 #endif // V8_UTIL_H_ | 282 #endif // V8_UTIL_H_ |
| OLD | NEW |