Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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
25
26 #include <string>
27 #include <TCollection_AsciiString.hxx> 
28 #include <TColStd_HSequenceOfTransient.hxx>
29
30 #include "SALOMEDS_SObject.hxx"
31
32 #include "SALOMEDS.hxx"
33 #include "SALOMEDS_SComponent.hxx"
34 #include "SALOMEDS_GenericAttribute.hxx"
35 #include "SALOMEDS_Study.hxx"
36 #include "SALOMEDS_SObject_i.hxx"
37
38 #include "SALOMEDSImpl_SComponent.hxx"
39 #include "SALOMEDSImpl_GenericAttribute.hxx"
40 #include "SALOMEDSImpl_Study.hxx"
41
42 #include "Utils_ORB_INIT.hxx" 
43 #include "Utils_SINGLETON.hxx" 
44
45 #ifdef WIN32
46 #include <process.h>
47 #else
48 #include <sys/types.h>
49 #include <unistd.h>
50 #endif
51
52 #include "OpUtil.hxx"
53 #include "utilities.h"
54
55 using namespace std;  
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   long addr = theSObject->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
66   if(_isLocal) {
67     _local_impl = ((SALOMEDSImpl_SObject*)(addr));
68     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
69   }
70   else {
71     _local_impl = NULL;
72     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
73   }
74
75   init_orb();
76 }
77
78 SALOMEDS_SObject::SALOMEDS_SObject(const Handle(SALOMEDSImpl_SObject)& theSObject)
79 :_isLocal(true)
80 {
81   _corba_impl = SALOMEDS::SObject::_nil();
82   _local_impl = theSObject;
83
84   init_orb();
85 }
86
87 SALOMEDS_SObject::~SALOMEDS_SObject()
88 {
89   if (!_isLocal) {
90     _corba_impl->Destroy();
91   }
92 }
93
94 std::string SALOMEDS_SObject::GetID()
95 {
96   std::string aValue;
97   if (_isLocal) {
98     SALOMEDS::Locker lock;
99     aValue = _local_impl->GetID().ToCString();
100   }
101   else aValue = _corba_impl->GetID();  
102   return aValue;
103 }
104
105 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
106 {
107   if (_isLocal) {
108     SALOMEDS::Locker lock;
109     Handle(SALOMEDSImpl_SComponent) aSCO =
110       Handle(SALOMEDSImpl_SComponent)::DownCast(_local_impl->GetFatherComponent());
111     return _PTR(SComponent)(new SALOMEDS_SComponent(aSCO));
112   }
113   return _PTR(SComponent)(new SALOMEDS_SComponent(_corba_impl->GetFatherComponent()));
114 }
115
116 _PTR(SObject) SALOMEDS_SObject::GetFather()
117 {
118   if (_isLocal) {
119     SALOMEDS::Locker lock;
120     return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
121   }
122   return _PTR(SObject)(new SALOMEDS_SObject(_corba_impl->GetFather()));
123 }
124
125 bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute,
126                                      const std::string& aTypeOfAttribute)
127 {
128   bool ret = false;
129   if (_isLocal) {
130     SALOMEDS::Locker lock;
131     Handle(SALOMEDSImpl_GenericAttribute) anAttr;
132     ret = _local_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute.c_str());
133     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
134   }
135   else {
136     SALOMEDS::GenericAttribute_var anAttr;
137     ret = _corba_impl->FindAttribute(anAttr.out(), aTypeOfAttribute.c_str());
138     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
139   }
140
141   return ret;
142 }
143
144 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
145 {
146   bool ret = false;
147   if (_isLocal) {
148     SALOMEDS::Locker lock;
149     Handle(SALOMEDSImpl_SObject) aSO;
150     ret = _local_impl->ReferencedObject(aSO);
151     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
152   }
153   else {
154     SALOMEDS::SObject_var aSO;
155     ret = _corba_impl->ReferencedObject(aSO.out());
156     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
157   }
158
159   return ret; 
160 }
161
162
163 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
164 {
165   bool ret = false;
166   if (_isLocal) {
167     SALOMEDS::Locker lock;
168     Handle(SALOMEDSImpl_SObject) aSO;
169     ret = _local_impl->FindSubObject(theTag, aSO);
170     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
171   }
172   else {
173     SALOMEDS::SObject_var aSO;
174     ret = _corba_impl->FindSubObject(theTag, aSO.out());
175     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
176   }
177
178   return ret;   
179 }
180
181 _PTR(Study) SALOMEDS_SObject::GetStudy()
182 {
183   if (_isLocal) {
184     SALOMEDS::Locker lock;
185     return _PTR(Study)(new SALOMEDS_Study(_local_impl->GetStudy()));
186   }
187   return _PTR(Study)(new SALOMEDS_Study(_corba_impl->GetStudy()));
188 }
189
190 std::string SALOMEDS_SObject::Name()
191 {
192   std::string aName;
193   if (_isLocal) {
194     SALOMEDS::Locker lock;
195     aName = _local_impl->Name().ToCString();
196   }
197   else aName = _corba_impl->Name();
198
199   return aName;
200 }
201
202 void  SALOMEDS_SObject::Name(const std::string& theName)
203 {
204   if (_isLocal) {
205     SALOMEDS::Locker lock;
206     _local_impl->Name((char*)theName.c_str());
207   }
208   else _corba_impl->Name(theName.c_str());
209 }
210
211 vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
212 {
213   vector<_PTR(GenericAttribute)> aVector;
214   int aLength = 0;
215   SALOMEDSClient_GenericAttribute* anAttr;
216
217   if (_isLocal) {
218     SALOMEDS::Locker lock;
219     Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetAllAttributes();
220     aLength = aSeq->Length();
221     for (int i = 1; i <= aLength; i++) {
222       anAttr = SALOMEDS_GenericAttribute::CreateAttribute
223         (Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aSeq->Value(i)));
224       aVector.push_back(_PTR(GenericAttribute)(anAttr));
225     }
226   }
227   else {
228     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
229     aLength = aSeq->length();
230     for (int i = 0; i < aLength; i++) {
231       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
232       aVector.push_back(_PTR(GenericAttribute)(anAttr));
233     }
234   }
235
236   return aVector;
237 }
238
239 std::string SALOMEDS_SObject::GetName()
240 {
241   std::string aName;
242   if (_isLocal) {
243     SALOMEDS::Locker lock;
244     aName = _local_impl->GetName().ToCString();
245   }
246   else aName = _corba_impl->GetName();
247
248   return aName;
249 }
250
251 std::string SALOMEDS_SObject::GetComment()
252 {
253   std::string aComment;
254   if (_isLocal) {
255     SALOMEDS::Locker lock;
256     aComment = _local_impl->GetComment().ToCString();
257   }
258   else aComment = _corba_impl->GetComment();
259
260   return aComment;
261 }
262
263 std::string SALOMEDS_SObject::GetIOR()
264 {
265   std::string anIOR;
266   if (_isLocal) {
267     SALOMEDS::Locker lock;
268     anIOR = _local_impl->GetIOR().ToCString();
269   }
270   else anIOR = _corba_impl->GetIOR();
271
272   return anIOR;
273 }
274
275 int SALOMEDS_SObject::Tag()
276 {
277   if (_isLocal) {
278     SALOMEDS::Locker lock;
279     return _local_impl->Tag();
280   }
281   return _corba_impl->Tag(); 
282 }
283
284 int SALOMEDS_SObject::Depth()
285 {
286   if (_isLocal) {
287     SALOMEDS::Locker lock;
288     return _local_impl->Depth();
289   }
290   return _corba_impl->Depth();  
291 }
292
293 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
294 {
295   CORBA::Object_var obj;
296   if (_isLocal) {
297     SALOMEDS::Locker lock;
298     std::string anIOR = GetIOR();
299     if (!anIOR.empty())
300       obj = _orb->string_to_object(anIOR.c_str());
301     return obj._retn();
302   }
303   else {
304     obj = _corba_impl->GetObject();
305     return obj._retn();
306   }
307
308   return CORBA::Object::_nil();
309 }
310
311 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
312 {
313   if(_isLocal) {
314     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::SObject::_duplicate(_corba_impl);
315     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(_local_impl, _orb);
316     _corba_impl = SALOMEDS::SObject::_duplicate(aSO);
317     return aSO._retn();
318   }
319   else {
320     return SALOMEDS::SObject::_duplicate(_corba_impl);
321   }
322   return SALOMEDS::SObject::_nil();
323 }
324
325
326 void SALOMEDS_SObject::init_orb()
327 {
328   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
329   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
330   _orb = init(0 , 0 ) ;     
331 }