ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

object.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef GRIM_OBJECT_H
00024 #define GRIM_OBJECT_H
00025 
00026 #include "common/list.h"
00027 
00028 namespace Grim {
00029 
00030 class SaveGame;
00031 
00032 class Pointer;
00033 class Object {
00034 public:
00035     Object();
00036     virtual ~Object();
00037 
00038     void reset() { };
00039     void reference();
00040     void dereference();
00041 
00042     int32 getId() const;
00043 
00044 private:
00045     void setId(int32 id);
00046     int _refCount;
00047     Common::List<Pointer *> _pointers;
00048     int32 _id;
00049     static int32 s_id;
00050 
00051     friend class Pointer;
00052 };
00053 
00054 class Pointer {
00055 protected:
00056     virtual ~Pointer() {}
00057 
00058     void addPointer(Object *obj) {
00059         obj->_pointers.push_back(this);
00060     }
00061     void rmPointer(Object *obj) {
00062         obj->_pointers.remove(this);
00063     }
00064 
00065     virtual void resetPointer() {}
00066 
00067     friend class Object;
00068 };
00069 
00070 template<class T> class ObjectPtr : public Pointer {
00071 public:
00072     ObjectPtr() :
00073         _obj(nullptr) {
00074 
00075     }
00076     ObjectPtr(T *obj) :
00077         _obj(obj) {
00078         if (obj) {
00079             Object *o = (Object *)_obj;
00080             o->reference();
00081             addPointer(o);
00082         }
00083     }
00084     ObjectPtr(const ObjectPtr<T> &ptr) : Pointer() {
00085         _obj = nullptr;
00086         *this = ptr;
00087     }
00088     ~ObjectPtr() {
00089         if (_obj) {
00090             Object *o = (Object *)_obj;
00091             rmPointer(o);
00092             o->dereference();
00093         }
00094     }
00095 
00096     ObjectPtr &operator=(T *obj) {
00097         if (obj != _obj) {
00098             if (_obj) {
00099                 rmPointer(_obj);
00100                 _obj->dereference();
00101                 _obj = nullptr;
00102 
00103             }
00104 
00105             if (obj) {
00106                 _obj = obj;
00107                 _obj->reference();
00108                 addPointer(obj);
00109             }
00110         }
00111 
00112         return *this;
00113     }
00114     ObjectPtr &operator=(const ObjectPtr<T> &ptr) {
00115         if (_obj != ptr._obj) {
00116             if (_obj) {
00117                 rmPointer(_obj);
00118                 _obj->dereference();
00119                 _obj = nullptr;
00120 
00121             }
00122 
00123             if (ptr._obj) {
00124                 _obj = ptr._obj;
00125                 _obj->reference();
00126                 addPointer(_obj);
00127             }
00128         }
00129 
00130         return *this;
00131     }
00132     bool operator==(const ObjectPtr &ptr) const {
00133         return (_obj == ptr._obj);
00134     }
00135     bool operator==(Object *obj) const {
00136         return (_obj == obj);
00137     }
00138     operator bool() const {
00139         return (_obj);
00140     }
00141     bool operator!() const {
00142         return (!_obj);
00143     }
00144 
00145     T *object() const {
00146         return _obj;
00147     }
00148     T *operator->() const {
00149         return _obj;
00150     }
00151     T &operator*() const {
00152         return *_obj;
00153     }
00154     operator T*() const {
00155         return _obj;
00156     }
00157 
00158 protected:
00159     void resetPointer() override {
00160         _obj = nullptr;
00161     }
00162 
00163 private:
00164     T *_obj;
00165 };
00166 
00167 }
00168 
00169 #endif


Generated on Sat Jul 20 2019 05:00:54 for ResidualVM by doxygen 1.7.1
curved edge   curved edge