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