]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/PassWeak.h
2 * Copyright (C) 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "WeakSetInlines.h"
31 #include <wtf/Assertions.h>
32 #include <wtf/NullPtr.h>
33 #include <wtf/TypeTraits.h>
37 template<typename T
> class PassWeak
;
38 template<typename T
> PassWeak
<T
> adoptWeak(WeakImpl
*);
40 template<typename T
> class PassWeak
{
43 PassWeak(std::nullptr_t
);
44 PassWeak(T
*, WeakHandleOwner
* = 0, void* context
= 0);
46 // It somewhat breaks the type system to allow transfer of ownership out of
47 // a const PassWeak. However, it makes it much easier to work with PassWeak
48 // temporaries, and we don't have a need to use real const PassWeaks anyway.
49 PassWeak(const PassWeak
&);
50 template<typename U
> PassWeak(const PassWeak
<U
>&);
54 T
* operator->() const;
58 bool operator!() const;
60 // This conversion operator allows implicit conversion to bool but not to other integer types.
61 typedef void* (PassWeak::*UnspecifiedBoolType
);
62 operator UnspecifiedBoolType
*() const;
64 WeakImpl
* leakImpl() const WARN_UNUSED_RETURN
;
67 friend PassWeak adoptWeak
<T
>(WeakImpl
*);
68 explicit PassWeak(WeakImpl
*);
73 template<typename T
> inline PassWeak
<T
>::PassWeak()
78 template<typename T
> inline PassWeak
<T
>::PassWeak(std::nullptr_t
)
83 template<typename T
> inline PassWeak
<T
>::PassWeak(T
* cell
, WeakHandleOwner
* weakOwner
, void* context
)
84 : m_impl(cell
? WeakSet::allocate(cell
, weakOwner
, context
) : 0)
88 template<typename T
> inline PassWeak
<T
>::PassWeak(const PassWeak
& o
)
89 : m_impl(o
.leakImpl())
93 template<typename T
> template<typename U
> inline PassWeak
<T
>::PassWeak(const PassWeak
<U
>& o
)
94 : m_impl(o
.leakImpl())
98 template<typename T
> inline PassWeak
<T
>::~PassWeak()
102 WeakSet::deallocate(m_impl
);
105 template<typename T
> inline T
* PassWeak
<T
>::operator->() const
107 ASSERT(m_impl
&& m_impl
->state() == WeakImpl::Live
);
108 return jsCast
<T
*>(m_impl
->jsValue().asCell());
111 template<typename T
> inline T
& PassWeak
<T
>::operator*() const
113 ASSERT(m_impl
&& m_impl
->state() == WeakImpl::Live
);
114 return *jsCast
<T
*>(m_impl
->jsValue().asCell());
117 template<typename T
> inline T
* PassWeak
<T
>::get() const
119 if (!m_impl
|| m_impl
->state() != WeakImpl::Live
)
121 return jsCast
<T
*>(m_impl
->jsValue().asCell());
124 template<typename T
> inline bool PassWeak
<T
>::operator!() const
126 return !m_impl
|| m_impl
->state() != WeakImpl::Live
|| !m_impl
->jsValue();
129 template<typename T
> inline PassWeak
<T
>::operator UnspecifiedBoolType
*() const
131 return reinterpret_cast<UnspecifiedBoolType
*>(!!*this);
134 template<typename T
> inline PassWeak
<T
>::PassWeak(WeakImpl
* impl
)
139 template<typename T
> inline WeakImpl
* PassWeak
<T
>::leakImpl() const
142 std::swap(tmp
, const_cast<WeakImpl
*&>(m_impl
));
146 template<typename T
> PassWeak
<T
> inline adoptWeak(WeakImpl
* impl
)
148 return PassWeak
<T
>(impl
);
151 template<typename T
, typename U
> inline bool operator==(const PassWeak
<T
>& a
, const PassWeak
<U
>& b
)
153 return a
.get() == b
.get();
156 template<typename T
, typename U
> inline bool operator==(const PassWeak
<T
>& a
, const Weak
<U
>& b
)
158 return a
.get() == b
.get();
161 template<typename T
, typename U
> inline bool operator==(const Weak
<T
>& a
, const PassWeak
<U
>& b
)
163 return a
.get() == b
.get();
166 template<typename T
, typename U
> inline bool operator==(const PassWeak
<T
>& a
, U
* b
)
171 template<typename T
, typename U
> inline bool operator==(T
* a
, const PassWeak
<U
>& b
)
176 template<typename T
, typename U
> inline bool operator!=(const PassWeak
<T
>& a
, const PassWeak
<U
>& b
)
178 return a
.get() != b
.get();
181 template<typename T
, typename U
> inline bool operator!=(const PassWeak
<T
>& a
, const Weak
<U
>& b
)
183 return a
.get() != b
.get();
186 template<typename T
, typename U
> inline bool operator!=(const Weak
<T
>& a
, const PassWeak
<U
>& b
)
188 return a
.get() != b
.get();
191 template<typename T
, typename U
> inline bool operator!=(const PassWeak
<T
>& a
, U
* b
)
196 template<typename T
, typename U
> inline bool operator!=(T
* a
, const PassWeak
<U
>& b
)