Ticket #8803: shared_ptr_v3.patch
File shared_ptr_v3.patch, 6.4 KB (added by , 17 years ago) |
---|
-
test/common/ptr.h
1 #include <cxxtest/TestSuite.h> 2 3 #include "common/ptr.h" 4 5 class PtrTestSuite : public CxxTest::TestSuite 6 { 7 public: 8 void test_assign() { 9 Common::SharedPtr<int> p1(new int(1)); 10 TS_ASSERT(p1.unique()); 11 TS_ASSERT_EQUALS(*p1, 1); 12 13 { 14 Common::SharedPtr<int> p2 = p1; 15 TS_ASSERT(!p1.unique()); 16 TS_ASSERT(p1.refCount() == p2.refCount()); 17 TS_ASSERT(p1.refCount() == 2); 18 TS_ASSERT(p1 == p2); 19 TS_ASSERT_EQUALS(*p2, 1); 20 { 21 Common::SharedPtr<int> p3; 22 p3 = p2; 23 TS_ASSERT(p3 == p2 && p3 == p1); 24 TS_ASSERT(p1.refCount() == 3); 25 TS_ASSERT_EQUALS(*p3, 1); 26 *p3 = 0; 27 TS_ASSERT_EQUALS(*p3, 0); 28 } 29 TS_ASSERT_EQUALS(*p2, 0); 30 TS_ASSERT(p1.refCount() == 2); 31 } 32 33 TS_ASSERT_EQUALS(*p1, 0); 34 TS_ASSERT(p1.unique()); 35 } 36 }; -
common/ptr.h
Eigenschaftsänderungen: test/common/ptr.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native
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) { if (_refCount) ++(*_refCount); } 44 template<class T2> SharedPtr(const SharedPtr<T2> &r) : _refCount(r._refCount), _pointer(r._pointer) { if (_refCount) ++(*_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 -
engines/kyra/resource.h
Eigenschaftsänderungen: common/ptr.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native
34 34 #include "common/hash-str.h" 35 35 #include "common/hashmap.h" 36 36 #include "common/stream.h" 37 #include "common/ptr.h" 37 38 38 39 #include "kyra/kyra.h" 39 40 … … 111 112 112 113 void initializeLoaders(); 113 114 const ResArchiveLoader *getLoader(ResFileEntry::kType type) const; 114 typedef Common::List<ResArchiveLoader*>::iterator LoaderIterator; 115 typedef Common::List<ResArchiveLoader*>::const_iterator CLoaderIterator; 116 Common::List<ResArchiveLoader*> _loaders; 115 typedef Common::List<Common::SharedPtr<ResArchiveLoader> > LoaderList; 116 typedef LoaderList::iterator LoaderIterator; 117 typedef LoaderList::const_iterator CLoaderIterator; 118 LoaderList _loaders; 117 119 ResFileMap _map; 118 120 119 121 KyraEngine *_vm; … … 331 333 #endif 332 334 333 335 336 -
engines/kyra/resource.cpp
29 29 #include "common/file.h" 30 30 #include "common/fs.h" 31 31 #include "common/func.h" 32 #include "common/algorithm.h"33 32 34 33 #include "gui/message.h" 35 34 … … 43 42 44 43 Resource::~Resource() { 45 44 unloadAllPakFiles(); 46 for (LoaderIterator i = _loaders.begin(); i != _loaders.end(); ++i)47 delete (*i);48 45 _loaders.clear(); 49 46 } 50 47 … … 648 645 } 649 646 650 647 void Resource::initializeLoaders() { 651 _loaders.push_back( new ResLoaderPak());652 _loaders.push_back( new ResLoaderIns());648 _loaders.push_back(LoaderList::value_type(new ResLoaderPak())); 649 _loaders.push_back(LoaderList::value_type(new ResLoaderIns())); 653 650 } 654 651 655 652 const ResArchiveLoader *Resource::getLoader(ResFileEntry::kType type) const { 656 653 for (CLoaderIterator i = _loaders.begin(); i != _loaders.end(); ++i) { 657 654 if ((*i)->getType() == type) 658 return *i;655 return (*i).get(); 659 656 } 660 657 return 0; 661 658 } 662 659 663 660 } // end of namespace Kyra 664 661 662