1 | /* |
2 | * Copyright (C) 2017, 2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/Algorithms.h> |
29 | #include <wtf/HashSet.h> |
30 | #include <wtf/HashTraits.h> |
31 | #include <wtf/WeakPtr.h> |
32 | |
33 | namespace WTF { |
34 | |
35 | template <typename T> |
36 | class WeakHashSet { |
37 | public: |
38 | typedef HashSet<Ref<WeakReference<T>>> WeakReferenceSet; |
39 | |
40 | class WeakHashSetConstIterator : public std::iterator<std::forward_iterator_tag, T, std::ptrdiff_t, const T*, const T&> { |
41 | private: |
42 | WeakHashSetConstIterator(const WeakReferenceSet& set, typename WeakReferenceSet::const_iterator position) |
43 | : m_position(position), m_endPosition(set.end()) |
44 | { |
45 | skipEmptyBuckets(); |
46 | } |
47 | |
48 | public: |
49 | T* get() const { return m_position->get().get(); } |
50 | T& operator*() const { return *get(); } |
51 | T* operator->() const { return get(); } |
52 | |
53 | WeakHashSetConstIterator& operator++() |
54 | { |
55 | ASSERT(m_position != m_endPosition); |
56 | ++m_position; |
57 | skipEmptyBuckets(); |
58 | return *this; |
59 | } |
60 | |
61 | void skipEmptyBuckets() |
62 | { |
63 | while (m_position != m_endPosition && !m_position->get().get()) |
64 | ++m_position; |
65 | } |
66 | |
67 | bool operator==(const WeakHashSetConstIterator& other) const |
68 | { |
69 | return m_position == other.m_position; |
70 | } |
71 | |
72 | bool operator!=(const WeakHashSetConstIterator& other) const |
73 | { |
74 | return m_position != other.m_position; |
75 | } |
76 | |
77 | private: |
78 | template <typename> friend class WeakHashSet; |
79 | |
80 | typename WeakReferenceSet::const_iterator m_position; |
81 | typename WeakReferenceSet::const_iterator m_endPosition; |
82 | }; |
83 | typedef WeakHashSetConstIterator const_iterator; |
84 | |
85 | WeakHashSet() { } |
86 | |
87 | const_iterator begin() const { return WeakHashSetConstIterator(m_set, m_set.begin()); } |
88 | const_iterator end() const { return WeakHashSetConstIterator(m_set, m_set.end()); } |
89 | |
90 | template <typename U> |
91 | void add(const U& value) |
92 | { |
93 | m_set.add(*makeWeakPtr<T>(const_cast<U&>(value)).m_ref); |
94 | } |
95 | |
96 | template <typename U> |
97 | bool remove(const U& value) |
98 | { |
99 | auto* weakReference = weak_reference_downcast<T>(value.weakPtrFactory().m_ref.get()); |
100 | if (!weakReference) |
101 | return false; |
102 | return m_set.remove(weakReference); |
103 | } |
104 | |
105 | template <typename U> |
106 | bool contains(const U& value) const |
107 | { |
108 | auto* weakReference = weak_reference_downcast<T>(value.weakPtrFactory().m_ref.get()); |
109 | if (!weakReference) |
110 | return false; |
111 | return m_set.contains(weakReference); |
112 | } |
113 | |
114 | unsigned capacity() const { return m_set.capacity(); } |
115 | |
116 | bool computesEmpty() const |
117 | { |
118 | if (m_set.isEmpty()) |
119 | return true; |
120 | for (auto& value : m_set) { |
121 | if (value->get()) |
122 | return false; |
123 | } |
124 | return true; |
125 | } |
126 | |
127 | bool hasNullReferences() const |
128 | { |
129 | return WTF::anyOf(m_set, [] (auto& value) { return !value->get(); }); |
130 | } |
131 | |
132 | unsigned computeSize() const |
133 | { |
134 | const_cast<WeakReferenceSet&>(m_set).removeIf([] (auto& value) { return !value->get(); }); |
135 | return m_set.size(); |
136 | } |
137 | |
138 | #if ASSERT_DISABLED |
139 | void checkConsistency() const { } |
140 | #else |
141 | void checkConsistency() const { m_set.checkConsistency(); } |
142 | #endif |
143 | |
144 | private: |
145 | WeakReferenceSet m_set; |
146 | }; |
147 | |
148 | template<typename T> struct HashTraits<Ref<WeakReference<T>>> : RefHashTraits<WeakReference<T>> { |
149 | static const bool hasIsReleasedWeakValueFunction = true; |
150 | static bool isReleasedWeakValue(const Ref<WeakReference<T>>& value) |
151 | { |
152 | return !value.isHashTableDeletedValue() && !value.isHashTableEmptyValue() && !value.get().get(); |
153 | } |
154 | }; |
155 | |
156 | } // namespace WTF |
157 | |
158 | using WTF::WeakHashSet; |
159 | |