Salome HOME
Fix for the bug IPAL22164: Fatal error during save/load a SMESH study
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeIOR.cxx
index ab6b6f11c80b471291b62a5a317bd3664601fbcd..668e1a142a8695878e0e50c99a3147b93c946a4d 100644 (file)
@@ -1,27 +1,97 @@
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+//  Copyright (C) 2003-2007  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   : SALOMEDSImpl_AttributeIOR.cxx
 //  Author : Sergey RUIN
 //  Module : SALOME
-
-#ifndef WNT
-using namespace std;
-#endif
+//
 #include "SALOMEDSImpl_AttributeIOR.hxx"
-#include <TCollection_AsciiString.hxx>
 #include "SALOMEDSImpl_Study.hxx"
-#include <Standard_GUID.hxx>
 
-IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeIOR, SALOMEDSImpl_GenericAttribute )
-IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeIOR, SALOMEDSImpl_GenericAttribute )
+//to disable automatic genericobj management comment the following line
+#define WITHGENERICOBJ
+
+#ifdef WITHGENERICOBJ
+#include "SALOME_GenericObj_i.hh"
+
+static CORBA::ORB_var getORB()
+{
+  int argc=0;
+  return CORBA::ORB_init(argc,0);
+}
+
+void IORGenericObjDecref(const std::string& anIOR)
+{
+  if(anIOR=="")return;
+  CORBA::Object_var obj;
+  SALOME::GenericObj_var gobj;
+  try
+    {
+      obj = getORB()->string_to_object(anIOR.c_str());
+      if(obj->_non_existent())return;
+      gobj = SALOME::GenericObj::_narrow(obj);
+      if(! CORBA::is_nil(gobj) )
+        {
+          gobj->Destroy();
+        }
+    }
+  catch(const CORBA::Exception& e)
+    {
+    }
+}
 
+void IORGenericObjIncref(const std::string& anIOR)
+{
+  CORBA::Object_var obj;
+  SALOME::GenericObj_var gobj;
+  try
+    {
+      obj = getORB()->string_to_object(anIOR.c_str());
+      if(obj->_non_existent())return;
+      gobj = SALOME::GenericObj::_narrow(obj);
+      if(! CORBA::is_nil(gobj) )
+        {
+          gobj->Register();
+        }
+    }
+  catch(const CORBA::Exception& e)
+    {
+    }
+}
+#else
+void IORGenericObjDecref(const std::string& anIOR)
+{}
+void IORGenericObjIncref(const std::string& anIOR)
+{}
+#endif
 
 //=======================================================================
 //function : GetID
 //purpose  : 
 //=======================================================================
 
-const Standard_GUID& SALOMEDSImpl_AttributeIOR::GetID () 
+const std::string& SALOMEDSImpl_AttributeIOR::GetID () 
 {
-  static Standard_GUID SALOMEDSImpl_AttributeIORID ("92888E01-7074-11d5-A690-0800369C8A03");
+  static std::string SALOMEDSImpl_AttributeIORID ("92888E01-7074-11d5-A690-0800369C8A03");
   return SALOMEDSImpl_AttributeIORID;
 }
 
@@ -32,17 +102,16 @@ const Standard_GUID& SALOMEDSImpl_AttributeIOR::GetID ()
 //purpose  : 
 //=======================================================================
 
-Handle(SALOMEDSImpl_AttributeIOR) SALOMEDSImpl_AttributeIOR::Set (const TDF_Label& L,
-                                                                 const TCollection_ExtendedString& S) 
+SALOMEDSImpl_AttributeIOR* SALOMEDSImpl_AttributeIOR::Set (const DF_Label& L,
+                                                           const std::string& S) 
 {
-  Handle(SALOMEDSImpl_AttributeIOR) A;
-  if (!L.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(),A)) {
+  SALOMEDSImpl_AttributeIOR* A = NULL;
+  if (!(A=(SALOMEDSImpl_AttributeIOR*)L.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
     A = new  SALOMEDSImpl_AttributeIOR(); 
     L.AddAttribute(A);
   }
 
   A->SetValue(S); 
-  SALOMEDSImpl_Study::IORUpdated(A);
   return A;
 }
 
@@ -50,27 +119,32 @@ Handle(SALOMEDSImpl_AttributeIOR) SALOMEDSImpl_AttributeIOR::Set (const TDF_Labe
 //function : SetValue
 //purpose  : 
 //=======================================================================
-void SALOMEDSImpl_AttributeIOR::SetValue(const TCollection_ExtendedString& theValue)
+void SALOMEDSImpl_AttributeIOR::SetValue(const std::string& theValue)
 {
   CheckLocked();
 
-  Handle(SALOMEDSImpl_Study) aStudy = SALOMEDSImpl_Study::GetStudy(Label());
-  aStudy->AddCreatedPostponed(theValue);
-  aStudy->AddPostponed(theValue);
+  SALOMEDSImpl_Study* study=SALOMEDSImpl_Study::GetStudy(Label());
 
   Backup();
+  //remove IOR entry in study
+  if(theValue != myString)
+    {
+      IORGenericObjIncref(theValue);
+      IORGenericObjDecref(myString);
+      study->DeleteIORLabelMapItem(myString);
+    }
+
   myString = theValue;
 
+  //add IOR entry in study
   SALOMEDSImpl_Study::IORUpdated(this);
-
-  //SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
 }
 
 //=======================================================================
 //function : Value
 //purpose  : 
 //=======================================================================
-TCollection_ExtendedString SALOMEDSImpl_AttributeIOR::Value() const
+std::string SALOMEDSImpl_AttributeIOR::Value() const
 {
   return myString;
 }
@@ -84,12 +158,17 @@ SALOMEDSImpl_AttributeIOR::SALOMEDSImpl_AttributeIOR()
 {
 }
 
+SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR()
+{
+  IORGenericObjDecref(myString);
+}
+
 //=======================================================================
 //function : ID
 //purpose  : 
 //=======================================================================
 
-const Standard_GUID& SALOMEDSImpl_AttributeIOR::ID () const { return GetID(); }
+const std::string& SALOMEDSImpl_AttributeIOR::ID () const { return GetID(); }
 
 
 //=======================================================================
@@ -97,7 +176,7 @@ const Standard_GUID& SALOMEDSImpl_AttributeIOR::ID () const { return GetID(); }
 //purpose  : 
 //=======================================================================
 
-Handle(TDF_Attribute) SALOMEDSImpl_AttributeIOR::NewEmpty () const
+DF_Attribute* SALOMEDSImpl_AttributeIOR::NewEmpty () const
 {  
   return new SALOMEDSImpl_AttributeIOR(); 
 }
@@ -107,9 +186,9 @@ Handle(TDF_Attribute) SALOMEDSImpl_AttributeIOR::NewEmpty () const
 //purpose  : 
 //=======================================================================
 
-void SALOMEDSImpl_AttributeIOR::Restore(const Handle(TDF_Attribute)& with) 
+void SALOMEDSImpl_AttributeIOR::Restore( DF_Attribute* with) 
 {
-  myString = Handle(SALOMEDSImpl_AttributeIOR)::DownCast (with)->Value();
+  myString = dynamic_cast<SALOMEDSImpl_AttributeIOR*>(with)->Value();
   return;
 }
 
@@ -118,9 +197,8 @@ void SALOMEDSImpl_AttributeIOR::Restore(const Handle(TDF_Attribute)& with)
 //purpose  : 
 //=======================================================================
 
-void SALOMEDSImpl_AttributeIOR::Paste (const Handle(TDF_Attribute)& into,
-                                      const Handle(TDF_RelocationTable)& RT) const
+void SALOMEDSImpl_AttributeIOR::Paste (DF_Attribute* into)
 {
-  Handle(SALOMEDSImpl_AttributeIOR)::DownCast (into)->SetValue(myString);
+  dynamic_cast<SALOMEDSImpl_AttributeIOR*>(into)->SetValue(myString);
 }