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