]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/Handle.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / heap / Handle.h
CommitLineData
14957cd0
A
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
14957cd0
A
46class HandleBase {
47 template <typename T> friend class Weak;
93a37866 48 template <typename T> friend class Strong;
6fe7ccc8 49 friend class HandleSet;
14957cd0 50 friend struct JSCallbackObjectData;
14957cd0
A
51
52public:
53 bool operator!() const { return !m_slot || !*m_slot; }
54
ed1e77d3 55 explicit operator bool() const { return m_slot && *m_slot; }
14957cd0 56
6fe7ccc8
A
57 HandleSlot slot() const { return m_slot; }
58
14957cd0
A
59protected:
60 HandleBase(HandleSlot slot)
61 : m_slot(slot)
62 {
63 }
64
65 void swap(HandleBase& other) { std::swap(m_slot, other.m_slot); }
66
14957cd0
A
67 void setSlot(HandleSlot slot)
68 {
69 m_slot = slot;
70 }
71
72private:
73 HandleSlot m_slot;
74};
75
76template <typename Base, typename T> struct HandleConverter {
77 T* operator->()
78 {
14957cd0
A
79 return static_cast<Base*>(this)->get();
80 }
81 const T* operator->() const
82 {
14957cd0
A
83 return static_cast<const Base*>(this)->get();
84 }
85
86 T* operator*()
87 {
14957cd0
A
88 return static_cast<Base*>(this)->get();
89 }
90 const T* operator*() const
91 {
14957cd0
A
92 return static_cast<const Base*>(this)->get();
93 }
94};
95
96template <typename Base> struct HandleConverter<Base, Unknown> {
97 Handle<JSObject> asObject() const;
98 bool isObject() const { return jsValue().isObject(); }
99 bool getNumber(double number) const { return jsValue().getNumber(number); }
93a37866 100 WTF::String getString(ExecState*) const;
14957cd0
A
101 bool isUndefinedOrNull() const { return jsValue().isUndefinedOrNull(); }
102
103private:
104 JSValue jsValue() const
105 {
14957cd0
A
106 return static_cast<const Base*>(this)->get();
107 }
108};
109
110template <typename T> class Handle : public HandleBase, public HandleConverter<Handle<T>, T> {
111public:
93a37866 112 template <typename A, typename B> friend struct HandleConverter;
14957cd0
A
113 typedef typename HandleTypes<T>::ExternalType ExternalType;
114 template <typename U> Handle(Handle<U> o)
115 {
116 typename HandleTypes<T>::template validateUpcast<U>();
117 setSlot(o.slot());
118 }
119
120 void swap(Handle& other) { HandleBase::swap(other); }
121
122 ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); }
123
124protected:
125 Handle(HandleSlot slot = 0)
126 : HandleBase(slot)
127 {
128 }
129
130private:
6fe7ccc8
A
131 friend class HandleSet;
132 friend class WeakBlock;
14957cd0
A
133
134 static Handle<T> wrapSlot(HandleSlot slot)
135 {
136 return Handle<T>(slot);
137 }
138};
139
140template <typename Base> Handle<JSObject> HandleConverter<Base, Unknown>::asObject() const
141{
142 return Handle<JSObject>::wrapSlot(static_cast<const Base*>(this)->slot());
143}
144
145template <typename T, typename U> inline bool operator==(const Handle<T>& a, const Handle<U>& b)
146{
147 return a.get() == b.get();
148}
149
150template <typename T, typename U> inline bool operator==(const Handle<T>& a, U* b)
151{
152 return a.get() == b;
153}
154
155template <typename T, typename U> inline bool operator==(T* a, const Handle<U>& b)
156{
157 return a == b.get();
158}
159
160template <typename T, typename U> inline bool operator!=(const Handle<T>& a, const Handle<U>& b)
161{
162 return a.get() != b.get();
163}
164
165template <typename T, typename U> inline bool operator!=(const Handle<T>& a, U* b)
166{
167 return a.get() != b;
168}
169
170template <typename T, typename U> inline bool operator!=(T* a, const Handle<U>& b)
171{
172 return a != b.get();
173}
174
175template <typename T, typename U> inline bool operator!=(const Handle<T>& a, JSValue b)
176{
177 return a.get() != b;
178}
179
180template <typename T, typename U> inline bool operator!=(JSValue a, const Handle<U>& b)
181{
182 return a != b.get();
183}
184
185}
186
187#endif