]>
git.saurik.com Git - apple/javascriptcore.git/blob - kjs/protect.h
1 // -*- c-basic-offset: 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_
28 #include "collector.h"
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
)
49 inline void gcUnprotectNullTolerant(JSValue
*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
{
59 ProtectedPtr() : m_ptr(NULL
) { }
61 ProtectedPtr(const 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
*);
79 template <class T
> ProtectedPtr
<T
>::ProtectedPtr(T
*ptr
)
88 template <class T
> ProtectedPtr
<T
>::ProtectedPtr(const ProtectedPtr
&o
)
97 template <class T
> ProtectedPtr
<T
>::~ProtectedPtr()
105 template <class T
> template <class U
> ProtectedPtr
<T
>::ProtectedPtr(const ProtectedPtr
<U
> &o
)
108 if (T
*ptr
= m_ptr
) {
114 template <class T
> ProtectedPtr
<T
> &ProtectedPtr
<T
>::operator=(const ProtectedPtr
<T
> &o
)
118 gcProtectNullTolerant(optr
);
119 gcUnprotectNullTolerant(m_ptr
);
124 template <class T
> inline ProtectedPtr
<T
> &ProtectedPtr
<T
>::operator=(T
*optr
)
127 gcProtectNullTolerant(optr
);
128 gcUnprotectNullTolerant(m_ptr
);
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(); }