Salome HOME
Issue 0020194: EDF 977 ALL: Get rid of warnings PACKAGE_VERSION already defined
[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_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(_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(_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   return _PTR(Study)(new SALOMEDS_Study(_corba_impl->GetStudy()));
203 }
204
205 std::string SALOMEDS_SObject::Name()
206 {
207   std::string aName;
208   if (_isLocal) {
209     SALOMEDS::Locker lock;
210     aName = _local_impl->Name();
211   }
212   else aName = _corba_impl->Name();
213
214   return aName;
215 }
216
217 void  SALOMEDS_SObject::Name(const std::string& theName)
218 {
219   if (_isLocal) {
220     SALOMEDS::Locker lock;
221     _local_impl->Name(theName);
222   }
223   else _corba_impl->Name(theName.c_str());
224 }
225
226 vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
227 {
228   vector<_PTR(GenericAttribute)> aVector;
229   int aLength = 0;
230   SALOMEDSClient_GenericAttribute* anAttr;
231
232   if (_isLocal) {
233     SALOMEDS::Locker lock;
234     vector<DF_Attribute*> aSeq = _local_impl->GetAllAttributes();
235     aLength = aSeq.size();
236     for (int i = 0; i < aLength; i++) {
237       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]));
238       aVector.push_back(_PTR(GenericAttribute)(anAttr));
239     }
240   }
241   else {
242     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
243     aLength = aSeq->length();
244     for (int i = 0; i < aLength; i++) {
245       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
246       aVector.push_back(_PTR(GenericAttribute)(anAttr));
247     }
248   }
249
250   return aVector;
251 }
252
253 std::string SALOMEDS_SObject::GetName()
254 {
255   std::string aName;
256   if (_isLocal) {
257     SALOMEDS::Locker lock;
258     aName = _local_impl->GetName();
259   }
260   else aName = _corba_impl->GetName();
261
262   return aName;
263 }
264
265 std::string SALOMEDS_SObject::GetComment()
266 {
267   std::string aComment;
268   if (_isLocal) {
269     SALOMEDS::Locker lock;
270     aComment = _local_impl->GetComment();
271   }
272   else aComment = _corba_impl->GetComment();
273
274   return aComment;
275 }
276
277 std::string SALOMEDS_SObject::GetIOR()
278 {
279   std::string anIOR;
280   if (_isLocal) {
281     SALOMEDS::Locker lock;
282     anIOR = _local_impl->GetIOR();
283   }
284   else anIOR = _corba_impl->GetIOR();
285
286   return anIOR;
287 }
288
289 int SALOMEDS_SObject::Tag()
290 {
291   if (_isLocal) {
292     SALOMEDS::Locker lock;
293     return _local_impl->Tag();
294   }
295   return _corba_impl->Tag(); 
296 }
297
298 int SALOMEDS_SObject::Depth()
299 {
300   if (_isLocal) {
301     SALOMEDS::Locker lock;
302     return _local_impl->Depth();
303   }
304   return _corba_impl->Depth();  
305 }
306
307 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
308 {
309   CORBA::Object_var obj;
310   if (_isLocal) {
311     SALOMEDS::Locker lock;
312     std::string anIOR = GetIOR();
313     if (!anIOR.empty())
314       obj = _orb->string_to_object(anIOR.c_str());
315     return obj._retn();
316   }
317   else {
318     obj = _corba_impl->GetObject();
319     return obj._retn();
320   }
321
322   return CORBA::Object::_nil();
323 }
324
325 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
326 {
327   if(_isLocal) {
328     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
329     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(*_local_impl, _orb);
330     _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
331     return aSO._retn();
332   }
333   else {
334     return SALOMEDS::SObject::_duplicate(_corba_impl);
335   }
336   return SALOMEDS::SObject::_nil();
337 }
338
339
340 void SALOMEDS_SObject::init_orb()
341 {
342   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
343   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
344   _orb = init(0 , 0 ) ;     
345 }