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