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