source: webkit/trunk/JavaScriptCore/kjs/protect.h@ 36100

Last change on this file since 36100 was 35027, checked in by [email protected], 17 years ago

JavaScriptCore:

2008-07-06 Sam Weinig <[email protected]>

Reviewed by Cameron Zwarich.

Second step in broad cleanup effort.

[ File list elided ]

WebCore:

2008-07-06 Sam Weinig <[email protected]>

Reviewed by Cameron Zwarich.

Add #include for kjs/protect.h.

  • xml/XMLHttpRequest.cpp: (WebCore::XMLHttpRequest::loadRequestAsynchronously):
  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1/*
2 * Copyright (C) 2004, 2008 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 "JSValue.h"
26#include "collector.h"
27
28namespace KJS {
29
30 inline void gcProtect(JSValue* val)
31 {
32 Heap* heap = Heap::heap(val);
33 if (heap)
34 heap->protect(val);
35 }
36
37 inline void gcUnprotect(JSValue* val)
38 {
39 Heap* heap = Heap::heap(val);
40 if (heap)
41 heap->unprotect(val);
42 }
43
44 inline void gcProtectNullTolerant(JSValue* val)
45 {
46 if (val)
47 gcProtect(val);
48 }
49
50 inline void gcUnprotectNullTolerant(JSValue* val)
51 {
52 if (val)
53 gcUnprotect(val);
54 }
55
56 // FIXME: Share more code with RefPtr template? The only differences are the ref/deref operation
57 // and the implicit conversion to raw pointer
58 template <class T> class ProtectedPtr {
59 public:
60 ProtectedPtr() : m_ptr(0) { }
61 ProtectedPtr(T* ptr);
62 ProtectedPtr(const ProtectedPtr&);
63 ~ProtectedPtr();
64
65 template <class U> ProtectedPtr(const ProtectedPtr<U>&);
66
67 T* get() const { return m_ptr; }
68 operator T*() const { return m_ptr; }
69 T* operator->() const { return m_ptr; }
70
71 bool operator!() const { return m_ptr == NULL; }
72
73 ProtectedPtr& operator=(const ProtectedPtr&);
74 ProtectedPtr& operator=(T*);
75
76 private:
77 T* m_ptr;
78 };
79
80 template <class T> ProtectedPtr<T>::ProtectedPtr(T* ptr)
81 : m_ptr(ptr)
82 {
83 if (ptr)
84 gcProtect(ptr);
85 }
86
87 template <class T> ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr& o)
88 : m_ptr(o.get())
89 {
90 if (T* ptr = m_ptr)
91 gcProtect(ptr);
92 }
93
94 template <class T> ProtectedPtr<T>::~ProtectedPtr()
95 {
96 if (T* ptr = m_ptr)
97 gcUnprotect(ptr);
98 }
99
100 template <class T> template <class U> ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr<U>& o)
101 : m_ptr(o.get())
102 {
103 if (T* ptr = m_ptr)
104 gcProtect(ptr);
105 }
106
107 template <class T> ProtectedPtr<T>& ProtectedPtr<T>::operator=(const ProtectedPtr<T>& o)
108 {
109 T* optr = o.m_ptr;
110 gcProtectNullTolerant(optr);
111 gcUnprotectNullTolerant(m_ptr);
112 m_ptr = optr;
113 return *this;
114 }
115
116 template <class T> inline ProtectedPtr<T>& ProtectedPtr<T>::operator=(T* optr)
117 {
118 gcProtectNullTolerant(optr);
119 gcUnprotectNullTolerant(m_ptr);
120 m_ptr = optr;
121 return *this;
122 }
123
124 template <class T> inline bool operator==(const ProtectedPtr<T>& a, const ProtectedPtr<T>& b) { return a.get() == b.get(); }
125 template <class T> inline bool operator==(const ProtectedPtr<T>& a, const T* b) { return a.get() == b; }
126 template <class T> inline bool operator==(const T* a, const ProtectedPtr<T>& b) { return a == b.get(); }
127
128 template <class T> inline bool operator!=(const ProtectedPtr<T>& a, const ProtectedPtr<T>& b) { return a.get() != b.get(); }
129 template <class T> inline bool operator!=(const ProtectedPtr<T>& a, const T* b) { return a.get() != b; }
130 template <class T> inline bool operator!=(const T* a, const ProtectedPtr<T>& b) { return a != b.get(); }
131
132} // namespace KJS
133
134#endif // protect_h
Note: See TracBrowser for help on using the repository browser.