CommonPtr.hh

00001 //********************************************************************
00002 //*** mm/CommonPtr.hh
00003 //*** Copyright (c) 2002-2005 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: CommonPtr.hh,v 1.3 2005-08-13 18:08:22 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 #ifndef MM_COMMONPTR_HH
00025 #define MM_COMMONPTR_HH
00026 
00027 #include <memory>
00028 #include <mm/gc_ptr.hh>
00029 
00030 namespace fatalmind {
00031 
00032 template<class T, class TM>
00033 class CommonPtr {
00034     public:
00035         CommonPtr() 
00036         : gc_p()
00037         , auto_p()
00038         , p()
00039         {
00040         }
00041 
00042         CommonPtr(gc_ptr<T, TM> a) 
00043         : gc_p(a)
00044         , auto_p()
00045         , p(gc_p.get())
00046         {
00047         }
00048 
00049         CommonPtr(std::auto_ptr<T> a) 
00050         : gc_p()
00051         , auto_p(a)
00052         , p(auto_p.get())
00053         {
00054         }
00055 
00056         CommonPtr(CommonPtr<T, TM>& rho)
00057         : gc_p(rho.gc_p)
00058         , auto_p(rho.auto_p)
00059         , p(rho.p)
00060         {
00061         }
00062 
00063         CommonPtr<T, TM>& operator=(CommonPtr<T, TM>& rho) {
00064             if (this != &rho) {
00065                 gc_p = rho.gc_p;
00066                 auto_p = rho.auto_p;
00067                 p = rho.p;
00068             }
00069             return *this;
00070         }
00071 
00072         inline T& operator*() const throw() {
00073             return *this;
00074         }
00075 
00076         T* operator->() const {
00077             return p;
00078         }
00079 
00080         void reset()
00081         {
00082             auto_p.reset();
00083             gc_p.reset();
00084             p = 0;
00085         }
00086 
00087         void reset(gc_ptr<T, TM> a)
00088         {
00089             auto_p.reset();
00090             gc_p = a;
00091             p = gc_p.get();
00092         }
00093 
00094         void reset(std::auto_ptr<T> a)
00095         {
00096             gc_p.reset();
00097             auto_p = a;
00098             p = auto_p.get();
00099         }
00100 
00101         void reset(CommonPtr<T, TM> a) {
00102             gc_p   = a.gc_p;
00103             auto_p = a.auto_p;
00104             p      = a.p;
00105         }
00106 
00107         T* get() {
00108             return p;
00109         }
00110 
00111     private:
00112         gc_ptr<T, TM> gc_p;
00113         std::auto_ptr<T> auto_p;
00114 
00115         T* p;
00116 };
00117 
00118 } // namespace fatalmind
00119 
00120 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3