| 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 CountValue; |
| 37 | typedef T ValueType; |
| 38 | typedef T *Pointer; |
| 39 | |
| 40 | SharedPtr() : _useCount(0), _pointer(0) {} |
| 41 | template<class T2> explicit SharedPtr(T2 *p) : _useCount(new CountValue(1)), _pointer(p) {} |
| 42 | |
| 43 | SharedPtr(const SharedPtr &r) : _useCount(r._useCount), _pointer(r._pointer) { ++(*_useCount); } |
| 44 | template<class T2> SharedPtr(const SharedPtr<T2> &r) : _useCount(r._useCount), _pointer(r._pointer) { ++(*_useCount); } |
| 45 | |
| 46 | ~SharedPtr() { dec(); } |
| 47 | |
| 48 | SharedPtr &operator =(const SharedPtr &r) { |
| 49 | dec(); |
| 50 | |
| 51 | _useCount = r._useCount; |
| 52 | ++(*_useCount); |
| 53 | _pointer = r._pointer; |
| 54 | |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | template<class T2> |
| 59 | SharedPtr &operator =(const SharedPtr<T2> &r) { |
| 60 | dec(); |
| 61 | |
| 62 | _useCount = r._useCount; |
| 63 | ++(*_useCount); |
| 64 | _pointer = r._pointer; |
| 65 | |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | ValueType &operator *() const { assert(_pointer); return *_pointer; } |
| 70 | Pointer operator ->() const { assert(_pointer); return _pointer; } |
| 71 | Pointer get() const { return _pointer; } |
| 72 | |
| 73 | operator bool() const { return _pointer != 0; } |
| 74 | bool unique() const { return useCount() == 1; } |
| 75 | CountValue useCount() const { return _useCount ? (*_useCount) : 0; } |
| 76 | private: |
| 77 | void dec() { |
| 78 | if (_useCount) { |
| 79 | --(*_useCount); |
| 80 | if (!*_useCount) { |
| 81 | delete _useCount; |
| 82 | delete _pointer; |
| 83 | _useCount = 0; |
| 84 | _pointer = 0; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | CountValue *_useCount; |
| 90 | T *_pointer; |
| 91 | }; |
| 92 | |
| 93 | } // end of namespace Common |
| 94 | |
| 95 | template<class T1, class T2> |
| 96 | bool operator ==(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 97 | return l.get() == r.get(); |
| 98 | } |
| 99 | |
| 100 | template<class T1, class T2> |
| 101 | bool operator !=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 102 | return l.get() == r.get(); |
| 103 | } |
| 104 | |
| 105 | template<class T1, class T2> |
| 106 | bool operator <(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 107 | return l.get() < r.get(); |
| 108 | } |
| 109 | |
| 110 | template<class T1, class T2> |
| 111 | bool operator <=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 112 | return l.get() <= r.get(); |
| 113 | } |
| 114 | |
| 115 | template<class T1, class T2> |
| 116 | bool operator >(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 117 | return l.get() > r.get(); |
| 118 | } |
| 119 | |
| 120 | template<class T1, class T2> |
| 121 | bool operator >=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) { |
| 122 | return l.get() >= r.get(); |
| 123 | } |
| 124 | |
| 125 | #endif |
| 126 | |