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