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