1 /************************************************************************
3 * Copyright 2011 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
27 #include <QApplication>
28 #include <QAtomicPointer>
32 namespace QtCollider
{
34 template<typename T
> class SafePtr
39 SafePtr( const SafePtr
& other
)
46 : d ( new Data( ptr
) )
49 SafePtr
& operator= ( const SafePtr
& other
) {
60 T
* operator-> () const { return d
->ptr
; }
62 T
& operator* () const { return *d
->ptr
; }
64 operator T
* () const { return (d
? d
->ptr
: 0); }
66 T
*ptr() const { return (d
? d
->ptr
: 0); }
68 void *id() const { return (void*) d
; } // useful for checking internal pointer identity
70 void invalidate() { qcDebugMsg(2,"SafePtr: invalidating"); if(d
) d
->ptr
= 0; }
74 Data ( T
* ptr_
) : ptr(ptr_
), refCount(1) {}
75 QAtomicPointer
<T
> ptr
;
82 qcDebugMsg(2,QString("SafePtr: +refcount = %1").arg(d
->refCount
));
87 bool ref
= d
->refCount
.deref();
88 qcDebugMsg(2,QString("SafePtr: -refcount = %1").arg(d
->refCount
));
90 qcDebugMsg(2,"SafePtr: unreferenced!");
99 } // namespace QtCollider