]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/WeakInlines.h
2 * Copyright (C) 2009, 2012, 2013 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.
30 #include "WeakSetInlines.h"
31 #include <wtf/Assertions.h>
32 #include <wtf/HashTraits.h>
36 template<typename T
> inline Weak
<T
>::Weak(T
* cell
, WeakHandleOwner
* weakOwner
, void* context
)
37 : m_impl(cell
? WeakSet::allocate(cell
, weakOwner
, context
) : 0)
41 template<typename T
> inline bool Weak
<T
>::isHashTableDeletedValue() const
43 return m_impl
== hashTableDeletedValue();
46 template<typename T
> inline Weak
<T
>::Weak(typename Weak
<T
>::HashTableDeletedValueTag
)
47 : m_impl(hashTableDeletedValue())
51 template<typename T
> template<typename U
> inline Weak
<T
>::Weak(const PassWeak
<U
>& other
)
52 : m_impl(other
.leakImpl())
56 template<class T
> inline void swap(Weak
<T
>& a
, Weak
<T
>& b
)
61 template<typename T
> inline void Weak
<T
>::swap(Weak
& other
)
63 std::swap(m_impl
, other
.m_impl
);
66 template<typename T
> inline Weak
<T
>& Weak
<T
>::operator=(const PassWeak
<T
>& o
)
69 m_impl
= o
.leakImpl();
73 template<typename T
> inline T
* Weak
<T
>::operator->() const
75 ASSERT(m_impl
&& m_impl
->state() == WeakImpl::Live
);
76 return jsCast
<T
*>(m_impl
->jsValue().asCell());
79 template<typename T
> inline T
& Weak
<T
>::operator*() const
81 ASSERT(m_impl
&& m_impl
->state() == WeakImpl::Live
);
82 return *jsCast
<T
*>(m_impl
->jsValue().asCell());
85 template<typename T
> inline T
* Weak
<T
>::get() const
87 if (!m_impl
|| m_impl
->state() != WeakImpl::Live
)
89 return jsCast
<T
*>(m_impl
->jsValue().asCell());
92 template<typename T
> inline bool Weak
<T
>::was(T
* other
) const
94 return jsCast
<T
*>(m_impl
->jsValue().asCell()) == other
;
97 template<typename T
> inline bool Weak
<T
>::operator!() const
99 return !m_impl
|| !m_impl
->jsValue() || m_impl
->state() != WeakImpl::Live
;
102 template<typename T
> inline Weak
<T
>::operator UnspecifiedBoolType
*() const
104 return reinterpret_cast<UnspecifiedBoolType
*>(!!*this);
107 template<typename T
> inline PassWeak
<T
> Weak
<T
>::release()
109 PassWeak
<T
> tmp
= adoptWeak
<T
>(m_impl
);
114 template<typename T
> inline WeakImpl
* Weak
<T
>::hashTableDeletedValue()
116 return reinterpret_cast<WeakImpl
*>(-1);
119 template <typename T
> inline bool operator==(const Weak
<T
>& lhs
, const Weak
<T
>& rhs
)
121 return lhs
.get() == rhs
.get();
124 // This function helps avoid modifying a weak table while holding an iterator into it. (Object allocation
125 // can run a finalizer that modifies the table. We avoid that by requiring a pre-constructed object as our value.)
126 template<typename Map
, typename Key
, typename Value
> inline void weakAdd(Map
& map
, const Key
& key
, Value value
)
128 ASSERT(!map
.get(key
));
129 map
.set(key
, value
); // The table may still have a zombie for value.
132 template<typename Map
, typename Key
, typename Value
> inline void weakRemove(Map
& map
, const Key
& key
, Value value
)
134 typename
Map::iterator it
= map
.find(key
);
135 ASSERT_UNUSED(value
, value
);
136 ASSERT(it
!= map
.end());
137 ASSERT(it
->value
.was(value
));
142 template<typename T
> inline void weakClear(Weak
<T
>& weak
, T
* cell
)
144 ASSERT_UNUSED(cell
, cell
);
145 ASSERT(weak
.was(cell
));
154 template<typename T
> struct VectorTraits
<JSC::Weak
<T
> > : SimpleClassVectorTraits
{
155 static const bool canCompareWithMemcmp
= false;
158 template<typename T
> struct HashTraits
<JSC::Weak
<T
> > : SimpleClassHashTraits
<JSC::Weak
<T
> > {
159 typedef JSC::Weak
<T
> StorageType
;
161 typedef std::nullptr_t EmptyValueType
;
162 static EmptyValueType
emptyValue() { return nullptr; }
164 typedef JSC::PassWeak
<T
> PassInType
;
165 static void store(PassInType value
, StorageType
& storage
) { storage
= value
; }
167 typedef JSC::PassWeak
<T
> PassOutType
;
168 static PassOutType
passOut(StorageType
& value
) { return value
.release(); }
169 static PassOutType
passOut(EmptyValueType
) { return PassOutType(); }
172 static PeekType
peek(const StorageType
& value
) { return value
.get(); }
173 static PeekType
peek(EmptyValueType
) { return PeekType(); }
178 #endif // WeakInlines_h