Salome HOME
31af76629d243a8897d0ca0a611bae07e0e664ee
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDS_SObject.hxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include <string>
28
29 #include "SALOMEDS_SObject.hxx"
30
31 #include "SALOMEDS.hxx"
32 #include "SALOMEDS_SComponent.hxx"
33 #include "SALOMEDS_GenericAttribute.hxx"
34 #include "SALOMEDS_Study.hxx"
35 #include "SALOMEDS_SObject_i.hxx"
36
37 #include "SALOMEDSImpl_SComponent.hxx"
38 #include "SALOMEDSImpl_GenericAttribute.hxx"
39 #include "SALOMEDSImpl_Study.hxx"
40
41 #include "OpUtil.hxx"
42
43 #include "Basics_Utils.hxx"
44
45 #include "utilities.h"
46
47 #ifdef WIN32
48 #include <windows.h>
49 #include <process.h>
50 #else
51 #include <sys/types.h>
52 #include <unistd.h>
53 #endif
54
55
56 SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
57 {
58 #ifdef WIN32
59   long pid =  (long)_getpid();
60 #else
61   long pid =  (long)getpid();
62 #endif  
63
64   CORBA::LongLong addr =  // mpv: fix for IPAL13534: for 64-bit platforms use 8-bytes long for pointer storage
65   theSObject->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
66
67   if(_isLocal) {
68     _local_impl = reinterpret_cast<SALOMEDSImpl_SObject*>(addr);
69     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
70   }
71   else {
72     _local_impl = NULL;
73     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
74   }
75
76   init_orb();
77 }
78
79 SALOMEDS_SObject::SALOMEDS_SObject(const SALOMEDSImpl_SObject& theSObject)
80 :_isLocal(true)
81 {
82   _corba_impl = SALOMEDS::SObject::_nil();
83
84   if(theSObject.IsComponent()) {
85     SALOMEDSImpl_SComponent sco = theSObject;
86     _local_impl = sco.GetPersistentCopy();
87   }
88   else {
89     _local_impl = theSObject.GetPersistentCopy();
90   }
91
92   init_orb();
93 }
94
95 SALOMEDS_SObject::~SALOMEDS_SObject()
96 {
97   if (!_isLocal) {
98     _corba_impl->UnRegister();
99   }
100   else {
101     if(_local_impl) delete _local_impl;
102   }
103 }
104
105 bool SALOMEDS_SObject::IsNull() const
106 {
107   return _isLocal ? ( !_local_impl || _local_impl->IsNull() ) : _corba_impl->IsNull();
108 }
109
110 std::string SALOMEDS_SObject::GetID()
111 {
112   std::string aValue;
113   if (_isLocal) {
114     SALOMEDS::Locker lock;
115     aValue = _local_impl->GetID();
116   }
117   else aValue = (CORBA::String_var)_corba_impl->GetID();  
118   return aValue;
119 }
120
121 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
122 {
123   if (_isLocal) {
124     SALOMEDS::Locker lock;
125     return _PTR(SComponent)(new SALOMEDS_SComponent(_local_impl->GetFatherComponent()));
126   }
127   return _PTR(SComponent)(new SALOMEDS_SComponent((SALOMEDS::SComponent_var)_corba_impl->GetFatherComponent()));
128 }
129
130 _PTR(SObject) SALOMEDS_SObject::GetFather()
131 {
132   if (_isLocal) {
133     SALOMEDS::Locker lock;
134     return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
135   }
136   return _PTR(SObject)(new SALOMEDS_SObject((SALOMEDS::SObject_var)_corba_impl->GetFather()));
137 }
138
139 bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute,
140                                      const std::string& aTypeOfAttribute)
141 {
142   bool ret = false;
143   if (_isLocal) {
144     SALOMEDS::Locker lock;
145     DF_Attribute* anAttr = NULL;
146     ret = _local_impl->FindAttribute(anAttr, aTypeOfAttribute);
147     if(ret) {
148       SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
149       anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(ga));
150     }
151   }
152   else {
153     SALOMEDS::GenericAttribute_var anAttr;
154     ret = _corba_impl->FindAttribute(anAttr.out(), aTypeOfAttribute.c_str());
155     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
156   }
157
158   return ret;
159 }
160
161 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
162 {
163   bool ret = false;
164   if (_isLocal) {
165     SALOMEDS::Locker lock;
166     SALOMEDSImpl_SObject aSO;
167     ret = _local_impl->ReferencedObject(aSO);
168     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
169   }
170   else {
171     SALOMEDS::SObject_var aSO;
172     ret = _corba_impl->ReferencedObject(aSO.out());
173     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
174   }
175
176   return ret; 
177 }
178
179
180 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
181 {
182   bool ret = false;
183   if (_isLocal) {
184     SALOMEDS::Locker lock;
185     SALOMEDSImpl_SObject aSO;
186     ret = _local_impl->FindSubObject(theTag, aSO);
187     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
188   }
189   else {
190     SALOMEDS::SObject_var aSO;
191     ret = _corba_impl->FindSubObject(theTag, aSO.out());
192     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
193   }
194
195   return ret;   
196 }
197
198 std::string SALOMEDS_SObject::Name()
199 {
200   std::string aName;
201   if (_isLocal) {
202     SALOMEDS::Locker lock;
203     aName = _local_impl->Name();
204   }
205   else aName = (CORBA::String_var)_corba_impl->Name();
206
207   return aName;
208 }
209
210 void  SALOMEDS_SObject::Name(const std::string& theName)
211 {
212   if (_isLocal) {
213     SALOMEDS::Locker lock;
214     _local_impl->Name(theName);
215   }
216   else _corba_impl->Name(theName.c_str());
217 }
218
219 std::vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
220 {
221   std::vector<_PTR(GenericAttribute)> aVector;
222   int aLength = 0;
223   SALOMEDSClient_GenericAttribute* anAttr;
224
225   if (_isLocal) {
226     SALOMEDS::Locker lock;
227     std::vector<DF_Attribute*> aSeq = _local_impl->GetAllAttributes();
228     aLength = (int)aSeq.size(); //!< TODO: conversion from size_t to int
229     for (int i = 0; i < aLength; i++) {
230       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]));
231       aVector.push_back(_PTR(GenericAttribute)(anAttr));
232     }
233   }
234   else {
235     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
236     aLength = aSeq->length();
237     for (int i = 0; i < aLength; i++) {
238       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
239       aVector.push_back(_PTR(GenericAttribute)(anAttr));
240     }
241   }
242
243   return aVector;
244 }
245
246 std::string SALOMEDS_SObject::GetName()
247 {
248   std::string aName;
249   if (_isLocal) {
250     SALOMEDS::Locker lock;
251     aName = _local_impl->GetName();
252   }
253   else aName = (CORBA::String_var) _corba_impl->GetName();
254
255   return aName;
256 }
257
258 std::string SALOMEDS_SObject::GetComment()
259 {
260   std::string aComment;
261   if (_isLocal) {
262     SALOMEDS::Locker lock;
263     aComment = _local_impl->GetComment();
264   }
265   else aComment = (CORBA::String_var) _corba_impl->GetComment();
266
267   return aComment;
268 }
269
270 std::string SALOMEDS_SObject::GetIOR()
271 {
272   std::string anIOR;
273   if (_isLocal) {
274     SALOMEDS::Locker lock;
275     anIOR = _local_impl->GetIOR();
276   }
277   else anIOR = (CORBA::String_var) _corba_impl->GetIOR();
278
279   return anIOR;
280 }
281
282 int SALOMEDS_SObject::Tag()
283 {
284   if (_isLocal) {
285     SALOMEDS::Locker lock;
286     return _local_impl->Tag();
287   }
288   return _corba_impl->Tag(); 
289 }
290
291 int SALOMEDS_SObject::GetLastChildTag()
292 {
293   if (_isLocal) {
294     SALOMEDS::Locker lock;
295     return _local_impl->GetLastChildTag();
296   }
297   return _corba_impl->GetLastChildTag(); 
298 }
299
300 int SALOMEDS_SObject::Depth()
301 {
302   if (_isLocal) {
303     SALOMEDS::Locker lock;
304     return _local_impl->Depth();
305   }
306   return _corba_impl->Depth();  
307 }
308
309 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
310 {
311   CORBA::Object_var obj;
312   if (_isLocal) {
313     SALOMEDS::Locker lock;
314     std::string anIOR = GetIOR();
315     if (!anIOR.empty())
316       obj = _orb->string_to_object(anIOR.c_str());
317     return obj._retn();
318   }
319   else {
320     obj = _corba_impl->GetObject();
321     return obj._retn();
322   }
323
324   return CORBA::Object::_nil();
325 }
326
327 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
328 {
329   if(_isLocal) {
330     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
331     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(*_local_impl, _orb);
332     _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
333     return aSO._retn();
334   }
335   else {
336     return SALOMEDS::SObject::_duplicate(_corba_impl);
337   }
338   return SALOMEDS::SObject::_nil();
339 }
340
341
342 void SALOMEDS_SObject::init_orb()
343 {
344   _orb = KERNEL::GetRefToORB();    
345 }
346
347 void SALOMEDS_SObject::SetAttrString(const std::string& name, const std::string& value)
348 {
349   if(_isLocal)
350     {
351       SALOMEDS::Locker lock;
352       _local_impl->SetAttrString(name,value);
353     }
354   else
355     {
356       _corba_impl->SetAttrString(name.c_str(),value.c_str());
357     }
358 }