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