autoptr.hxx

Go to the documentation of this file.
00001 /*
00002  *   xyzzy
00003  *   Copyright (C) 2007    Karl W. Pfalzer
00004  *
00005  *   This program is free software; you can redistribute it and/or
00006  *   modify it under the terms of the GNU General Public License
00007  *   as published by the Free Software Foundation; either version 2
00008  *   of the License, or (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.
00018  *   51 Franklin Street, Fifth Floor
00019  *   Boston, MA  02110-1301, USA.
00020  */
00021 
00022 #if !defined(_xyzzy_autoptr_hxx_)
00023 #    define  _xyzzy_autoptr_hxx_
00024 
00025 namespace xyzzy
00026 {
00027     template<class T>
00028     class PTAutoPtr
00029     {
00030     public:
00031         explicit PTAutoPtr(T* p, bool owns = true)
00032         :   m_p(p), m_owns(owns)
00033         {}
00034 
00035         PTAutoPtr(const PTAutoPtr<T> &r)
00036         {
00037             init(r);
00038         }
00039 
00040         const PTAutoPtr& operator=(const PTAutoPtr &r)
00041         {
00042             destroy();
00043             init(r);
00044             return *this;
00045         }
00046 
00047         T* get() const
00048         {
00049             return m_p;
00050         }
00051 
00052         T* operator->()
00053         {
00054             return get();
00055         }
00056 
00057         T& operator*() const
00058         {
00059             return *m_p;
00060         }
00061 
00062         ~PTAutoPtr()
00063         {
00064             destroy();
00065         }
00066 
00067     private:
00068         void init(const PTAutoPtr &r)
00069         {
00070             m_p = r.m_p;
00071             m_owns = r.m_owns;
00072             PTAutoPtr &rr = const_cast<PTAutoPtr&>(r);
00073             rr.m_owns = false;
00074             rr.m_p = 0;
00075         }
00076 
00077         void destroy()
00078         {
00079             if (m_owns)
00080             {
00081                 delete m_p;
00082                 m_p = 0;
00083                 m_owns = false;
00084             }
00085         }
00086 
00087         T       *m_p;
00088         bool    m_owns;
00089     };
00090 };
00091 
00092 #endif  //_xyzzy_autoptr_hxx_

Generated on Thu Mar 22 13:51:07 2007 for anvil by  doxygen 1.5.1