]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/Weak.h
0938249b8b0223247d4824e2c06aedab1bd9c671
[apple/javascriptcore.git] / heap / Weak.h
1 /*
2 * Copyright (C) 2009, 2012 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 #ifndef Weak_h
27 #define Weak_h
28
29 #include <wtf/Assertions.h>
30 #include "PassWeak.h"
31 #include "WeakSetInlines.h"
32
33 namespace JSC {
34
35 template<typename T> class Weak : public WeakImplAccessor<Weak<T>, T> {
36 WTF_MAKE_NONCOPYABLE(Weak);
37 public:
38 friend class WeakImplAccessor<Weak<T>, T>;
39 typedef typename WeakImplAccessor<Weak<T>, T>::GetType GetType;
40
41 Weak();
42 Weak(std::nullptr_t);
43 Weak(GetType, WeakHandleOwner* = 0, void* context = 0);
44
45 enum HashTableDeletedValueTag { HashTableDeletedValue };
46 bool isHashTableDeletedValue() const;
47 Weak(HashTableDeletedValueTag);
48
49 template<typename U> Weak(const PassWeak<U>&);
50
51 ~Weak();
52
53 void swap(Weak&);
54 Weak& operator=(const PassWeak<T>&);
55
56 bool operator!() const;
57
58 // This conversion operator allows implicit conversion to bool but not to other integer types.
59 typedef JSValue (HandleBase::*UnspecifiedBoolType);
60 operator UnspecifiedBoolType*() const;
61
62 PassWeak<T> release();
63 void clear();
64
65 private:
66 static WeakImpl* hashTableDeletedValue();
67
68 WeakImpl* m_impl;
69 };
70
71 template<typename T> inline Weak<T>::Weak()
72 : m_impl(0)
73 {
74 }
75
76 template<typename T> inline Weak<T>::Weak(std::nullptr_t)
77 : m_impl(0)
78 {
79 }
80
81 template<typename T> inline Weak<T>::Weak(typename Weak<T>::GetType getType, WeakHandleOwner* weakOwner, void* context)
82 : m_impl(getType ? WeakSet::allocate(getType, weakOwner, context) : 0)
83 {
84 }
85
86 template<typename T> inline bool Weak<T>::isHashTableDeletedValue() const
87 {
88 return m_impl == hashTableDeletedValue();
89 }
90
91 template<typename T> inline Weak<T>::Weak(typename Weak<T>::HashTableDeletedValueTag)
92 : m_impl(hashTableDeletedValue())
93 {
94 }
95
96 template<typename T> template<typename U> inline Weak<T>::Weak(const PassWeak<U>& other)
97 : m_impl(other.leakImpl())
98 {
99 }
100
101 template<typename T> inline Weak<T>::~Weak()
102 {
103 clear();
104 }
105
106 template<class T> inline void swap(Weak<T>& a, Weak<T>& b)
107 {
108 a.swap(b);
109 }
110
111 template<typename T> inline void Weak<T>::swap(Weak& other)
112 {
113 std::swap(m_impl, other.m_impl);
114 }
115
116 template<typename T> inline Weak<T>& Weak<T>::operator=(const PassWeak<T>& o)
117 {
118 clear();
119 m_impl = o.leakImpl();
120 return *this;
121 }
122
123 template<typename T> inline bool Weak<T>::operator!() const
124 {
125 return !m_impl || !m_impl->jsValue() || m_impl->state() != WeakImpl::Live;
126 }
127
128 template<typename T> inline Weak<T>::operator UnspecifiedBoolType*() const
129 {
130 return reinterpret_cast<UnspecifiedBoolType*>(!!*this);
131 }
132
133 template<typename T> inline PassWeak<T> Weak<T>::release()
134 {
135 PassWeak<T> tmp = adoptWeak<T>(m_impl);
136 m_impl = 0;
137 return tmp;
138 }
139
140 template<typename T> inline void Weak<T>::clear()
141 {
142 if (!m_impl)
143 return;
144 WeakSet::deallocate(m_impl);
145 m_impl = 0;
146 }
147
148 template<typename T> inline WeakImpl* Weak<T>::hashTableDeletedValue()
149 {
150 return reinterpret_cast<WeakImpl*>(-1);
151 }
152
153 } // namespace JSC
154
155 namespace WTF {
156
157 template<typename T> struct VectorTraits<JSC::Weak<T> > : SimpleClassVectorTraits {
158 static const bool canCompareWithMemcmp = false;
159 };
160
161 template<typename T> struct HashTraits<JSC::Weak<T> > : SimpleClassHashTraits<JSC::Weak<T> > {
162 typedef JSC::Weak<T> StorageType;
163
164 typedef std::nullptr_t EmptyValueType;
165 static EmptyValueType emptyValue() { return nullptr; }
166
167 typedef JSC::PassWeak<T> PassInType;
168 static void store(PassInType value, StorageType& storage) { storage = value; }
169
170 typedef JSC::PassWeak<T> PassOutType;
171 static PassOutType passOut(StorageType& value) { return value.release(); }
172 static PassOutType passOut(EmptyValueType) { return PassOutType(); }
173
174 typedef typename StorageType::GetType PeekType;
175 static PeekType peek(const StorageType& value) { return value.get(); }
176 static PeekType peek(EmptyValueType) { return PeekType(); }
177 };
178
179 }
180
181 #endif // Weak_h