Ticket #8803: shared_ptr.patch

File shared_ptr.patch, 6.8 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.useCount() == p2.useCount());
     17                        TS_ASSERT(p1.useCount() == 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.useCount() == 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.useCount() == 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 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; }
     76private:
     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
     95template<class T1, class T2>
     96bool operator ==(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     97        return l.get() == r.get();
     98}
     99
     100template<class T1, class T2>
     101bool operator !=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     102        return l.get() == r.get();
     103}
     104
     105template<class T1, class T2>
     106bool operator <(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     107        return l.get() < r.get();
     108}
     109
     110template<class T1, class T2>
     111bool operator <=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     112        return l.get() <= r.get();
     113}
     114
     115template<class T1, class T2>
     116bool operator >(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     117        return l.get() > r.get();
     118}
     119
     120template<class T1, class T2>
     121bool operator >=(const Common::SharedPtr<T1> &l, const Common::SharedPtr<T2> &r) {
     122        return l.get() >= r.get();
     123}
     124
     125#endif
     126
  • 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;
  • 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}