IvfMathPointer.h

00001 //
00002 // Copyright 1999-2006 by Structural Mechanics, Lund University.
00003 //
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Library General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later version.
00008 //
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Library General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Library General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00017 // USA.
00018 //
00019 // Please report all bugs and problems to "ivf@byggmek.lth.se".
00020 //
00021 //
00022 // Written by Jonas Lindemann
00023 //
00024 
00025 #ifndef _CIvfPointer_h_
00026 #define _CIvfPointer_h_
00027 
00028 #include <ivf/ivfconfig.h>
00029 #include <ivf/IvfDef.h>
00030 
00053 template <class T> class CIvfPointer {
00054 private:
00055         T* m_object;
00056 public:
00057         CIvfPointer(T* object = 0)
00058         {
00059                 m_object = object;
00060                 if (m_object)
00061                 {
00062                         IvfDbg1("CIvfPointer: created for " << m_object->getClassName());
00063                         m_object->addReference();
00064                 }
00065                 else
00066                 {
00067                         IvfDbg1("CIvfPointer: assigned 0");
00068                 }
00069         }
00070 
00071         CIvfPointer(const CIvfPointer& ivfObject)
00072         {
00073                 m_object = ivfObject.m_object;
00074                 if (m_object)
00075                 {
00076                         IvfDbg1("CIvfPointer: assigned " << m_object->getClassName());
00077                         m_object->addReference();
00078                 }
00079                 else
00080                 {
00081                         IvfDbg1("CIvfPointer: assigned 0");
00082                 }
00083         }
00084 
00085         virtual ~CIvfPointer()
00086         {
00087                 if (m_object)
00088                 {
00089                         m_object->deleteReference();
00090                         IvfDbg1("CIvfPointer: " << m_object->getClassName() << " dereferenced.");
00091                         if (!m_object->referenced())
00092                         {
00093                                 delete m_object;
00094                                 IvfDbg1("CIvfPointer: " << m_object->getClassName() << " deleted.");
00095                         }
00096                 }
00097         }
00098 
00099         operator T* () const { return m_object; }
00100         T& operator* () const { return *m_object; }
00101         T* operator-> () const { return m_object; }
00102 
00103         CIvfPointer& operator= (const CIvfPointer& ivfPointer)
00104         {
00105                 if (m_object!=ivfPointer.m_object)
00106                 {
00107                         if (m_object)
00108                         {
00109                                 m_object->deleteReference();
00110                                 IvfDbg1("CIvfPointer(=): " << m_object->getClassName() << " dereferenced.");
00111                                 if (!m_object->referenced())
00112                                 {
00113                                         delete m_object;
00114                                         IvfDbg1("CIvfPointer(=): " << m_object->getClassName() << " deleted.");
00115                                 }
00116                         }
00117 
00118                         m_object = ivfPointer.m_object;
00119 
00120                         if (m_object)
00121                         {
00122                                 IvfDbg1("CIvfPointer(=): assigned " << m_object->getClassName());
00123                                 m_object->addReference();
00124                         }
00125                         else
00126                         {
00127                                 IvfDbg1("CIvfPointer(=): assigned 0");
00128                         }
00129                 }
00130                 return *this;
00131         }
00132 
00133         CIvfPointer& operator= (T* ivfObject)
00134         {
00135                 if (m_object!=ivfObject)
00136                 {
00137                         if (m_object)
00138                         {
00139                                 m_object->deleteReference();
00140                                 IvfDbg1("CIvfPointer(=): " << m_object->getClassName() << " dereferenced.");
00141                                 if (!m_object->referenced())
00142                                 {
00143                                         delete m_object;
00144                                         IvfDbg1("CIvfPointer(=): " << m_object->getClassName() << " deleted.");
00145                                 }
00146                         }
00147 
00148                         m_object = ivfObject;
00149 
00150                         if (m_object)
00151                         {
00152                                 IvfDbg1("CIvfPointer(=): assigned " << m_object->getClassName());
00153                                 m_object->addReference();
00154                         }
00155                         else
00156                         {
00157                                 IvfDbg1("CIvfPointer(=): assigned 0");
00158                         }
00159                 }
00160                 return *this;
00161         }
00162 
00163         bool operator== (T* ivfObject) const { return m_object == ivfObject; }
00164         bool operator!= (T* ivfObject) const { return m_object != ivfObject; }
00165         bool operator== (const CIvfPointer& ivfPointer) const 
00166         {
00167                 return m_object == ivfPointer.m_object;
00168         }
00169 
00170         bool operator!= (const CIvfPointer& ivfPointer) const
00171         {
00172                 return m_object != ivfPointer.m_object;
00173         }
00174 };
00175 
00176 #endif 

Generated on Fri Sep 1 15:36:47 2006 for Interactive Visualisation Framework - Ivf++ by  doxygen 1.4.6-NO