| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef ManufacturerDataMap_h |
| 6 #define ManufacturerDataMap_h |
| 7 |
| 8 #include "bindings/core/v8/Maplike.h" |
| 9 #include "bindings/core/v8/ScriptWrappable.h" |
| 10 #include "core/dom/DOMArrayBuffer.h" |
| 11 #include "platform/heap/Handle.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 class ExceptionState; |
| 16 class ScriptState; |
| 17 |
| 18 class ManufacturerDataMap final |
| 19 : public GarbageCollected<ManufacturerDataMap> |
| 20 , public ScriptWrappable |
| 21 , public Maplike<unsigned short, RefPtr<DOMArrayBuffer>> { |
| 22 DEFINE_WRAPPERTYPEINFO(); |
| 23 public: |
| 24 using MapEntry = std::pair<unsigned short, RefPtr<DOMArrayBuffer>>; |
| 25 |
| 26 explicit ManufacturerDataMap() |
| 27 : m_entries() |
| 28 , m_keys() |
| 29 { |
| 30 } |
| 31 |
| 32 // IDL attributes / methods |
| 33 size_t size() const { return m_keys.size(); }; |
| 34 |
| 35 DEFINE_INLINE_TRACE() |
| 36 { |
| 37 visitor->trace(m_keys); |
| 38 visitor->trace(m_entries); |
| 39 } |
| 40 |
| 41 private: |
| 42 // Required by the Iterable interface. |
| 43 IterationSource* startIteration(ScriptState*, ExceptionState&) override |
| 44 { |
| 45 return new MapIterationSource(this); |
| 46 } |
| 47 |
| 48 // Required by the Maplike interface. |
| 49 bool getMapEntry(ScriptState*, const unsigned short& key, RefPtr<DOMArrayBuf
fer>& value, ExceptionState&) override |
| 50 { |
| 51 auto it = m_keys.find(key); |
| 52 if (it == m_keys.end()) { |
| 53 return false; |
| 54 } |
| 55 value = m_entries[it->value].second; |
| 56 return true; |
| 57 } |
| 58 |
| 59 // Map requires that the values are iterated over in the same order they |
| 60 // were added so we keep a vector of the entries. |
| 61 HeapVector<std::pair<unsigned short, RefPtr<DOMArrayBuffer>>> m_entries; |
| 62 // Map requires sublinear access times so we keep a map of Company |
| 63 // Identifier codes to the indices of the entries in the vector. |
| 64 HeapHashMap<unsigned short, int> m_keys; |
| 65 |
| 66 // Class used by Iterable to access the entries in the map. |
| 67 // Note: This template class relies on the fact that m_map.m_entries will |
| 68 // never be modified once it is created. |
| 69 class MapIterationSource final : public PairIterable<unsigned short, RefPtr<
DOMArrayBuffer>>::IterationSource { |
| 70 public: |
| 71 MapIterationSource(ManufacturerDataMap* map) |
| 72 : m_map(map) |
| 73 , m_current(0) |
| 74 { |
| 75 } |
| 76 |
| 77 // Required by the IterationSource interface. |
| 78 bool next(ScriptState*, unsigned short& key, RefPtr<DOMArrayBuffer>& val
ue, ExceptionState&) override |
| 79 { |
| 80 if (m_current >= m_map->size()) { |
| 81 return false; |
| 82 } |
| 83 const MapEntry& entry = m_map->m_entries[m_current]; |
| 84 key = entry.first; |
| 85 value = entry.second; |
| 86 m_current++; |
| 87 return true; |
| 88 } |
| 89 |
| 90 DEFINE_INLINE_TRACE() |
| 91 { |
| 92 visitor->trace(m_map); |
| 93 } |
| 94 private: |
| 95 // m_map needs to be kept alive while Javascript holds an iterator to it
, |
| 96 // so we make it a member. |
| 97 const Member<ManufacturerDataMap> m_map; |
| 98 size_t m_current; |
| 99 }; |
| 100 }; |
| 101 |
| 102 } // namespace blink |
| 103 |
| 104 #endif // ManufacturerDataMap_h |
| OLD | NEW |