2 * Copyright (C) 2009, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include <wtf/Assertions.h>
31 #include "WeakSetInlines.h"
35 template<typename T
> class Weak
: public WeakImplAccessor
<Weak
<T
>, T
> {
36 WTF_MAKE_NONCOPYABLE(Weak
);
38 friend class WeakImplAccessor
<Weak
<T
>, T
>;
39 typedef typename WeakImplAccessor
<Weak
<T
>, T
>::GetType GetType
;
43 Weak(GetType
, WeakHandleOwner
* = 0, void* context
= 0);
45 enum HashTableDeletedValueTag
{ HashTableDeletedValue
};
46 bool isHashTableDeletedValue() const;
47 Weak(HashTableDeletedValueTag
);
49 template<typename U
> Weak(const PassWeak
<U
>&);
54 Weak
& operator=(const PassWeak
<T
>&);
56 bool operator!() const;
58 // This conversion operator allows implicit conversion to bool but not to other integer types.
59 typedef JSValue (HandleBase::*UnspecifiedBoolType
);
60 operator UnspecifiedBoolType
*() const;
62 PassWeak
<T
> release();
66 static WeakImpl
* hashTableDeletedValue();
71 template<typename T
> inline Weak
<T
>::Weak()
76 template<typename T
> inline Weak
<T
>::Weak(std::nullptr_t
)
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)
86 template<typename T
> inline bool Weak
<T
>::isHashTableDeletedValue() const
88 return m_impl
== hashTableDeletedValue();
91 template<typename T
> inline Weak
<T
>::Weak(typename Weak
<T
>::HashTableDeletedValueTag
)
92 : m_impl(hashTableDeletedValue())
96 template<typename T
> template<typename U
> inline Weak
<T
>::Weak(const PassWeak
<U
>& other
)
97 : m_impl(other
.leakImpl())
101 template<typename T
> inline Weak
<T
>::~Weak()
106 template<class T
> inline void swap(Weak
<T
>& a
, Weak
<T
>& b
)
111 template<typename T
> inline void Weak
<T
>::swap(Weak
& other
)
113 std::swap(m_impl
, other
.m_impl
);
116 template<typename T
> inline Weak
<T
>& Weak
<T
>::operator=(const PassWeak
<T
>& o
)
119 m_impl
= o
.leakImpl();
123 template<typename T
> inline bool Weak
<T
>::operator!() const
125 return !m_impl
|| !m_impl
->jsValue() || m_impl
->state() != WeakImpl::Live
;
128 template<typename T
> inline Weak
<T
>::operator UnspecifiedBoolType
*() const
130 return reinterpret_cast<UnspecifiedBoolType
*>(!!*this);
133 template<typename T
> inline PassWeak
<T
> Weak
<T
>::release()
135 PassWeak
<T
> tmp
= adoptWeak
<T
>(m_impl
);
140 template<typename T
> inline void Weak
<T
>::clear()
144 WeakSet::deallocate(m_impl
);
148 template<typename T
> inline WeakImpl
* Weak
<T
>::hashTableDeletedValue()
150 return reinterpret_cast<WeakImpl
*>(-1);
157 template<typename T
> struct VectorTraits
<JSC::Weak
<T
> > : SimpleClassVectorTraits
{
158 static const bool canCompareWithMemcmp
= false;
161 template<typename T
> struct HashTraits
<JSC::Weak
<T
> > : SimpleClassHashTraits
<JSC::Weak
<T
> > {
162 typedef JSC::Weak
<T
> StorageType
;
164 typedef std::nullptr_t EmptyValueType
;
165 static EmptyValueType
emptyValue() { return nullptr; }
167 typedef JSC::PassWeak
<T
> PassInType
;
168 static void store(PassInType value
, StorageType
& storage
) { storage
= value
; }
170 typedef JSC::PassWeak
<T
> PassOutType
;
171 static PassOutType
passOut(StorageType
& value
) { return value
.release(); }
172 static PassOutType
passOut(EmptyValueType
) { return PassOutType(); }
174 typedef typename
StorageType::GetType PeekType
;
175 static PeekType
peek(const StorageType
& value
) { return value
.get(); }
176 static PeekType
peek(EmptyValueType
) { return PeekType(); }