Ticket #8803: shared_ptr_v3.patch

File shared_ptr_v3.patch, 6.4 KB (added by lordhoto, 16 years ago)
  • test/common/ptr.h

     
     1#include <cxxtest/TestSuite.h>
     2
     3#include "common/ptr.h"
     4
     5class 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
     30namespace Common {
     31
     32template<class T>
     33class SharedPtr {
     34        template<class T2> friend class SharedPtr;
     35public:
     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; }
     78private:
     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
     97template<class T1, class T2>
     98bool operator ==(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     99        return l.get() == r.get();
     100}
     101
     102template<class T1, class T2>
     103bool 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
    
     
    3434#include "common/hash-str.h"
    3535#include "common/hashmap.h"
    3636#include "common/stream.h"
     37#include "common/ptr.h"
    3738
    3839#include "kyra/kyra.h"
    3940
     
    111112
    112113        void initializeLoaders();
    113114        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;
    117119        ResFileMap _map;
    118120
    119121        KyraEngine *_vm;
     
    331333#endif
    332334
    333335
     336
  • engines/kyra/resource.cpp

     
    2929#include "common/file.h"
    3030#include "common/fs.h"
    3131#include "common/func.h"
    32 #include "common/algorithm.h"
    3332
    3433#include "gui/message.h"
    3534
     
    4342
    4443Resource::~Resource() {
    4544        unloadAllPakFiles();
    46         for (LoaderIterator i = _loaders.begin(); i != _loaders.end(); ++i)
    47                 delete (*i);
    4845        _loaders.clear();
    4946}
    5047
     
    648645}
    649646
    650647void 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()));
    653650}
    654651
    655652const ResArchiveLoader *Resource::getLoader(ResFileEntry::kType type) const {
    656653        for (CLoaderIterator i = _loaders.begin(); i != _loaders.end(); ++i) {
    657654                if ((*i)->getType() == type)
    658                         return *i;
     655                        return (*i).get();
    659656        }
    660657        return 0;
    661658}
    662659
    663660} // end of namespace Kyra
    664661
     662