also propagate updateContents() calls to parent frame
[kdelibs.git] / kjs / protect.h
blob912e45c1c17a1b3c141ca671d507f9cf12c6bf2f
1 // -*- c-basic-offset: 2 -*-
2 /*
3 * This file is part of the KDE libraries
4 * Copyright (C) 2004 Apple Computer, Inc.
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.
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.
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.
24 #ifndef _KJS_PROTECT_H_
25 #define _KJS_PROTECT_H_
27 #include "value.h"
28 #include "collector.h"
29 #include "JSLock.h"
31 namespace KJS {
33 inline void gcProtect(JSValue *val)
35 Collector::protect(val);
38 inline void gcUnprotect(JSValue *val)
40 Collector::unprotect(val);
43 inline void gcProtectNullTolerant(JSValue *val)
45 if (val)
46 gcProtect(val);
49 inline void gcUnprotectNullTolerant(JSValue *val)
51 if (val)
52 gcUnprotect(val);
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();
64 template <class U> ProtectedPtr(const ProtectedPtr<U> &);
66 T *get() const { return m_ptr; }
67 operator T *() const { return m_ptr; }
68 T *operator->() const { return m_ptr; }
70 bool operator!() const { return m_ptr == NULL; }
72 ProtectedPtr &operator=(const ProtectedPtr &);
73 ProtectedPtr &operator=(T *);
75 private:
76 T *m_ptr;
79 template <class T> ProtectedPtr<T>::ProtectedPtr(T *ptr)
80 : m_ptr(ptr)
82 if (ptr) {
83 JSLock lock;
84 gcProtect(ptr);
88 template <class T> ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr &o)
89 : m_ptr(o.get())
91 if (T *ptr = m_ptr) {
92 JSLock lock;
93 gcProtect(ptr);
97 template <class T> ProtectedPtr<T>::~ProtectedPtr()
99 if (T *ptr = m_ptr) {
100 JSLock lock;
101 gcUnprotect(ptr);
105 template <class T> template <class U> ProtectedPtr<T>::ProtectedPtr(const ProtectedPtr<U> &o)
106 : m_ptr(o.get())
108 if (T *ptr = m_ptr) {
109 JSLock lock;
110 gcProtect(ptr);
114 template <class T> ProtectedPtr<T> &ProtectedPtr<T>::operator=(const ProtectedPtr<T> &o)
116 JSLock lock;
117 T *optr = o.m_ptr;
118 gcProtectNullTolerant(optr);
119 gcUnprotectNullTolerant(m_ptr);
120 m_ptr = optr;
121 return *this;
124 template <class T> inline ProtectedPtr<T> &ProtectedPtr<T>::operator=(T *optr)
126 JSLock lock;
127 gcProtectNullTolerant(optr);
128 gcUnprotectNullTolerant(m_ptr);
129 m_ptr = optr;
130 return *this;
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(); }
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(); }
141 } // namespace
143 #endif