Salome HOME
Synchronize adm files
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, 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 "Utils_ORB_INIT.hxx" 
42 #include "Utils_SINGLETON.hxx" 
43
44 #include "Basics_Utils.hxx"
45
46 #include "utilities.h"
47
48 #ifdef WIN32
49 #include <windows.h>
50 #include <process.h>
51 #else
52 #include <sys/types.h>
53 #include <unistd.h>
54 #endif
55
56
57 SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
58 {
59 #ifdef WIN32
60   long pid =  (long)_getpid();
61 #else
62   long pid =  (long)getpid();
63 #endif  
64
65   CORBA::LongLong addr =  // mpv: fix for IPAL13534: for 64-bit platforms use 8-bytes long for pointer storage
66   theSObject->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
67
68   if(_isLocal) {
69     _local_impl = reinterpret_cast<SALOMEDSImpl_SObject*>(addr);
70     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
71   }
72   else {
73     _local_impl = NULL;
74     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
75   }
76
77   init_orb();
78 }
79
80 SALOMEDS_SObject::SALOMEDS_SObject(const SALOMEDSImpl_SObject& theSObject)
81 :_isLocal(true)
82 {
83   _corba_impl = SALOMEDS::SObject::_nil();
84
85   if(theSObject.IsComponent()) {
86     SALOMEDSImpl_SComponent sco = theSObject;
87     _local_impl = sco.GetPersistentCopy();
88   }
89   else {
90     _local_impl = theSObject.GetPersistentCopy();
91   }
92
93   init_orb();
94 }
95
96 SALOMEDS_SObject::~SALOMEDS_SObject()
97 {
98   if (!_isLocal) {
99     _corba_impl->UnRegister();
100   }
101   else {
102     if(_local_impl) delete _local_impl;
103   }
104 }
105
106 bool SALOMEDS_SObject::IsNull() const
107 {
108   return _isLocal ? ( !_local_impl || _local_impl->IsNull() ) : _corba_impl->IsNull();
109 }
110
111 std::string SALOMEDS_SObject::GetID()
112 {
113   std::string aValue;
114   if (_isLocal) {
115     SALOMEDS::Locker lock;
116     aValue = _local_impl->GetID();
117   }
118   else aValue = (CORBA::String_var)_corba_impl->GetID();  
119   return aValue;
120 }
121
122 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
123 {
124   if (_isLocal) {
125     SALOMEDS::Locker lock;
126     return _PTR(SComponent)(new SALOMEDS_SComponent(_local_impl->GetFatherComponent()));
127   }
128   return _PTR(SComponent)(new SALOMEDS_SComponent((SALOMEDS::SComponent_var)_corba_impl->GetFatherComponent()));
129 }
130
131 _PTR(SObject) SALOMEDS_SObject::GetFather()
132 {
133   if (_isLocal) {
134     SALOMEDS::Locker lock;
135     return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
136   }
137   return _PTR(SObject)(new SALOMEDS_SObject((SALOMEDS::SObject_var)_corba_impl->GetFather()));
138 }
139
140 bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute,
141                                      const std::string& aTypeOfAttribute)
142 {
143   bool ret = false;
144   if (_isLocal) {
145     SALOMEDS::Locker lock;
146     DF_Attribute* anAttr = NULL;
147     ret = _local_impl->FindAttribute(anAttr, aTypeOfAttribute);
148     if(ret) {
149       SALOMEDSImpl_GenericAttribute* ga = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
150       anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(ga));
151     }
152   }
153   else {
154     SALOMEDS::GenericAttribute_var anAttr;
155     ret = _corba_impl->FindAttribute(anAttr.out(), aTypeOfAttribute.c_str());
156     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
157   }
158
159   return ret;
160 }
161
162 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
163 {
164   bool ret = false;
165   if (_isLocal) {
166     SALOMEDS::Locker lock;
167     SALOMEDSImpl_SObject aSO;
168     ret = _local_impl->ReferencedObject(aSO);
169     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
170   }
171   else {
172     SALOMEDS::SObject_var aSO;
173     ret = _corba_impl->ReferencedObject(aSO.out());
174     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
175   }
176
177   return ret; 
178 }
179
180
181 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
182 {
183   bool ret = false;
184   if (_isLocal) {
185     SALOMEDS::Locker lock;
186     SALOMEDSImpl_SObject aSO;
187     ret = _local_impl->FindSubObject(theTag, aSO);
188     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
189   }
190   else {
191     SALOMEDS::SObject_var aSO;
192     ret = _corba_impl->FindSubObject(theTag, aSO.out());
193     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
194   }
195
196   return ret;   
197 }
198
199 _PTR(Study) SALOMEDS_SObject::GetStudy()
200 {
201   if (_isLocal) {
202     SALOMEDS::Locker lock;
203     return _PTR(Study)(new SALOMEDS_Study(_local_impl->GetStudy()));
204   }
205   SALOMEDS::Study_var study=_corba_impl->GetStudy();
206   return _PTR(Study)(new SALOMEDS_Study(study));
207 }
208
209 std::string SALOMEDS_SObject::Name()
210 {
211   std::string aName;
212   if (_isLocal) {
213     SALOMEDS::Locker lock;
214     aName = _local_impl->Name();
215   }
216   else aName = (CORBA::String_var)_corba_impl->Name();
217
218   return aName;
219 }
220
221 void  SALOMEDS_SObject::Name(const std::string& theName)
222 {
223   if (_isLocal) {
224     SALOMEDS::Locker lock;
225     _local_impl->Name(theName);
226   }
227   else _corba_impl->Name(theName.c_str());
228 }
229
230 std::vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
231 {
232   std::vector<_PTR(GenericAttribute)> aVector;
233   int aLength = 0;
234   SALOMEDSClient_GenericAttribute* anAttr;
235
236   if (_isLocal) {
237     SALOMEDS::Locker lock;
238     std::vector<DF_Attribute*> aSeq = _local_impl->GetAllAttributes();
239     aLength = aSeq.size();
240     for (int i = 0; i < aLength; i++) {
241       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]));
242       aVector.push_back(_PTR(GenericAttribute)(anAttr));
243     }
244   }
245   else {
246     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
247     aLength = aSeq->length();
248     for (int i = 0; i < aLength; i++) {
249       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
250       aVector.push_back(_PTR(GenericAttribute)(anAttr));
251     }
252   }
253
254   return aVector;
255 }
256
257 std::string SALOMEDS_SObject::GetName()
258 {
259   std::string aName;
260   if (_isLocal) {
261     SALOMEDS::Locker lock;
262     aName = _local_impl->GetName();
263   }
264   else aName = (CORBA::String_var) _corba_impl->GetName();
265
266   return aName;
267 }
268
269 std::string SALOMEDS_SObject::GetComment()
270 {
271   std::string aComment;
272   if (_isLocal) {
273     SALOMEDS::Locker lock;
274     aComment = _local_impl->GetComment();
275   }
276   else aComment = (CORBA::String_var) _corba_impl->GetComment();
277
278   return aComment;
279 }
280
281 std::string SALOMEDS_SObject::GetIOR()
282 {
283   std::string anIOR;
284   if (_isLocal) {
285     SALOMEDS::Locker lock;
286     anIOR = _local_impl->GetIOR();
287   }
288   else anIOR = (CORBA::String_var) _corba_impl->GetIOR();
289
290   return anIOR;
291 }
292
293 int SALOMEDS_SObject::Tag()
294 {
295   if (_isLocal) {
296     SALOMEDS::Locker lock;
297     return _local_impl->Tag();
298   }
299   return _corba_impl->Tag(); 
300 }
301
302 int SALOMEDS_SObject::Depth()
303 {
304   if (_isLocal) {
305     SALOMEDS::Locker lock;
306     return _local_impl->Depth();
307   }
308   return _corba_impl->Depth();  
309 }
310
311 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
312 {
313   CORBA::Object_var obj;
314   if (_isLocal) {
315     SALOMEDS::Locker lock;
316     std::string anIOR = GetIOR();
317     if (!anIOR.empty())
318       obj = _orb->string_to_object(anIOR.c_str());
319     return obj._retn();
320   }
321   else {
322     obj = _corba_impl->GetObject();
323     return obj._retn();
324   }
325
326   return CORBA::Object::_nil();
327 }
328
329 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
330 {
331   if(_isLocal) {
332     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
333     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(*_local_impl, _orb);
334     _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
335     return aSO._retn();
336   }
337   else {
338     return SALOMEDS::SObject::_duplicate(_corba_impl);
339   }
340   return SALOMEDS::SObject::_nil();
341 }
342
343
344 void SALOMEDS_SObject::init_orb()
345 {
346   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
347   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
348   _orb = init(0 , 0 ) ;     
349 }
350
351 void SALOMEDS_SObject::SetAttrString(const std::string& name, const std::string& value)
352 {
353   if(_isLocal)
354     {
355       SALOMEDS::Locker lock;
356       _local_impl->SetAttrString(name,value);
357     }
358   else
359     {
360       _corba_impl->SetAttrString(name.c_str(),value.c_str());
361     }
362 }