Back to index

salome-gui  6.5.0
SUIT_DataObjectKey.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  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 #include "SUIT_DataObjectKey.h"
00024 
00025 #include <string.h>
00026 
00027 #ifndef WIN32
00028 #include <typeinfo>
00029 #define _typeinfo std::type_info
00030 #else
00031 #include <typeinfo.h>
00032 #define _typeinfo type_info
00033 #endif
00034 
00042 SUIT_DataObjectKey::SUIT_DataObjectKey()
00043 : refCounter( 0 )
00044 {
00045 }
00046 
00050 SUIT_DataObjectKey::~SUIT_DataObjectKey()
00051 {
00052 }
00053 
00061 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle()
00062 : myKey( 0 )
00063 {
00064 }
00065 
00069 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( const SUIT_DataObjectKeyHandle& other )
00070 : myKey( other.myKey )
00071 {
00072   myKey = other.myKey;
00073 
00074   beginScope();
00075 }
00076 
00080 SUIT_DataObjectKeyHandle::SUIT_DataObjectKeyHandle( SUIT_DataObjectKey* key )
00081 : myKey( key )
00082 {
00083   beginScope();
00084 }
00085 
00089 SUIT_DataObjectKeyHandle::~SUIT_DataObjectKeyHandle()
00090 {
00091   nullify();
00092 }
00093 
00098 bool SUIT_DataObjectKeyHandle::isNull() const
00099 {
00100   return !myKey;
00101 }
00102 
00106 void SUIT_DataObjectKeyHandle::nullify()
00107 {
00108   endScope();
00109 }
00110 
00115 bool SUIT_DataObjectKeyHandle::operator<( const SUIT_DataObjectKeyHandle& kw ) const
00116 {
00117   if ( myKey == kw.myKey )
00118     return false;
00119 
00120   if ( !myKey || !kw.myKey )
00121     return myKey < kw.myKey;
00122 
00123   const _typeinfo& i1 = typeid( *myKey );
00124   const _typeinfo& i2 = typeid( *kw.myKey );
00125 
00126   int cmp = strcmp( i1.name(), i2.name() );
00127   if ( cmp < 0 )
00128     return true;
00129   else if ( cmp > 0 )
00130     return false;
00131   else
00132     return myKey->isLess( kw.myKey );
00133 }
00134 
00139 bool SUIT_DataObjectKeyHandle::operator==( const SUIT_DataObjectKeyHandle& kw ) const
00140 {
00141   if ( myKey == kw.myKey )
00142     return true;
00143 
00144   if ( !myKey || !kw.myKey )
00145     return false;
00146 
00147   if ( typeid( *myKey ) != typeid( *kw.myKey ) )
00148     return false;
00149 
00150   return myKey->isEqual( kw.myKey );
00151 }
00152 
00156 SUIT_DataObjectKeyHandle& SUIT_DataObjectKeyHandle::operator=( const SUIT_DataObjectKeyHandle& kw )
00157 {
00158   if ( myKey != kw.myKey )
00159   {
00160     endScope();
00161 
00162     myKey = kw.myKey;
00163 
00164     beginScope();
00165   }
00166   return *this;
00167 }
00168 
00172 void SUIT_DataObjectKeyHandle::beginScope()
00173 {
00174   if ( myKey )
00175     myKey->refCounter++;
00176 }
00177 
00181 void SUIT_DataObjectKeyHandle::endScope()
00182 {
00183   if ( !myKey )
00184     return;
00185 
00186   myKey->refCounter--;
00187 
00188   if ( !myKey->refCounter )
00189   {
00190     delete myKey;
00191     myKey = 0;
00192   }
00193 }