| 1 | /* ScummVM - Graphic Adventure Engine |
| 2 | * |
| 3 | * ScummVM is the legal property of its developers, whose names |
| 4 | * are too numerous to list here. Please refer to the COPYRIGHT |
| 5 | * file distributed with this source distribution. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | * $URL$ |
| 22 | * $Id$ |
| 23 | */ |
| 24 | |
| 25 | #ifndef COMMON_PTR_H |
| 26 | #define COMMON_PTR_H |
| 27 | |
| 28 | #include "common/scummsys.h" |
| 29 | |
| 30 | namespace Common { |
| 31 | |
| 32 | template<class T> |
| 33 | class SharedPtr { |
| 34 | template<class T2> friend class SharedPtr; |
| 35 | public: |
| 36 | typedef int RefValue; |
| 37 | typedef T ValueType; |
| 38 | typedef T *Pointer; |
| 39 | |
| 40 | SharedPtr() : _refCount(0), _pointer(0) {} |
| 41 | template<class T2> explicit SharedPtr(T2 *p) : _refCount(new RefValue(1)), _pointer(p) {} |
| 42 | |
| 43 | SharedPtr(const SharedPtr &r) : _refCount(r._refCount), _pointer(r._pointer) { ++(*_refCount); } |
| 44 | template<class T2> SharedPtr(const SharedPtr<T2> &r) : _refCount(r._refCount), _pointer(r._pointer) { ++(*_refCount); } |
| 45 | |
| 46 | ~SharedPtr() { decRef(); } |
| 47 | |
| 48 | SharedPtr &operator =(const SharedPtr &r) { |
| 49 | if (r._refCount) |
| 50 | ++(*r._refCount); |
| 51 | decRef(); |
| 52 | |
| 53 | _refCount = r._refCount; |
| 54 | _pointer = r._pointer; |
| 55 | |
| 56 | return *this; |
| 57 | } |
| 58 | |
| 59 | template<class T2> |
| 60 | SharedPtr &operator =(const SharedPtr<T2> &r) { |
| 61 | if (r._refCount) |
| 62 | ++(*r._refCount); |
| 63 | decRef(); |
| 64 | |
| 65 | _refCount = r._refCount; |
| 66 | _pointer = r._pointer; |
| 67 | |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | ValueType &operator *() const { assert(_pointer); return *_pointer; } |
| 72 | Pointer operator ->() const { assert(_pointer); return _pointer; } |
| 73 | Pointer get() const { return _pointer; } |
| 74 | |
| 75 | operator bool() const { return _pointer != 0; } |
| 76 | bool unique() const { return refCount() == 1; } |
| 77 | RefValue refCount() const { return _refCount ? *_refCount : 0; } |
| 78 | private: |
| 79 | void decRef() { |
| 80 | if (_refCount) { |
| 81 | --(*_refCount); |
| 82 | if (!*_refCount) { |
| 83 | delete _refCount; |
| 84 | delete _pointer; |
| 85 | _refCount = 0; |
| 86 | _pointer = 0; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | RefValue *_refCount; |
| 92 | T *_pointer; |
| 93 | }; |
| 94 | |
| 95 | } // end of namespace Common |
| 96 | |
| 97 | template<class T1, class T2> |
| 98 | bool operator ==(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 99 | return l.get() == r.get(); |
| 100 | } |
| 101 | |
| 102 | template<class T1, class T2> |
| 103 | bool operator !=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 104 | return l.get() != r.get(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | #endif |
| 109 | |