]> git.saurik.com Git - apple/javascriptcore.git/blob - kjs/protect.h
JavaScriptCore-466.1.6.tar.gz
[apple/javascriptcore.git] / kjs / protect.h
1 // -*- c-basic-offset: 2 -*-
2 /*
3 * This file is part of the KDE libraries
4 * Copyright (C) 2004 Apple Computer, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23
24 #ifndef _KJS_PROTECT_H_
25 #define _KJS_PROTECT_H_
26
27 #include "value.h"
28 #include "collector.h"
29 #include "JSLock.h"
30
31 namespace KJS {
32
33 inline void gcProtect(JSValue *val)
34 {
35 Collector::protect(val);
36 }
37
38 inline void gcUnprotect(JSValue *val)
39 {
40 Collector::unprotect(val);
41 }
42
43 inline void gcProtectNullTolerant(JSValue *val)
44 {
45 if (val)
46 gcProtect(val);
47 }
48
49 inline void gcUnprotectNullTolerant(JSValue *val)
50 {
51 if (val)
52 gcUnprotect(val);
53 }
54
55 // FIXME: Share more code with RefPtr template? The only differences are the ref/deref operation
56 // and the implicit conversion to raw pointer
57 template <class T> class ProtectedPtr {
58 public:
59 ProtectedPtr() : m_ptr(NULL) { }
60 ProtectedPtr(T *ptr);
61 ProtectedPtr(const ProtectedPtr &);
62 ~ProtectedPtr();
63
64 template <class U> ProtectedPtr(const ProtectedPtr<U> &);
65
66 T *get() const { return m_ptr; }
67 operator T *() const { return m_ptr; }
68 T *operator->() const { return m_ptr; }
69
70 bool operator!() const { return m_ptr == NULL; }
71
72 ProtectedPtr &operator=(const ProtectedPtr &);
73 ProtectedPtr &operator=(T *);
74
75 private:
76 T *m_ptr;
77 };
78
79 template <class T> ProtectedPtr<T>::ProtectedPtr(T *ptr)
80 : m_ptr(ptr)
81 {
82 if (ptr) {
83 JSLock lock;
84 gcProtect(ptr);
85 }
86 }
87
88 template <class T> ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr &o)
89 : m_ptr(o.get())
90 {
91 if (T *ptr = m_ptr) {
92 JSLock lock;
93 gcProtect(ptr);
94 }
95 }
96
97 template <class T> ProtectedPtr<T>::~ProtectedPtr()
98 {
99 if (T *ptr = m_ptr) {
100 JSLock lock;
101 gcUnprotect(ptr);
102 }
103 }
104
105 template <class T> template <class U> ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr<U> &o)
106 : m_ptr(o.get())
107 {
108 if (T *ptr = m_ptr) {
109 JSLock lock;
110 gcProtect(ptr);
111 }
112 }
113
114 template <class T> ProtectedPtr<T> &ProtectedPtr<T>::operator=(const ProtectedPtr<T> &o)
115 {
116 JSLock lock;
117 T *optr = o.m_ptr;
118 gcProtectNullTolerant(optr);
119 gcUnprotectNullTolerant(m_ptr);
120 m_ptr = optr;
121 return *this;
122 }
123
124 template <class T> inline ProtectedPtr<T> &ProtectedPtr<T>::operator=(T *optr)
125 {
126 JSLock lock;
127 gcProtectNullTolerant(optr);
128 gcUnprotectNullTolerant(m_ptr);
129 m_ptr = optr;
130 return *this;
131 }
132
133 template <class T> inline bool operator==(const ProtectedPtr<T> &a, const ProtectedPtr<T> &b) { return a.get() == b.get(); }
134 template <class T> inline bool operator==(const ProtectedPtr<T> &a, const T *b) { return a.get() == b; }
135 template <class T> inline bool operator==(const T *a, const ProtectedPtr<T> &b) { return a == b.get(); }
136
137 template <class T> inline bool operator!=(const ProtectedPtr<T> &a, const ProtectedPtr<T> &b) { return a.get() != b.get(); }
138 template <class T> inline bool operator!=(const ProtectedPtr<T> &a, const T *b) { return a.get() != b; }
139 template <class T> inline bool operator!=(const T *a, const ProtectedPtr<T> &b) { return a != b.get(); }
140
141 } // namespace
142
143 #endif