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<> struct HashTraits<Ref<WeakPtrImpl>> : RefHashTraits<WeakPtrImpl> { |
36 | static const bool hasIsReleasedWeakValueFunction = true; |
37 | static bool isReleasedWeakValue(const Ref<WeakPtrImpl>& value) |
38 | { |
39 | return !value.isHashTableDeletedValue() && !value.isHashTableEmptyValue() && !value.get(); |
40 | } |
41 | }; |
42 | |
43 | template <typename T> |
44 | class WeakHashSet { |
45 | public: |
46 | typedef HashSet<Ref<WeakPtrImpl>> WeakPtrImplSet; |
47 | |
48 | class WeakHashSetConstIterator : public std::iterator<std::forward_iterator_tag, T, std::ptrdiff_t, const T*, const T&> { |
49 | private: |
50 | WeakHashSetConstIterator(const WeakPtrImplSet& set, typename WeakPtrImplSet::const_iterator position) |
51 | : m_position(position), m_endPosition(set.end()) |
52 | { |
53 | skipEmptyBuckets(); |
54 | } |
55 | |
56 | public: |
57 | T* get() const { return static_cast<T*>((*m_position)->template get<T>()); } |
58 | T& operator*() const { return *get(); } |
59 | T* operator->() const { return get(); } |
60 | |
61 | WeakHashSetConstIterator& operator++() |
62 | { |
63 | ASSERT(m_position != m_endPosition); |
64 | ++m_position; |
65 | skipEmptyBuckets(); |
66 | return *this; |
67 | } |
68 | |
69 | void skipEmptyBuckets() |
70 | { |
71 | while (m_position != m_endPosition && !get()) |
72 | ++m_position; |
73 | } |
74 | |
75 | bool operator==(const WeakHashSetConstIterator& other) const |
76 | { |
77 | return m_position == other.m_position; |
78 | } |
79 | |
80 | bool operator!=(const WeakHashSetConstIterator& other) const |
81 | { |
82 | return m_position != other.m_position; |
83 | } |
84 | |
85 | private: |
86 | template <typename> friend class WeakHashSet; |
87 | |
88 | typename WeakPtrImplSet::const_iterator m_position; |
89 | typename WeakPtrImplSet::const_iterator m_endPosition; |
90 | }; |
91 | typedef WeakHashSetConstIterator const_iterator; |
92 | |
93 | WeakHashSet() { } |
94 | |
95 | const_iterator begin() const { return WeakHashSetConstIterator(m_set, m_set.begin()); } |
96 | const_iterator end() const { return WeakHashSetConstIterator(m_set, m_set.end()); } |
97 | |
98 | template <typename U> |
99 | void add(const U& value) |
100 | { |
101 | m_set.add(*makeWeakPtr<T>(const_cast<U&>(value)).m_impl); |
102 | } |
103 | |
104 | template <typename U> |
105 | bool remove(const U& value) |
106 | { |
107 | auto& weakPtrImpl = value.weakPtrFactory().m_impl; |
108 | if (!weakPtrImpl || !*weakPtrImpl) |
109 | return false; |
110 | return m_set.remove(*weakPtrImpl); |
111 | } |
112 | |
113 | template <typename U> |
114 | bool contains(const U& value) const |
115 | { |
116 | auto& weakPtrImpl = value.weakPtrFactory().m_impl; |
117 | if (!weakPtrImpl || !*weakPtrImpl) |
118 | return false; |
119 | return m_set.contains(*weakPtrImpl); |
120 | } |
121 | |
122 | unsigned capacity() const { return m_set.capacity(); } |
123 | |
124 | bool computesEmpty() const { return begin() == end(); } |
125 | |
126 | bool hasNullReferences() const |
127 | { |
128 | return WTF::anyOf(m_set, [] (auto& value) { return !value.get(); }); |
129 | } |
130 | |
131 | unsigned computeSize() const |
132 | { |
133 | const_cast<WeakPtrImplSet&>(m_set).removeIf([] (auto& value) { return !value.get(); }); |
134 | return m_set.size(); |
135 | } |
136 | |
137 | #if ASSERT_DISABLED |
138 | void checkConsistency() const { } |
139 | #else |
140 | void checkConsistency() const { m_set.checkConsistency(); } |
141 | #endif |
142 | |
143 | private: |
144 | WeakPtrImplSet m_set; |
145 | }; |
146 | |
147 | } // namespace WTF |
148 | |
149 | using WTF::WeakHashSet; |
150 | |