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