]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Protect.h
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / runtime / Protect.h
1 /*
2 * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21
22 #ifndef Protect_h
23 #define Protect_h
24
25 #include "JSCell.h"
26 #include "Collector.h"
27
28 namespace JSC {
29
30 inline void gcProtect(JSCell* val)
31 {
32 Heap::heap(val)->protect(val);
33 }
34
35 inline void gcUnprotect(JSCell* val)
36 {
37 Heap::heap(val)->unprotect(val);
38 }
39
40 inline void gcProtectNullTolerant(JSCell* val)
41 {
42 if (val)
43 gcProtect(val);
44 }
45
46 inline void gcUnprotectNullTolerant(JSCell* val)
47 {
48 if (val)
49 gcUnprotect(val);
50 }
51
52 inline void gcProtect(JSValuePtr value)
53 {
54 if (value && value.isCell())
55 gcProtect(asCell(value));
56 }
57
58 inline void gcUnprotect(JSValuePtr value)
59 {
60 if (value && value.isCell())
61 gcUnprotect(asCell(value));
62 }
63
64 // FIXME: Share more code with RefPtr template? The only differences are the ref/deref operation
65 // and the implicit conversion to raw pointer
66 template <class T> class ProtectedPtr {
67 public:
68 ProtectedPtr() : m_ptr(0) {}
69 ProtectedPtr(T* ptr);
70 ProtectedPtr(const ProtectedPtr&);
71 ~ProtectedPtr();
72
73 template <class U> ProtectedPtr(const ProtectedPtr<U>&);
74
75 T* get() const { return m_ptr; }
76 operator T*() const { return m_ptr; }
77 operator JSValuePtr() const { return JSValuePtr(m_ptr); }
78 T* operator->() const { return m_ptr; }
79
80 operator bool() const { return m_ptr; }
81 bool operator!() const { return !m_ptr; }
82
83 ProtectedPtr& operator=(const ProtectedPtr&);
84 ProtectedPtr& operator=(T*);
85
86 private:
87 T* m_ptr;
88 };
89
90 class ProtectedJSValuePtr {
91 public:
92 ProtectedJSValuePtr() {}
93 ProtectedJSValuePtr(JSValuePtr value);
94 ProtectedJSValuePtr(const ProtectedJSValuePtr&);
95 ~ProtectedJSValuePtr();
96
97 template <class U> ProtectedJSValuePtr(const ProtectedPtr<U>&);
98
99 JSValuePtr get() const { return m_value; }
100 operator JSValuePtr() const { return m_value; }
101 JSValuePtr operator->() const { return m_value; }
102
103 operator bool() const { return m_value; }
104 bool operator!() const { return !m_value; }
105
106 ProtectedJSValuePtr& operator=(const ProtectedJSValuePtr&);
107 ProtectedJSValuePtr& operator=(JSValuePtr);
108
109 private:
110 JSValuePtr m_value;
111 };
112
113 template <class T> inline ProtectedPtr<T>::ProtectedPtr(T* ptr)
114 : m_ptr(ptr)
115 {
116 gcProtectNullTolerant(m_ptr);
117 }
118
119 template <class T> inline ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr& o)
120 : m_ptr(o.get())
121 {
122 gcProtectNullTolerant(m_ptr);
123 }
124
125 template <class T> inline ProtectedPtr<T>::~ProtectedPtr()
126 {
127 gcUnprotectNullTolerant(m_ptr);
128 }
129
130 template <class T> template <class U> inline ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr<U>& o)
131 : m_ptr(o.get())
132 {
133 gcProtectNullTolerant(m_ptr);
134 }
135
136 template <class T> inline ProtectedPtr<T>& ProtectedPtr<T>::operator=(const ProtectedPtr<T>& o)
137 {
138 T* optr = o.m_ptr;
139 gcProtectNullTolerant(optr);
140 gcUnprotectNullTolerant(m_ptr);
141 m_ptr = optr;
142 return *this;
143 }
144
145 template <class T> inline ProtectedPtr<T>& ProtectedPtr<T>::operator=(T* optr)
146 {
147 gcProtectNullTolerant(optr);
148 gcUnprotectNullTolerant(m_ptr);
149 m_ptr = optr;
150 return *this;
151 }
152
153 inline ProtectedJSValuePtr::ProtectedJSValuePtr(JSValuePtr value)
154 : m_value(value)
155 {
156 gcProtect(m_value);
157 }
158
159 inline ProtectedJSValuePtr::ProtectedJSValuePtr(const ProtectedJSValuePtr& o)
160 : m_value(o.get())
161 {
162 gcProtect(m_value);
163 }
164
165 inline ProtectedJSValuePtr::~ProtectedJSValuePtr()
166 {
167 gcUnprotect(m_value);
168 }
169
170 template <class U> ProtectedJSValuePtr::ProtectedJSValuePtr(const ProtectedPtr<U>& o)
171 : m_value(o.get())
172 {
173 gcProtect(m_value);
174 }
175
176 inline ProtectedJSValuePtr& ProtectedJSValuePtr::operator=(const ProtectedJSValuePtr& o)
177 {
178 JSValuePtr ovalue = o.m_value;
179 gcProtect(ovalue);
180 gcUnprotect(m_value);
181 m_value = ovalue;
182 return *this;
183 }
184
185 inline ProtectedJSValuePtr& ProtectedJSValuePtr::operator=(JSValuePtr ovalue)
186 {
187 gcProtect(ovalue);
188 gcUnprotect(m_value);
189 m_value = ovalue;
190 return *this;
191 }
192
193 template <class T> inline bool operator==(const ProtectedPtr<T>& a, const ProtectedPtr<T>& b) { return a.get() == b.get(); }
194 template <class T> inline bool operator==(const ProtectedPtr<T>& a, const T* b) { return a.get() == b; }
195 template <class T> inline bool operator==(const T* a, const ProtectedPtr<T>& b) { return a == b.get(); }
196
197 template <class T> inline bool operator!=(const ProtectedPtr<T>& a, const ProtectedPtr<T>& b) { return a.get() != b.get(); }
198 template <class T> inline bool operator!=(const ProtectedPtr<T>& a, const T* b) { return a.get() != b; }
199 template <class T> inline bool operator!=(const T* a, const ProtectedPtr<T>& b) { return a != b.get(); }
200
201 inline bool operator==(const ProtectedJSValuePtr& a, const ProtectedJSValuePtr& b) { return a.get() == b.get(); }
202 inline bool operator==(const ProtectedJSValuePtr& a, const JSValuePtr b) { return a.get() == b; }
203 template <class T> inline bool operator==(const ProtectedJSValuePtr& a, const ProtectedPtr<T>& b) { return a.get() == JSValuePtr(b.get()); }
204 inline bool operator==(const JSValuePtr a, const ProtectedJSValuePtr& b) { return a == b.get(); }
205 template <class T> inline bool operator==(const ProtectedPtr<T>& a, const ProtectedJSValuePtr& b) { return JSValuePtr(a.get()) == b.get(); }
206
207 inline bool operator!=(const ProtectedJSValuePtr& a, const ProtectedJSValuePtr& b) { return a.get() != b.get(); }
208 inline bool operator!=(const ProtectedJSValuePtr& a, const JSValuePtr b) { return a.get() != b; }
209 template <class T> inline bool operator!=(const ProtectedJSValuePtr& a, const ProtectedPtr<T>& b) { return a.get() != JSValuePtr(b.get()); }
210 inline bool operator!=(const JSValuePtr a, const ProtectedJSValuePtr& b) { return a != b.get(); }
211 template <class T> inline bool operator!=(const ProtectedPtr<T>& a, const ProtectedJSValuePtr& b) { return JSValuePtr(a.get()) != b.get(); }
212
213 } // namespace JSC
214
215 #endif // Protect_h