Back to index

salome-kernel  6.5.0
SALOMEDS_SObject.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 //  File   : SALOMEDS_SObject.hxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 //
00027 #include <string>
00028 
00029 #include "SALOMEDS_SObject.hxx"
00030 
00031 #include "SALOMEDS.hxx"
00032 #include "SALOMEDS_SComponent.hxx"
00033 #include "SALOMEDS_GenericAttribute.hxx"
00034 #include "SALOMEDS_Study.hxx"
00035 #include "SALOMEDS_SObject_i.hxx"
00036 
00037 #include "SALOMEDSImpl_SComponent.hxx"
00038 #include "SALOMEDSImpl_GenericAttribute.hxx"
00039 #include "SALOMEDSImpl_Study.hxx"
00040 
00041 #include "Utils_ORB_INIT.hxx" 
00042 #include "Utils_SINGLETON.hxx" 
00043 
00044 #include "Basics_Utils.hxx"
00045 
00046 #include "utilities.h"
00047 
00048 #ifdef WIN32
00049 #include <windows.h>
00050 #include <process.h>
00051 #else
00052 #include <sys/types.h>
00053 #include <unistd.h>
00054 #endif
00055 
00056 
00057 SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
00058 {
00059 #ifdef WIN32
00060   long pid =  (long)_getpid();
00061 #else
00062   long pid =  (long)getpid();
00063 #endif  
00064 
00065   CORBA::LongLong addr =  // mpv: fix for IPAL13534: for 64-bit platforms use 8-bytes long for pointer storage
00066   theSObject->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
00067 
00068   if(_isLocal) {
00069     _local_impl = reinterpret_cast<SALOMEDSImpl_SObject*>(addr);
00070     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
00071   }
00072   else {
00073     _local_impl = NULL;
00074     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
00075   }
00076 
00077   init_orb();
00078 }
00079 
00080 SALOMEDS_SObject::SALOMEDS_SObject(const SALOMEDSImpl_SObject& theSObject)
00081 :_isLocal(true)
00082 {
00083   _corba_impl = SALOMEDS::SObject::_nil();
00084 
00085   if(theSObject.IsComponent()) {
00086     SALOMEDSImpl_SComponent sco = theSObject;
00087     _local_impl = sco.GetPersistentCopy();
00088   }
00089   else {
00090     _local_impl = theSObject.GetPersistentCopy();
00091   }
00092 
00093   init_orb();
00094 }
00095 
00096 SALOMEDS_SObject::~SALOMEDS_SObject()
00097 {
00098   if (!_isLocal) {
00099     _corba_impl->UnRegister();
00100   }
00101   else {
00102     if(_local_impl) delete _local_impl;
00103   }
00104 }
00105 
00106 bool SALOMEDS_SObject::IsNull() const
00107 {
00108   return _isLocal ? ( !_local_impl || _local_impl->IsNull() ) : _corba_impl->IsNull();
00109 }
00110 
00111 std::string SALOMEDS_SObject::GetID()
00112 {
00113   std::string aValue;
00114   if (_isLocal) {
00115     SALOMEDS::Locker lock;
00116     aValue = _local_impl->GetID();
00117   }
00118   else aValue = (CORBA::String_var)_corba_impl->GetID();  
00119   return aValue;
00120 }
00121 
00122 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
00123 {
00124   if (_isLocal) {
00125     SALOMEDS::Locker lock;
00126     return _PTR(SComponent)(new SALOMEDS_SComponent(_local_impl->GetFatherComponent()));
00127   }
00128   return _PTR(SComponent)(new SALOMEDS_SComponent((SALOMEDS::SComponent_var)_corba_impl->GetFatherComponent()));
00129 }
00130 
00131 _PTR(SObject) SALOMEDS_SObject::GetFather()
00132 {
00133   if (_isLocal) {
00134     SALOMEDS::Locker lock;
00135     return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
00136   }
00137   return _PTR(SObject)(new SALOMEDS_SObject((SALOMEDS::SObject_var)_corba_impl->GetFather()));
00138 }
00139 
00140 bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute,
00141                                      const std::string& aTypeOfAttribute)
00142 {
00143   bool ret = false;
00144   if (_isLocal) {
00145     SALOMEDS::Locker lock;
00146     DF_Attribute* anAttr = NULL;
00147     ret = _local_impl->FindAttribute(anAttr, aTypeOfAttribute);
00148     if(ret) {
00149       SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
00150       anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(ga));
00151     }
00152   }
00153   else {
00154     SALOMEDS::GenericAttribute_var anAttr;
00155     ret = _corba_impl->FindAttribute(anAttr.out(), aTypeOfAttribute.c_str());
00156     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
00157   }
00158 
00159   return ret;
00160 }
00161 
00162 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
00163 {
00164   bool ret = false;
00165   if (_isLocal) {
00166     SALOMEDS::Locker lock;
00167     SALOMEDSImpl_SObject aSO;
00168     ret = _local_impl->ReferencedObject(aSO);
00169     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
00170   }
00171   else {
00172     SALOMEDS::SObject_var aSO;
00173     ret = _corba_impl->ReferencedObject(aSO.out());
00174     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
00175   }
00176 
00177   return ret; 
00178 }
00179 
00180 
00181 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
00182 {
00183   bool ret = false;
00184   if (_isLocal) {
00185     SALOMEDS::Locker lock;
00186     SALOMEDSImpl_SObject aSO;
00187     ret = _local_impl->FindSubObject(theTag, aSO);
00188     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
00189   }
00190   else {
00191     SALOMEDS::SObject_var aSO;
00192     ret = _corba_impl->FindSubObject(theTag, aSO.out());
00193     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
00194   }
00195 
00196   return ret;   
00197 }
00198 
00199 _PTR(Study) SALOMEDS_SObject::GetStudy()
00200 {
00201   if (_isLocal) {
00202     SALOMEDS::Locker lock;
00203     return _PTR(Study)(new SALOMEDS_Study(_local_impl->GetStudy()));
00204   }
00205   SALOMEDS::Study_var study=_corba_impl->GetStudy();
00206   return _PTR(Study)(new SALOMEDS_Study(study));
00207 }
00208 
00209 std::string SALOMEDS_SObject::Name()
00210 {
00211   std::string aName;
00212   if (_isLocal) {
00213     SALOMEDS::Locker lock;
00214     aName = _local_impl->Name();
00215   }
00216   else aName = (CORBA::String_var)_corba_impl->Name();
00217 
00218   return aName;
00219 }
00220 
00221 void  SALOMEDS_SObject::Name(const std::string& theName)
00222 {
00223   if (_isLocal) {
00224     SALOMEDS::Locker lock;
00225     _local_impl->Name(theName);
00226   }
00227   else _corba_impl->Name(theName.c_str());
00228 }
00229 
00230 std::vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
00231 {
00232   std::vector<_PTR(GenericAttribute)> aVector;
00233   int aLength = 0;
00234   SALOMEDSClient_GenericAttribute* anAttr;
00235 
00236   if (_isLocal) {
00237     SALOMEDS::Locker lock;
00238     std::vector<DF_Attribute*> aSeq = _local_impl->GetAllAttributes();
00239     aLength = aSeq.size();
00240     for (int i = 0; i < aLength; i++) {
00241       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]));
00242       aVector.push_back(_PTR(GenericAttribute)(anAttr));
00243     }
00244   }
00245   else {
00246     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
00247     aLength = aSeq->length();
00248     for (int i = 0; i < aLength; i++) {
00249       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
00250       aVector.push_back(_PTR(GenericAttribute)(anAttr));
00251     }
00252   }
00253 
00254   return aVector;
00255 }
00256 
00257 std::string SALOMEDS_SObject::GetName()
00258 {
00259   std::string aName;
00260   if (_isLocal) {
00261     SALOMEDS::Locker lock;
00262     aName = _local_impl->GetName();
00263   }
00264   else aName = (CORBA::String_var) _corba_impl->GetName();
00265 
00266   return aName;
00267 }
00268 
00269 std::string SALOMEDS_SObject::GetComment()
00270 {
00271   std::string aComment;
00272   if (_isLocal) {
00273     SALOMEDS::Locker lock;
00274     aComment = _local_impl->GetComment();
00275   }
00276   else aComment = (CORBA::String_var) _corba_impl->GetComment();
00277 
00278   return aComment;
00279 }
00280 
00281 std::string SALOMEDS_SObject::GetIOR()
00282 {
00283   std::string anIOR;
00284   if (_isLocal) {
00285     SALOMEDS::Locker lock;
00286     anIOR = _local_impl->GetIOR();
00287   }
00288   else anIOR = (CORBA::String_var) _corba_impl->GetIOR();
00289 
00290   return anIOR;
00291 }
00292 
00293 int SALOMEDS_SObject::Tag()
00294 {
00295   if (_isLocal) {
00296     SALOMEDS::Locker lock;
00297     return _local_impl->Tag();
00298   }
00299   return _corba_impl->Tag(); 
00300 }
00301 
00302 int SALOMEDS_SObject::Depth()
00303 {
00304   if (_isLocal) {
00305     SALOMEDS::Locker lock;
00306     return _local_impl->Depth();
00307   }
00308   return _corba_impl->Depth();  
00309 }
00310 
00311 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
00312 {
00313   CORBA::Object_var obj;
00314   if (_isLocal) {
00315     SALOMEDS::Locker lock;
00316     std::string anIOR = GetIOR();
00317     if (!anIOR.empty())
00318       obj = _orb->string_to_object(anIOR.c_str());
00319     return obj._retn();
00320   }
00321   else {
00322     obj = _corba_impl->GetObject();
00323     return obj._retn();
00324   }
00325 
00326   return CORBA::Object::_nil();
00327 }
00328 
00329 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
00330 {
00331   if(_isLocal) {
00332     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
00333     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(*_local_impl, _orb);
00334     _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
00335     return aSO._retn();
00336   }
00337   else {
00338     return SALOMEDS::SObject::_duplicate(_corba_impl);
00339   }
00340   return SALOMEDS::SObject::_nil();
00341 }
00342 
00343 
00344 void SALOMEDS_SObject::init_orb()
00345 {
00346   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
00347   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
00348   _orb = init(0 , 0 ) ;     
00349 }
00350 
00351 void SALOMEDS_SObject::SetAttrString(const std::string& name, const std::string& value)
00352 {
00353   if(_isLocal)
00354     {
00355       SALOMEDS::Locker lock;
00356       _local_impl->SetAttrString(name,value);
00357     }
00358   else
00359     {
00360       _corba_impl->SetAttrString(name.c_str(),value.c_str());
00361     }
00362 }