1/*
2 * Copyright (C) 2011 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 Handle_h
27#define Handle_h
28
29#include "HandleTypes.h"
30
31namespace JSC {
32
33/*
34    A Handle is a smart pointer that updates automatically when the garbage
35    collector moves the object to which it points.
36
37    The base Handle class represents a temporary reference to a pointer whose
38    lifetime is guaranteed by something else.
39*/
40
41template <class T> class Handle;
42
43// Creating a JSValue Handle is invalid
44template <> class Handle<JSValue>;
45
46class HandleBase {
47    template <typename T> friend class Weak;
48    template <typename T> friend class Strong;
49    friend class HandleSet;
50    friend struct JSCallbackObjectData;
51
52public:
53    bool operator!() const { return !m_slot || !*m_slot; }
54
55    // This conversion operator allows implicit conversion to bool but not to other integer types.
56    typedef JSValue (HandleBase::*UnspecifiedBoolType);
57    operator UnspecifiedBoolType*() const { return (m_slot && *m_slot) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; }
58
59    HandleSlot slot() const { return m_slot; }
60
61protected:
62    HandleBase(HandleSlot slot)
63        : m_slot(slot)
64    {
65    }
66
67    void swap(HandleBase& other) { std::swap(m_slot, other.m_slot); }
68
69    void setSlot(HandleSlot slot)
70    {
71        m_slot = slot;
72    }
73
74private:
75    HandleSlot m_slot;
76};
77
78template <typename Base, typename T> struct HandleConverter {
79    T* operator->()
80    {
81        return static_cast<Base*>(this)->get();
82    }
83    const T* operator->() const
84    {
85        return static_cast<const Base*>(this)->get();
86    }
87
88    T* operator*()
89    {
90        return static_cast<Base*>(this)->get();
91    }
92    const T* operator*() const
93    {
94        return static_cast<const Base*>(this)->get();
95    }
96};
97
98template <typename Base> struct HandleConverter<Base, Unknown> {
99    Handle<JSObject> asObject() const;
100    bool isObject() const { return jsValue().isObject(); }
101    bool getNumber(double number) const { return jsValue().getNumber(number); }
102    WTF::String getString(ExecState*) const;
103    bool isUndefinedOrNull() const { return jsValue().isUndefinedOrNull(); }
104
105private:
106    JSValue jsValue() const
107    {
108        return static_cast<const Base*>(this)->get();
109    }
110};
111
112template <typename T> class Handle : public HandleBase, public HandleConverter<Handle<T>, T> {
113public:
114    template <typename A, typename B> friend struct HandleConverter;
115    typedef typename HandleTypes<T>::ExternalType ExternalType;
116    template <typename U> Handle(Handle<U> o)
117    {
118        typename HandleTypes<T>::template validateUpcast<U>();
119        setSlot(o.slot());
120    }
121
122    void swap(Handle& other) { HandleBase::swap(other); }
123
124    ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); }
125
126protected:
127    Handle(HandleSlot slot = 0)
128        : HandleBase(slot)
129    {
130    }
131
132private:
133    friend class HandleSet;
134    friend class WeakBlock;
135
136    static Handle<T> wrapSlot(HandleSlot slot)
137    {
138        return Handle<T>(slot);
139    }
140};
141
142template <typename Base> Handle<JSObject> HandleConverter<Base, Unknown>::asObject() const
143{
144    return Handle<JSObject>::wrapSlot(static_cast<const Base*>(this)->slot());
145}
146
147template <typename T, typename U> inline bool operator==(const Handle<T>& a, const Handle<U>& b)
148{
149    return a.get() == b.get();
150}
151
152template <typename T, typename U> inline bool operator==(const Handle<T>& a, U* b)
153{
154    return a.get() == b;
155}
156
157template <typename T, typename U> inline bool operator==(T* a, const Handle<U>& b)
158{
159    return a == b.get();
160}
161
162template <typename T, typename U> inline bool operator!=(const Handle<T>& a, const Handle<U>& b)
163{
164    return a.get() != b.get();
165}
166
167template <typename T, typename U> inline bool operator!=(const Handle<T>& a, U* b)
168{
169    return a.get() != b;
170}
171
172template <typename T, typename U> inline bool operator!=(T* a, const Handle<U>& b)
173{
174    return a != b.get();
175}
176
177template <typename T, typename U> inline bool operator!=(const Handle<T>& a, JSValue b)
178{
179    return a.get() != b;
180}
181
182template <typename T, typename U> inline bool operator!=(JSValue a, const Handle<U>& b)
183{
184    return a != b.get();
185}
186
187}
188
189#endif
190