]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/WeakGCPtr.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / runtime / WeakGCPtr.h
1 /*
2 * Copyright (C) 2009 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 WeakGCPtr_h
27 #define WeakGCPtr_h
28
29 #include "Collector.h"
30 #include <wtf/Noncopyable.h>
31
32 namespace JSC {
33
34 // A smart pointer whose get() function returns 0 for cells awaiting destruction.
35 template <typename T> class WeakGCPtr : Noncopyable {
36 public:
37 WeakGCPtr() : m_ptr(0) { }
38 WeakGCPtr(T* ptr) { assign(ptr); }
39
40 T* get() const
41 {
42 if (!m_ptr || !Heap::isCellMarked(m_ptr))
43 return 0;
44 return m_ptr;
45 }
46
47 bool clear(JSCell* ptr)
48 {
49 if (ptr == m_ptr) {
50 m_ptr = 0;
51 return true;
52 }
53 return false;
54 }
55
56 T& operator*() const { return *get(); }
57 T* operator->() const { return get(); }
58
59 bool operator!() const { return !get(); }
60
61 // This conversion operator allows implicit conversion to bool but not to other integer types.
62 #if COMPILER(WINSCW)
63 operator bool() const { return m_ptr; }
64 #else
65 typedef T* WeakGCPtr::*UnspecifiedBoolType;
66 operator UnspecifiedBoolType() const { return get() ? &WeakGCPtr::m_ptr : 0; }
67 #endif
68
69 WeakGCPtr& operator=(T*);
70
71 #if !ASSERT_DISABLED
72 bool hasDeadObject() const { return !!m_ptr; }
73 #endif
74
75 private:
76 void assign(T* ptr)
77 {
78 ASSERT(ptr);
79 Heap::markCell(ptr);
80 m_ptr = ptr;
81 }
82
83 T* m_ptr;
84 };
85
86 template <typename T> inline WeakGCPtr<T>& WeakGCPtr<T>::operator=(T* optr)
87 {
88 assign(optr);
89 return *this;
90 }
91
92 template <typename T, typename U> inline bool operator==(const WeakGCPtr<T>& a, const WeakGCPtr<U>& b)
93 {
94 return a.get() == b.get();
95 }
96
97 template <typename T, typename U> inline bool operator==(const WeakGCPtr<T>& a, U* b)
98 {
99 return a.get() == b;
100 }
101
102 template <typename T, typename U> inline bool operator==(T* a, const WeakGCPtr<U>& b)
103 {
104 return a == b.get();
105 }
106
107 template <typename T, typename U> inline bool operator!=(const WeakGCPtr<T>& a, const WeakGCPtr<U>& b)
108 {
109 return a.get() != b.get();
110 }
111
112 template <typename T, typename U> inline bool operator!=(const WeakGCPtr<T>& a, U* b)
113 {
114 return a.get() != b;
115 }
116
117 template <typename T, typename U> inline bool operator!=(T* a, const WeakGCPtr<U>& b)
118 {
119 return a != b.get();
120 }
121
122 template <typename T, typename U> inline WeakGCPtr<T> static_pointer_cast(const WeakGCPtr<U>& p)
123 {
124 return WeakGCPtr<T>(static_cast<T*>(p.get()));
125 }
126
127 template <typename T, typename U> inline WeakGCPtr<T> const_pointer_cast(const WeakGCPtr<U>& p)
128 {
129 return WeakGCPtr<T>(const_cast<T*>(p.get()));
130 }
131
132 template <typename T> inline T* getPtr(const WeakGCPtr<T>& p)
133 {
134 return p.get();
135 }
136
137 } // namespace JSC
138
139 #endif // WeakGCPtr_h