Version: 6.3.1

src/VISU_I/SALOME_GenericObjPointer.hh

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOME_GenericObjPtr.hh
00024 //  Author : Oleg UVAROV
00025 //  Module : SALOME
00026 //
00027 #ifndef SALOME_GenericObjPointer_HH
00028 #define SALOME_GenericObjPointer_HH
00029 
00030 #include "SALOMEconfig.h"
00031 #include CORBA_SERVER_HEADER(SALOME_GenericObj)
00032 
00033 #include <iosfwd>  // for std::basic_ostream
00034 
00035 namespace SALOME
00036 {
00037   //----------------------------------------------------------------------------
00038   template <class TGenericObj>
00039   class GenericObjPtr
00040   {
00042     TGenericObj* myPointer;
00043 
00044     void
00045     swap(GenericObjPtr& thePointer)
00046     {
00047       TGenericObj* aPointer = thePointer.myPointer;
00048       thePointer.myPointer = this->myPointer;
00049       this->myPointer = aPointer;
00050     }
00051 
00052     void
00053     Register()
00054     {
00055       if(this->myPointer)
00056      this->myPointer->Register();
00057     }
00058 
00059     void
00060     UnRegister()
00061     {
00062       if(this->myPointer){
00063      this->myPointer->UnRegister();
00064      this->myPointer = NULL;
00065       }
00066     }
00067 
00068   public:
00070     GenericObjPtr():
00071       myPointer(NULL)
00072     {}
00073 
00075     template<class TGenObj>
00076     explicit
00077     GenericObjPtr(TGenObj* thePointer): 
00078       myPointer(thePointer) 
00079     {
00080       this->Register();
00081     }
00082 
00087     GenericObjPtr(const GenericObjPtr& thePointer):
00088       myPointer(thePointer.myPointer) 
00089     {
00090       this->Register();
00091     }
00092 
00097     template<class TGenObj>
00098     GenericObjPtr(const GenericObjPtr<TGenObj>& thePointer):
00099       myPointer(thePointer.get()) 
00100     {
00101       this->Register();
00102     }
00103 
00105     ~GenericObjPtr()
00106     {
00107       this->UnRegister();
00108     }
00109 
00114     template<class TGenObj>
00115     GenericObjPtr&
00116     operator=(TGenObj* thePointer)
00117     {
00118       GenericObjPtr aTmp(thePointer);
00119       aTmp.swap(*this);
00120       return *this;
00121     }
00122 
00127     GenericObjPtr& 
00128     operator=(const GenericObjPtr& thePointer)
00129     {
00130       GenericObjPtr aTmp(thePointer);
00131       aTmp.swap(*this);
00132       return *this;
00133     }
00134 
00139     template<class TGenObj>
00140     GenericObjPtr& 
00141     operator=(const GenericObjPtr<TGenObj>& thePointer)
00142     {
00143       GenericObjPtr aTmp(thePointer);
00144       aTmp.swap(*this);
00145       return *this;
00146     }
00147 
00149     virtual
00150     TGenericObj* 
00151     get() const
00152     {
00153       return this->myPointer;
00154     }
00155 
00157     operator TGenericObj* () const
00158     {
00159       return this->get();
00160     }
00161 
00166     TGenericObj& 
00167     operator*() const
00168     {
00169       return *this->get();
00170     }
00171 
00173     TGenericObj* operator->() const
00174     {
00175       return this->get();
00176     }
00177 
00178     operator bool () const
00179     {
00180       return this->get() != 0;
00181     }
00182   };
00183 }
00184 
00185 template<class T, class U> 
00186 inline 
00187 bool 
00188 operator<(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
00189 {
00190   return a.get() < b.get();
00191 }
00192 
00193 template<class T, class U> 
00194 inline
00195 bool 
00196 operator==(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
00197 {
00198   return a.get() == b.get();
00199 }
00200 
00201 template<class T, class U> 
00202 inline 
00203 bool 
00204 operator!=(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
00205 {
00206   return a.get() != b.get();
00207 }
00208 
00209 template<class Y> 
00210 std::ostream& 
00211 operator<< (std::ostream & os, SALOME::GenericObjPtr<Y> const & p)
00212 {
00213   os << p.get();
00214   return os;
00215 }
00216 
00217 
00218 #endif
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS