RefPtr.hpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #ifndef RefPtr_hpp
00023 #define RefPtr_hpp
00024 
00025 #include "util/error/Log.hpp"
00026 #include "comp.hpp"
00027 
00028 namespace se_core {
00029 
00032     template <class T> class _SeCoreExport RefPtr {
00033     private:
00034         class Ptr {
00035         public:
00036             Ptr(T* ptr)
00037                 : ptr_(ptr), refCount_(1) {
00038             }
00039 
00040             T* object() {
00041                 return ptr_;
00042             }
00043 
00044             void incRef() {
00045                 ++refCount_;
00046             }
00047 
00048             void decRef() {
00049                 --refCount_;
00050                 if(!refCount_)
00051                     delete this;
00052             }
00053             T* ptr_;
00054             int refCount_;
00055 
00056         private:
00057             ~Ptr() {}
00058         };
00059 
00060     public:
00064         RefPtr()
00065             : ptr_(0), isOwner_(false) {
00066         }
00067 
00071         virtual ~RefPtr() {
00072             if(ptr_) {
00073                 if(isOwner_)
00074                     ptr_->ptr_ = 0;
00075                 ptr_->decRef();
00076             }
00077         }
00078 
00079         void set(RefPtr& sp) {
00080             AssertFatal(!isOwner_, "Owner RefPtr's should never be assigned a new value");
00081             if(ptr_)
00082                 ptr_->decRef();
00083 
00084             ptr_ = sp.ptr_;
00085 
00086             ptr_->incRef();
00087         }
00088 
00089         void reset() {
00090             if(ptr_)
00091                 ptr_->decRef();
00092 
00093             ptr_ = 0;
00094         }
00095 
00096         bool isNull() const {
00097             return (ptr_ == 0 || ptr_->ptr_ == 0);
00098         }
00099 
00100         T* object() {
00101             AssertFatal(ptr_, "Check for isNull() before fetching object");
00102             return ptr_->ptr_;
00103         }
00104 
00105         T* object() const {
00106             AssertFatal(ptr_, "Check for isNull() before fetching object");
00107             return ptr_->ptr_;
00108         }
00109 
00110         RefPtr(T* ptr)
00111             : ptr_(new Ptr(ptr)), isOwner_(true) {
00112         }
00113 
00114         inline T* operator->() {
00115             Assert(!isNull());
00116             return ptr_->ptr_;
00117         }
00118 
00119         RefPtr& operator=(T* c) {
00120             AssertFatal(!isOwner_, "Owner RefPtr's should never be assigned a new value");
00121             if(ptr_)
00122                 ptr_->decRef();
00123 
00124             ptr_ = c->ref().ptr_;
00125 
00126             ptr_->incRef();
00127 
00128             return *this;
00129         }
00130 
00131     private:
00134         Ptr* ptr_;
00135         bool isOwner_;
00136 
00137         RefPtr(const RefPtr&);
00138         RefPtr& operator=(const RefPtr&);
00139     };
00140 
00141 }
00142 
00143 #endif

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:10 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo