Salome HOME
Updated copyright comment
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject.cxx
index eaf664e03cbd987f459c06f659255a7838005b5f..97debc4360a9e4487850a4c1bc286a7a4c3b345a 100644 (file)
@@ -1,54 +1,57 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2024  CEA, EDF, 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 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// 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 
+// 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/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : SALOMEDS_SObject.hxx
 //  Author : Sergey RUIN
 //  Module : SALOME
-
-
-
+//
 #include <string>
-#include <TCollection_AsciiString.hxx> 
-#include <TColStd_HSequenceOfTransient.hxx>
 
 #include "SALOMEDS_SObject.hxx"
+
+#include "SALOMEDS.hxx"
 #include "SALOMEDS_SComponent.hxx"
-#include "SALOMEDSImpl_SComponent.hxx"
 #include "SALOMEDS_GenericAttribute.hxx"
-#include "SALOMEDSImpl_GenericAttribute.hxx"
 #include "SALOMEDS_Study.hxx"
+#include "SALOMEDS_SObject_i.hxx"
+
+#include "SALOMEDSImpl_SComponent.hxx"
+#include "SALOMEDSImpl_GenericAttribute.hxx"
 #include "SALOMEDSImpl_Study.hxx"
 
-#include "Utils_ORB_INIT.hxx" 
-#include "Utils_SINGLETON.hxx" 
+#include "OpUtil.hxx"
+
+#include "Basics_Utils.hxx"
+
+#include "utilities.h"
 
 #ifdef WIN32
+#include <windows.h>
 #include <process.h>
 #else
 #include <sys/types.h>
 #include <unistd.h>
 #endif
 
-#include "OpUtil.hxx"
-#include "utilities.h"
-
-using namespace std;  
 
 SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
 {
@@ -58,9 +61,11 @@ SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
   long pid =  (long)getpid();
 #endif  
 
-  long addr = theSObject->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
+  CORBA::LongLong addr =  // mpv: fix for IPAL13534: for 64-bit platforms use 8-bytes long for pointer storage
+  theSObject->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
+
   if(_isLocal) {
-    _local_impl = ((SALOMEDSImpl_SObject*)(addr));
+    _local_impl = reinterpret_cast<SALOMEDSImpl_SObject*>(addr);
     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
   }
   else {
@@ -71,11 +76,18 @@ SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
   init_orb();
 }
 
-SALOMEDS_SObject::SALOMEDS_SObject(const Handle(SALOMEDSImpl_SObject)& theSObject)
+SALOMEDS_SObject::SALOMEDS_SObject(const SALOMEDSImpl_SObject& theSObject)
 :_isLocal(true)
 {
   _corba_impl = SALOMEDS::SObject::_nil();
-  _local_impl = theSObject;
+
+  if(theSObject.IsComponent()) {
+    SALOMEDSImpl_SComponent sco = theSObject;
+    _local_impl = sco.GetPersistentCopy();
+  }
+  else {
+    _local_impl = theSObject.GetPersistentCopy();
+  }
 
   init_orb();
 }
@@ -83,40 +95,59 @@ SALOMEDS_SObject::SALOMEDS_SObject(const Handle(SALOMEDSImpl_SObject)& theSObjec
 SALOMEDS_SObject::~SALOMEDS_SObject()
 {
   if (!_isLocal) {
-    _corba_impl->Destroy();
+    _corba_impl->UnRegister();
   }
+  else {
+    if(_local_impl) delete _local_impl;
+  }
+}
+
+bool SALOMEDS_SObject::IsNull() const
+{
+  return _isLocal ? ( !_local_impl || _local_impl->IsNull() ) : _corba_impl->IsNull();
 }
 
 std::string SALOMEDS_SObject::GetID()
 {
   std::string aValue;
-  if(_isLocal) aValue = _local_impl->GetID().ToCString();
-  else aValue = _corba_impl->GetID();  
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    aValue = _local_impl->GetID();
+  }
+  else aValue = (CORBA::String_var)_corba_impl->GetID();  
   return aValue;
 }
 
 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
 {
-  if(_isLocal) {
-    Handle(SALOMEDSImpl_SComponent) aSCO = Handle(SALOMEDSImpl_SComponent)::DownCast(_local_impl->GetFatherComponent());
-    return _PTR(SComponent)(new SALOMEDS_SComponent(aSCO));
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    return _PTR(SComponent)(new SALOMEDS_SComponent(_local_impl->GetFatherComponent()));
   }
-  return _PTR(SComponent)(new SALOMEDS_SComponent(_corba_impl->GetFatherComponent()));
+  return _PTR(SComponent)(new SALOMEDS_SComponent((SALOMEDS::SComponent_var)_corba_impl->GetFatherComponent()));
 }
 
 _PTR(SObject) SALOMEDS_SObject::GetFather()
 {
-  if(_isLocal) return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
-  return _PTR(SObject)(new SALOMEDS_SObject(_corba_impl->GetFather()));
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
+  }
+  return _PTR(SObject)(new SALOMEDS_SObject((SALOMEDS::SObject_var)_corba_impl->GetFather()));
 }
 
-bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute, const std::string& aTypeOfAttribute)
+bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute,
+                                     const std::string& aTypeOfAttribute)
 {
   bool ret = false;
-  if(_isLocal) {
-    Handle(SALOMEDSImpl_GenericAttribute) anAttr;
-    ret = _local_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute.c_str());
-    if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    DF_Attribute* anAttr = NULL;
+    ret = _local_impl->FindAttribute(anAttr, aTypeOfAttribute);
+    if(ret) {
+      SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
+      anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(ga));
+    }
   }
   else {
     SALOMEDS::GenericAttribute_var anAttr;
@@ -130,8 +161,9 @@ bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute, const
 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
 {
   bool ret = false;
-  if(_isLocal) {
-    Handle(SALOMEDSImpl_SObject) aSO;
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    SALOMEDSImpl_SObject aSO;
     ret = _local_impl->ReferencedObject(aSO);
     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
   }
@@ -148,8 +180,9 @@ bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
 {
   bool ret = false;
-  if(_isLocal) {
-    Handle(SALOMEDSImpl_SObject) aSO;
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    SALOMEDSImpl_SObject aSO;
     ret = _local_impl->FindSubObject(theTag, aSO);
     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
   }
@@ -162,45 +195,46 @@ bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
   return ret;   
 }
 
-_PTR(Study) SALOMEDS_SObject::GetStudy()
-{
-  if(_isLocal) return _PTR(Study)(new SALOMEDS_Study(_local_impl->GetStudy()));
-  return _PTR(Study)(new SALOMEDS_Study(_corba_impl->GetStudy()));
-}
-
 std::string SALOMEDS_SObject::Name()
 {
   std::string aName;
-  if(_isLocal) aName = _local_impl->Name().ToCString();
-  else aName = _corba_impl->Name();
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    aName = _local_impl->Name();
+  }
+  else aName = (CORBA::String_var)_corba_impl->Name();
 
   return aName;
 }
 
 void  SALOMEDS_SObject::Name(const std::string& theName)
 {
-  if(_isLocal) _local_impl->Name((char*)theName.c_str());
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    _local_impl->Name(theName);
+  }
   else _corba_impl->Name(theName.c_str());
 }
 
-vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
+std::vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
 {
-  vector<_PTR(GenericAttribute)> aVector;
+  std::vector<_PTR(GenericAttribute)> aVector;
   int aLength = 0;
   SALOMEDSClient_GenericAttribute* anAttr;
 
-  if(_isLocal) {
-    Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetAllAttributes();
-    aLength = aSeq->Length();
-    for(int i = 1; i <= aLength; i++) {
-      anAttr = SALOMEDS_GenericAttribute::CreateAttribute(Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aSeq->Value(i)));
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    std::vector<DF_Attribute*> aSeq = _local_impl->GetAllAttributes();
+    aLength = (int)aSeq.size(); //!< TODO: conversion from size_t to int
+    for (int i = 0; i < aLength; i++) {
+      anAttr = SALOMEDS_GenericAttribute::CreateAttribute(dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]));
       aVector.push_back(_PTR(GenericAttribute)(anAttr));
     }
   }
   else {
     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
     aLength = aSeq->length();
-    for(int i = 0; i < aLength; i++) {
+    for (int i = 0; i < aLength; i++) {
       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
       aVector.push_back(_PTR(GenericAttribute)(anAttr));
     }
@@ -212,8 +246,11 @@ vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
 std::string SALOMEDS_SObject::GetName()
 {
   std::string aName;
-  if(_isLocal) aName = _local_impl->GetName().ToCString();
-  else aName = _corba_impl->GetName();
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    aName = _local_impl->GetName();
+  }
+  else aName = (CORBA::String_var) _corba_impl->GetName();
 
   return aName;
 }
@@ -221,8 +258,11 @@ std::string SALOMEDS_SObject::GetName()
 std::string SALOMEDS_SObject::GetComment()
 {
   std::string aComment;
-  if(_isLocal) aComment = _local_impl->GetComment().ToCString();
-  else aComment = _corba_impl->GetComment();
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    aComment = _local_impl->GetComment();
+  }
+  else aComment = (CORBA::String_var) _corba_impl->GetComment();
 
   return aComment;
 }
@@ -230,28 +270,47 @@ std::string SALOMEDS_SObject::GetComment()
 std::string SALOMEDS_SObject::GetIOR()
 {
   std::string anIOR;
-  if(_isLocal) anIOR = _local_impl->GetIOR().ToCString();
-  else anIOR = _corba_impl->GetIOR();
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    anIOR = _local_impl->GetIOR();
+  }
+  else anIOR = (CORBA::String_var) _corba_impl->GetIOR();
 
   return anIOR;
 }
 
 int SALOMEDS_SObject::Tag()
 {
-  if(_isLocal) return _local_impl->Tag();
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    return _local_impl->Tag();
+  }
   return _corba_impl->Tag(); 
 }
 
+int SALOMEDS_SObject::GetLastChildTag()
+{
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    return _local_impl->GetLastChildTag();
+  }
+  return _corba_impl->GetLastChildTag(); 
+}
+
 int SALOMEDS_SObject::Depth()
 {
-  if(_isLocal) return _local_impl->Depth();
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    return _local_impl->Depth();
+  }
   return _corba_impl->Depth();  
 }
 
 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
 {
   CORBA::Object_var obj;
-  if(_isLocal) {
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
     std::string anIOR = GetIOR();
     if (!anIOR.empty())
       obj = _orb->string_to_object(anIOR.c_str());
@@ -268,12 +327,13 @@ CORBA::Object_ptr SALOMEDS_SObject::GetObject()
 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
 {
   if(_isLocal) {
-    if(!CORBA::is_nil(_corba_impl)) return _corba_impl;
-    SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(_local_impl, _orb);
+    if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
+    SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(*_local_impl, _orb);
+    _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
     return aSO._retn();
   }
   else {
-    return _corba_impl;
+    return SALOMEDS::SObject::_duplicate(_corba_impl);
   }
   return SALOMEDS::SObject::_nil();
 }
@@ -281,7 +341,18 @@ SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
 
 void SALOMEDS_SObject::init_orb()
 {
-  ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
-  ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
-  _orb = init(0 , 0 ) ;     
+  _orb = KERNEL::GetRefToORB();    
+}
+
+void SALOMEDS_SObject::SetAttrString(const std::string& name, const std::string& value)
+{
+  if(_isLocal)
+    {
+      SALOMEDS::Locker lock;
+      _local_impl->SetAttrString(name,value);
+    }
+  else
+    {
+      _corba_impl->SetAttrString(name.c_str(),value.c_str());
+    }
 }