Salome HOME
To avoid compilation pb on RedHat 8.0.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDS_SObject.hxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24 #include <string>
25 #include <TCollection_AsciiString.hxx> 
26 #include <TColStd_HSequenceOfTransient.hxx>
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 #ifdef WIN32
44 #include <windows.h>
45 #include <process.h>
46 #else
47 #include <sys/types.h>
48 #include <unistd.h>
49 #endif
50
51 #include "OpUtil.hxx"
52 #include "utilities.h"
53
54 using namespace std;  
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(GetHostname().c_str(), pid, _isLocal);
66
67   if(_isLocal) {
68     _local_impl = ((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 Handle(SALOMEDSImpl_SObject)& theSObject)
80 :_isLocal(true)
81 {
82   _corba_impl = SALOMEDS::SObject::_nil();
83   _local_impl = theSObject;
84
85   init_orb();
86 }
87
88 SALOMEDS_SObject::~SALOMEDS_SObject()
89 {
90   if (!_isLocal) {
91     _corba_impl->Destroy();
92   }
93 }
94
95 std::string SALOMEDS_SObject::GetID()
96 {
97   std::string aValue;
98   if (_isLocal) {
99     SALOMEDS::Locker lock;
100     aValue = _local_impl->GetID().ToCString();
101   }
102   else aValue = _corba_impl->GetID();  
103   return aValue;
104 }
105
106 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
107 {
108   if (_isLocal) {
109     SALOMEDS::Locker lock;
110     Handle(SALOMEDSImpl_SComponent) aSCO =
111       Handle(SALOMEDSImpl_SComponent)::DownCast(_local_impl->GetFatherComponent());
112     return _PTR(SComponent)(new SALOMEDS_SComponent(aSCO));
113   }
114   return _PTR(SComponent)(new SALOMEDS_SComponent(_corba_impl->GetFatherComponent()));
115 }
116
117 _PTR(SObject) SALOMEDS_SObject::GetFather()
118 {
119   if (_isLocal) {
120     SALOMEDS::Locker lock;
121     return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
122   }
123   return _PTR(SObject)(new SALOMEDS_SObject(_corba_impl->GetFather()));
124 }
125
126 bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute,
127                                      const std::string& aTypeOfAttribute)
128 {
129   bool ret = false;
130   if (_isLocal) {
131     SALOMEDS::Locker lock;
132     Handle(SALOMEDSImpl_GenericAttribute) anAttr;
133     ret = _local_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute.c_str());
134     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
135   }
136   else {
137     SALOMEDS::GenericAttribute_var anAttr;
138     ret = _corba_impl->FindAttribute(anAttr.out(), aTypeOfAttribute.c_str());
139     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
140   }
141
142   return ret;
143 }
144
145 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
146 {
147   bool ret = false;
148   if (_isLocal) {
149     SALOMEDS::Locker lock;
150     Handle(SALOMEDSImpl_SObject) aSO;
151     ret = _local_impl->ReferencedObject(aSO);
152     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
153   }
154   else {
155     SALOMEDS::SObject_var aSO;
156     ret = _corba_impl->ReferencedObject(aSO.out());
157     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
158   }
159
160   return ret; 
161 }
162
163
164 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
165 {
166   bool ret = false;
167   if (_isLocal) {
168     SALOMEDS::Locker lock;
169     Handle(SALOMEDSImpl_SObject) aSO;
170     ret = _local_impl->FindSubObject(theTag, aSO);
171     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
172   }
173   else {
174     SALOMEDS::SObject_var aSO;
175     ret = _corba_impl->FindSubObject(theTag, aSO.out());
176     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
177   }
178
179   return ret;   
180 }
181
182 _PTR(Study) SALOMEDS_SObject::GetStudy()
183 {
184   if (_isLocal) {
185     SALOMEDS::Locker lock;
186     return _PTR(Study)(new SALOMEDS_Study(_local_impl->GetStudy()));
187   }
188   return _PTR(Study)(new SALOMEDS_Study(_corba_impl->GetStudy()));
189 }
190
191 std::string SALOMEDS_SObject::Name()
192 {
193   std::string aName;
194   if (_isLocal) {
195     SALOMEDS::Locker lock;
196     aName = _local_impl->Name().ToCString();
197   }
198   else aName = _corba_impl->Name();
199
200   return aName;
201 }
202
203 void  SALOMEDS_SObject::Name(const std::string& theName)
204 {
205   if (_isLocal) {
206     SALOMEDS::Locker lock;
207     _local_impl->Name((char*)theName.c_str());
208   }
209   else _corba_impl->Name(theName.c_str());
210 }
211
212 vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
213 {
214   vector<_PTR(GenericAttribute)> aVector;
215   int aLength = 0;
216   SALOMEDSClient_GenericAttribute* anAttr;
217
218   if (_isLocal) {
219     SALOMEDS::Locker lock;
220     Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetAllAttributes();
221     aLength = aSeq->Length();
222     for (int i = 1; i <= aLength; i++) {
223       anAttr = SALOMEDS_GenericAttribute::CreateAttribute
224         (Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aSeq->Value(i)));
225       aVector.push_back(_PTR(GenericAttribute)(anAttr));
226     }
227   }
228   else {
229     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
230     aLength = aSeq->length();
231     for (int i = 0; i < aLength; i++) {
232       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
233       aVector.push_back(_PTR(GenericAttribute)(anAttr));
234     }
235   }
236
237   return aVector;
238 }
239
240 std::string SALOMEDS_SObject::GetName()
241 {
242   std::string aName;
243   if (_isLocal) {
244     SALOMEDS::Locker lock;
245     aName = _local_impl->GetName().ToCString();
246   }
247   else aName = _corba_impl->GetName();
248
249   return aName;
250 }
251
252 std::string SALOMEDS_SObject::GetComment()
253 {
254   std::string aComment;
255   if (_isLocal) {
256     SALOMEDS::Locker lock;
257     aComment = _local_impl->GetComment().ToCString();
258   }
259   else aComment = _corba_impl->GetComment();
260
261   return aComment;
262 }
263
264 std::string SALOMEDS_SObject::GetIOR()
265 {
266   std::string anIOR;
267   if (_isLocal) {
268     SALOMEDS::Locker lock;
269     anIOR = _local_impl->GetIOR().ToCString();
270   }
271   else anIOR = _corba_impl->GetIOR();
272
273   return anIOR;
274 }
275
276 int SALOMEDS_SObject::Tag()
277 {
278   if (_isLocal) {
279     SALOMEDS::Locker lock;
280     return _local_impl->Tag();
281   }
282   return _corba_impl->Tag(); 
283 }
284
285 int SALOMEDS_SObject::Depth()
286 {
287   if (_isLocal) {
288     SALOMEDS::Locker lock;
289     return _local_impl->Depth();
290   }
291   return _corba_impl->Depth();  
292 }
293
294 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
295 {
296   CORBA::Object_var obj;
297   if (_isLocal) {
298     SALOMEDS::Locker lock;
299     std::string anIOR = GetIOR();
300     if (!anIOR.empty())
301       obj = _orb->string_to_object(anIOR.c_str());
302     return obj._retn();
303   }
304   else {
305     obj = _corba_impl->GetObject();
306     return obj._retn();
307   }
308
309   return CORBA::Object::_nil();
310 }
311
312 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
313 {
314   if(_isLocal) {
315     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
316     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(_local_impl, _orb);
317     _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
318     return aSO._retn();
319   }
320   else {
321     return SALOMEDS::SObject::_duplicate(_corba_impl);
322   }
323   return SALOMEDS::SObject::_nil();
324 }
325
326
327 void SALOMEDS_SObject::init_orb()
328 {
329   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
330   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
331   _orb = init(0 , 0 ) ;     
332 }