From c2925c556a18344ac254d176b84b8dffbe6cbf2f Mon Sep 17 00:00:00 2001 From: ouv Date: Wed, 29 Nov 2006 08:38:21 +0000 Subject: [PATCH] To introduce SALOME_GenericObjPtr - smart pointer for GenericObj --- src/VISU_I/Makefile.in | 10 +- src/VISU_I/SALOME_GenericObjPtr.cc | 126 ++++++++++++++++++++ src/VISU_I/SALOME_GenericObjPtr.hh | 156 +++++++++++++++++++++++++ src/VISU_I/VISU_ColoredPrs3dCache_i.cc | 26 +++-- src/VISU_I/VISU_ColoredPrs3dCache_i.hh | 15 +-- src/VISU_I/VISU_ColoredPrs3d_i.cc | 2 - 6 files changed, 310 insertions(+), 25 deletions(-) create mode 100644 src/VISU_I/SALOME_GenericObjPtr.cc create mode 100644 src/VISU_I/SALOME_GenericObjPtr.hh diff --git a/src/VISU_I/Makefile.in b/src/VISU_I/Makefile.in index 5e6ae2dd..bfdc2a50 100644 --- a/src/VISU_I/Makefile.in +++ b/src/VISU_I/Makefile.in @@ -48,8 +48,8 @@ LIB_SRC = \ VISU_Vectors_i.cc \ VISU_StreamLines_i.cc \ VISU_ScalarMapOnDeformedShape_i.cc \ - VISU_ColoredPrs3dHolder_i.cc \ VISU_ColoredPrs3dCache_i.cc \ + VISU_ColoredPrs3dHolder_i.cc \ VISU_ColoredPrs3dFactory.cc \ VISU_Mesh_i.cc \ VISU_ViewManager_i.cc \ @@ -58,7 +58,8 @@ LIB_SRC = \ VISU_Table_i.cc \ VISU_Gen_i.cc \ VISU_CorbaMedConvertor.cxx \ - VISU_DumpPython.cc + VISU_DumpPython.cc \ + SALOME_GenericObjPtr.cc LIB_MOC = \ VISU_TimeAnimation.h @@ -104,9 +105,10 @@ EXPORT_HEADERS = \ VISU_View_i.hh \ VISU_TimeAnimation.h \ VISU_ScalarMapOnDeformedShape_i.hh \ - VISU_ColoredPrs3dHolder_i.hh \ VISU_ColoredPrs3dCache_i.hh \ - VISU_ColoredPrs3dFactory.hh + VISU_ColoredPrs3dHolder_i.hh \ + VISU_ColoredPrs3dFactory.hh \ + SALOME_GenericObjPtr.hh # additionnal information to compil and link file CPPFLAGS += \ diff --git a/src/VISU_I/SALOME_GenericObjPtr.cc b/src/VISU_I/SALOME_GenericObjPtr.cc new file mode 100644 index 00000000..6b19648c --- /dev/null +++ b/src/VISU_I/SALOME_GenericObjPtr.cc @@ -0,0 +1,126 @@ +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// File : SALOME_GenericObjPtr.cc +// Author : Oleg UVAROV +// Module : SALOME + +#include "SALOME_GenericObjPtr.hh" + +using namespace SALOME; + +//---------------------------------------------------------------------------- +GenericObjPtrBase +::GenericObjPtrBase() : + Object(0) +{ + // Add a reference to the object. + this->Register(); +} + +//---------------------------------------------------------------------------- +GenericObjPtrBase +::GenericObjPtrBase(GenericObj_i* r): + Object(r) +{ + // Add a reference to the object. + this->Register(); +} + +//---------------------------------------------------------------------------- +GenericObjPtrBase +::GenericObjPtrBase(const GenericObjPtrBase& r): + Object(r.Object) +{ + // Add a reference to the object. + this->Register(); +} + +//---------------------------------------------------------------------------- +GenericObjPtrBase +::GenericObjPtrBase(GenericObj_i* r, const GenericObjPtrBase::NoReference&): + Object(r) +{ + // Do not add a reference to the object because we received the + // NoReference argument. +} + +//---------------------------------------------------------------------------- +GenericObjPtrBase +::~GenericObjPtrBase() +{ + // The main pointer must be set to NULL before calling UnRegister, + // so use a local variable to save the pointer. This is because the + // garbage collection reference graph traversal may make it back to + // this smart pointer, and we do not want to include this reference. + if(GenericObj_i* object = this->Object) + { + this->Object = 0; + object->Destroy(); + } +} + +//---------------------------------------------------------------------------- +GenericObjPtrBase& +GenericObjPtrBase +::operator=(GenericObj_i* r) +{ + // This is an exception-safe assignment idiom that also gives the + // correct order of register/unregister calls to all objects + // involved. A temporary is constructed that references the new + // object. Then the main pointer and temporary are swapped and the + // temporary's destructor unreferences the old object. + GenericObjPtrBase(r).Swap(*this); + return *this; +} + +//---------------------------------------------------------------------------- +GenericObjPtrBase& +GenericObjPtrBase +::operator=(const GenericObjPtrBase& r) +{ + // This is an exception-safe assignment idiom that also gives the + // correct order of register/unregister calls to all objects + // involved. A temporary is constructed that references the new + // object. Then the main pointer and temporary are swapped and the + // temporary's destructor unreferences the old object. + GenericObjPtrBase(r).Swap(*this); + return *this; +} + +//---------------------------------------------------------------------------- +void GenericObjPtrBase +::Swap(GenericObjPtrBase& r) +{ + // Just swap the pointers. This is used internally by the + // assignment operator. + GenericObj_i* temp = r.Object; + r.Object = this->Object; + this->Object = temp; +} + +//---------------------------------------------------------------------------- +void GenericObjPtrBase +::Register() +{ + // Add a reference only if the object is not NULL. + if(this->Object) + this->Object->Register(); +} diff --git a/src/VISU_I/SALOME_GenericObjPtr.hh b/src/VISU_I/SALOME_GenericObjPtr.hh new file mode 100644 index 00000000..ecccd2db --- /dev/null +++ b/src/VISU_I/SALOME_GenericObjPtr.hh @@ -0,0 +1,156 @@ +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// File : SALOME_GenericObjPtr.hh +// Author : Oleg UVAROV +// Module : SALOME + +#ifndef SALOME_GenericObjPtr_HH +#define SALOME_GenericObjPtr_HH + +#include "SALOME_GenericObj_i.hh" + +namespace SALOME +{ + class GenericObjPtrBase + { + public: + // Description: + // Initialize smart pointer to NULL. + GenericObjPtrBase(); + + // Description: + // Initialize smart pointer to given object. + GenericObjPtrBase( GenericObj_i* ); + + // Description: + // Initialize smart pointer with a new reference to the same object + // referenced by given smart pointer. + GenericObjPtrBase( const GenericObjPtrBase& ); + + // Description: + // Destroy smart pointer and remove the reference to its object. + ~GenericObjPtrBase(); + + // Description: + // Assign object to reference. This removes any reference to an old object. + GenericObjPtrBase& operator=( GenericObj_i* ); + GenericObjPtrBase& operator=( const GenericObjPtrBase& ); + + // Description: + // Get the contained pointer. + GenericObj_i* GetPointer() const + { + return this->Object; + } + + protected: + // Initialize smart pointer to given object, but do not increment + // reference count. The destructor will still decrement the count. + // This effectively makes it an auto-ptr. + class NoReference {}; + GenericObjPtrBase(GenericObj_i* r, const NoReference&); + + // Pointer to the actual object. + GenericObj_i* Object; + + private: + // Internal utility methods. + void Swap( GenericObjPtrBase& ); + void Register(); + }; + + template + class GenericObjPtr: public GenericObjPtrBase + { + public: + // Description: + // Initialize smart pointer to NULL. + GenericObjPtr() {} + + // Description: + // Initialize smart pointer to given object. + GenericObjPtr(T* r): GenericObjPtrBase(r) {} + + // Description: + // Initialize smart pointer with a new reference to the same object + // referenced by given smart pointer. + GenericObjPtr(const GenericObjPtrBase& r): GenericObjPtrBase(r) {} + + // Description: + // Assign object to reference. This removes any reference to an old + // object. + GenericObjPtr& operator=(T* r) + { + this->GenericObjPtrBase::operator=(r); + return *this; + } + + // Description: + // Assign object to reference. This removes any reference to an old + // object. + GenericObjPtr& operator=(const GenericObjPtrBase& r) + { + this->GenericObjPtrBase::operator=(r); + return *this; + } + + // Description: + // Get the contained pointer. + T* GetPointer() const + { + return dynamic_cast(this->Object); + } + + // Description: + // Get the contained pointer. + operator T* () const + { + return dynamic_cast(this->Object); + } + + // Description: + // Dereference the pointer and return a reference to the contained + // object. + T& operator*() const + { + return *dynamic_cast(this->Object); + } + + // Description: + // Provides normal pointer target member access using operator ->. + T* operator->() const + { + return dynamic_cast(this->Object); + } + + // Description: + // Create an instance of an object. + static GenericObjPtr New() + { + return GenericObjPtr(T::New(), NoReference()); + } + + protected: + GenericObjPtr(T* r, const NoReference& n): GenericObjPtrBase(r, n) {} + }; +} + +#endif diff --git a/src/VISU_I/VISU_ColoredPrs3dCache_i.cc b/src/VISU_I/VISU_ColoredPrs3dCache_i.cc index 478c2eee..43acacb4 100644 --- a/src/VISU_I/VISU_ColoredPrs3dCache_i.cc +++ b/src/VISU_I/VISU_ColoredPrs3dCache_i.cc @@ -50,6 +50,7 @@ static int MYDEBUG = 0; static int MYDEBUG = 0; #endif + //---------------------------------------------------------------------------- VISU::ColoredPrs3dCache_i ::ColoredPrs3dCache_i(SALOMEDS::Study_ptr theStudy): @@ -106,7 +107,7 @@ VISU::ColoredPrs3dCache_i TLastVisitedPrsList::const_iterator aPrsIter = aPrsList.begin(); TLastVisitedPrsList::const_iterator aPrsIterEnd = aPrsList.end(); for(; aPrsIter != aPrsIterEnd; aPrsIter++){ - if(VISU::ColoredPrs3d_i* aPrs3d = *aPrsIter) + if(TPrs3dPtr aPrs3d = *aPrsIter) aMemoryUsed += aPrs3d->GetMemorySize(); } } @@ -263,8 +264,11 @@ VISU::ColoredPrs3dCache_i ::RegisterInHolder(VISU::ColoredPrs3d_i* thePrs3d, VISU::ColoredPrs3dHolder_i* theHolder) { - thePrs3d->SetHolderEntry( theHolder->GetEntry() ); - GetLastVisitedPrsList(theHolder).push_front(thePrs3d); + TPrs3dPtr aPrs3d( thePrs3d ); + aPrs3d->Destroy(); + + aPrs3d->SetHolderEntry( theHolder->GetEntry() ); + GetLastVisitedPrsList(theHolder).push_front(aPrs3d); return thePrs3d; } @@ -285,14 +289,12 @@ VISU::TLastVisitedPrsList& VISU::ColoredPrs3dCache_i ::GetLastVisitedPrsList(VISU::ColoredPrs3dHolder_i* theHolder) { - //cout << "VISU::ColoredPrs3dCache_i::GetLastVisitedPrsList("; - //cout << theHolder->GetEntry().latin1() << ")" << endl; return myHolderMap[theHolder->GetEntry()]; } //---------------------------------------------------------------------------- -VISU::ColoredPrs3d_i* +VISU::TPrs3dPtr VISU::ColoredPrs3dCache_i ::GetLastVisitedPrs(VISU::ColoredPrs3dHolder_i* theHolder) { @@ -305,7 +307,7 @@ VISU::ColoredPrs3dCache_i //---------------------------------------------------------------------------- -VISU::ColoredPrs3d_i* +VISU::TPrs3dPtr VISU::ColoredPrs3dCache_i ::FindPrsByInput(TLastVisitedPrsList& theLastVisitedPrsList, const VISU::ColoredPrs3dHolder::BasicInput& theInput) @@ -315,7 +317,7 @@ VISU::ColoredPrs3dCache_i TLastVisitedPrsList::iterator aEndIter = theLastVisitedPrsList.end(); for(; anIter != aEndIter; anIter++) { - VISU::ColoredPrs3d_i* aPrs3d = *anIter; + TPrs3dPtr aPrs3d = *anIter; VISU::ColoredPrs3dHolder::BasicInput_var anInput = aPrs3d->GetBasicInput(); if(anInput->myResult->_is_equivalent(theInput.myResult) && !strcmp(anInput->myMeshName.in(), theInput.myMeshName.in()) && @@ -360,11 +362,11 @@ VISU::ColoredPrs3dCache_i VISU::View3D_ptr theView3D) { //cout << "VISU::ColoredPrs3dCache_i::UpdateLastVisitedPrs" << endl; - VISU::ColoredPrs3d_i* aLastVisitedPrs3d = GetLastVisitedPrs(theHolder); + TPrs3dPtr aLastVisitedPrs3d = GetLastVisitedPrs(theHolder); TLastVisitedPrsList& aLastVisitedPrsList = GetLastVisitedPrsList(theHolder); - VISU::ColoredPrs3d_i* aPrs3d = FindPrsByInput(aLastVisitedPrsList, theInput); + TPrs3dPtr aPrs3d = FindPrsByInput(aLastVisitedPrsList, theInput); bool anIsCheckPossible = GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED; - if(aPrs3d){ + if(aPrs3d.GetPointer()){ aLastVisitedPrsList.push_front(aPrs3d); //cout << "FindPrsByInput " << aPrs3d; }else if(anIsCheckPossible && IsPossible(theHolder->GetPrsType(), theInput)){ @@ -395,7 +397,7 @@ VISU::ColoredPrs3dCache_i vtkActorCollection* anActors = aRenderer->GetActors(); VISU_Actor* anActor = SVTK::Find(anActors, - VISU::TIsSamePrs3d(aLastVisitedPrs3d)); + VISU::TIsSamePrs3d(aLastVisitedPrs3d.GetPointer())); // If the holder was erased from view then do nothing if(!anActor || !anActor->GetVisibility()) return true; diff --git a/src/VISU_I/VISU_ColoredPrs3dCache_i.hh b/src/VISU_I/VISU_ColoredPrs3dCache_i.hh index ecb15ff6..7737184a 100644 --- a/src/VISU_I/VISU_ColoredPrs3dCache_i.hh +++ b/src/VISU_I/VISU_ColoredPrs3dCache_i.hh @@ -27,20 +27,21 @@ #ifndef VISU_ColoredPrs3dCache_i_HeaderFile #define VISU_ColoredPrs3dCache_i_HeaderFile -#include "VISU_PrsObject_i.hh" - -#include "SALOME_GenericObj_i.hh" - #include "VISU_ColoredPrs3dFactory.hh" +#include "SALOME_GenericObjPtr.hh" + namespace VISU { class Result_i; class ColoredPrs3d_i; class ColoredPrs3dHolder_i; + typedef SALOME::GenericObjPtr TPrs3dPtr; + typedef std::deque TLastVisitedPrsList; + typedef std::string THolderEntry; - typedef std::deque TLastVisitedPrsList; + //typedef std::deque TLastVisitedPrsList; typedef std::map TColoredPrs3dHolderMap; /*! @@ -150,7 +151,7 @@ namespace VISU TLastVisitedPrsList& GetLastVisitedPrsList(VISU::ColoredPrs3dHolder_i* theHolder); - VISU::ColoredPrs3d_i* + TPrs3dPtr GetLastVisitedPrs(VISU::ColoredPrs3dHolder_i* theHolder); bool @@ -164,7 +165,7 @@ namespace VISU IsPossible(VISU::VISUType theType, const VISU::ColoredPrs3dHolder::BasicInput& theInput); - VISU::ColoredPrs3d_i* + TPrs3dPtr FindPrsByInput(TLastVisitedPrsList& theLastVisitedPrsList, const VISU::ColoredPrs3dHolder::BasicInput& theInput); diff --git a/src/VISU_I/VISU_ColoredPrs3d_i.cc b/src/VISU_I/VISU_ColoredPrs3d_i.cc index 34429154..6dc381dd 100644 --- a/src/VISU_I/VISU_ColoredPrs3d_i.cc +++ b/src/VISU_I/VISU_ColoredPrs3d_i.cc @@ -106,7 +106,6 @@ bool VISU::ColoredPrs3d_i ::SetInput() { - cout << "VISU::ColoredPrs3d_i::SetInput()" << endl; try{ if(TSuperClass::SetInput()){ if(CheckIsPossible()){ @@ -119,7 +118,6 @@ VISU::ColoredPrs3d_i } }catch(...){ } - cout << "VISU::ColoredPrs3d_i::SetInput() finished" << endl; return false; } -- 2.39.2