Salome HOME
Added @BOOST_CPPFLAGS@
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject_i.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_i.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24
25
26 #include "utilities.h"
27 #include "SALOMEDS_SObject_i.hxx"
28 #include "SALOMEDS_SComponent_i.hxx"
29 #include "SALOMEDS_GenericAttribute_i.hxx"
30 #include "SALOMEDS_StudyManager_i.hxx"
31 #include "SALOMEDS.hxx"
32 #include "SALOMEDSImpl_GenericAttribute.hxx"
33 #include "SALOMEDSImpl_SComponent.hxx"
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_AttributeIOR.hxx"
36
37 #include <map>
38
39 #ifdef WIN32
40 #include <process.h>
41 #else
42 #include <sys/types.h>
43 #include <unistd.h>
44 #endif
45
46 #include "OpUtil.hxx"
47
48 using namespace std;
49
50 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::New(const SALOMEDSImpl_SObject& theImpl, CORBA::ORB_ptr theORB)
51 {
52   SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i(theImpl, theORB);
53   SALOMEDS::SObject_var so  = SALOMEDS::SObject::_narrow(so_servant->_this());
54
55   return so._retn();
56 }
57
58
59 //============================================================================
60 /*! Function : constructor
61  *  Purpose  :
62  */
63 //============================================================================
64 SALOMEDS_SObject_i::SALOMEDS_SObject_i(const SALOMEDSImpl_SObject& impl, CORBA::ORB_ptr orb)
65 {
66   if(!impl.IsNull()) {
67      if(impl.IsComponent()) {
68          SALOMEDSImpl_SComponent sco = impl;
69          _impl = sco.GetPersistentCopy();       
70      }
71      else {
72          _impl = impl.GetPersistentCopy();
73      }
74   }
75   _orb = CORBA::ORB::_duplicate(orb);
76    //SALOME::GenericObj_i::myPOA = SALOMEDS_StudyManager_i::GetPOA(GetStudy());
77 }
78
79
80 //============================================================================
81 /*! Function : destructor
82  *  Purpose  :
83  */
84 //============================================================================
85 SALOMEDS_SObject_i::~SALOMEDS_SObject_i()
86 {
87    if(_impl) delete _impl;    
88 }
89
90
91 //============================================================================
92 /*! Function :GetID
93  *  Purpose  :
94  */
95 //============================================================================
96 char* SALOMEDS_SObject_i::GetID()
97 {
98   SALOMEDS::Locker lock;
99   return CORBA::string_dup(_impl->GetID().c_str());
100 }
101
102 //============================================================================
103 /*! Function : GetFatherComponent
104  *  Purpose  :
105  */
106 //============================================================================
107 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
108 {
109   SALOMEDS::Locker lock;
110   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (_impl->GetFatherComponent(), _orb);
111   return sco._retn();
112 }
113
114 //============================================================================
115 /*! Function : GetFather
116  *  Purpose  :
117  */
118 //============================================================================
119 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
120 {
121   SALOMEDS::Locker lock;
122   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (_impl->GetFather(), _orb);
123   return so._retn();
124 }
125
126 //============================================================================
127 /*! Function :
128  *  Purpose  :
129  */
130 //============================================================================
131 SALOMEDS::Study_ptr SALOMEDS_SObject_i::GetStudy()
132 {
133   SALOMEDS::Locker lock;
134   SALOMEDSImpl_Study* aStudy = _impl->GetStudy();
135   if(!aStudy) {
136     MESSAGE("Problem GetStudy");
137     return SALOMEDS::Study::_nil();
138   }
139
140   string IOR = aStudy->GetTransientReference();
141   CORBA::Object_var obj = _orb->string_to_object(IOR.c_str());
142   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(obj) ;
143   ASSERT(!CORBA::is_nil(Study));
144   return SALOMEDS::Study::_duplicate(Study);
145 }
146
147 //============================================================================
148 /*! Function : FindAttribute
149  *  Purpose  : Find attribute of given type on this SObject
150  */
151 //============================================================================
152 CORBA::Boolean SALOMEDS_SObject_i::FindAttribute (SALOMEDS::GenericAttribute_out anAttribute,
153                                                   const char* aTypeOfAttribute)
154 {
155   SALOMEDS::Locker lock;
156   DF_Attribute* anAttr = NULL;
157   if(_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute)) {
158     anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
159     return true;
160   }
161
162   return false;
163 }
164
165 //============================================================================
166 /*! Function : GetAllAttributes
167  *  Purpose  : Returns list of all attributes for this sobject
168  */
169 //============================================================================
170
171 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
172 {
173   SALOMEDS::Locker lock;
174   vector<DF_Attribute*> aSeq = _impl->GetAllAttributes();
175   SALOMEDS::ListOfAttributes_var SeqOfAttr = new SALOMEDS::ListOfAttributes;
176   int length = aSeq.size();
177
178   SeqOfAttr->length(length);
179
180   if (length != 0) {
181     for(int i = 0; i < length; i++) {
182       SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]);
183       SALOMEDS::GenericAttribute_var anAttribute;
184       anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
185       if (!CORBA::is_nil(anAttribute)) {
186         SeqOfAttr[i] = anAttribute;
187       }
188     }
189   }
190   return SeqOfAttr._retn();
191 }
192
193
194 //============================================================================
195 /*! Function : ReferencedObject
196  *  Purpose  :
197  */
198 //============================================================================
199 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out obj)
200 {
201   SALOMEDS::Locker lock;
202   SALOMEDSImpl_SObject aRefObj;
203   if(!_impl->ReferencedObject(aRefObj)) return false;
204
205   obj = SALOMEDS_SObject_i::New (aRefObj, _orb);
206   return true;
207 }
208
209 //============================================================================
210 /*! Function : FindSubObject
211  *  Purpose  :
212  */
213 //============================================================================
214 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(CORBA::Long atag, SALOMEDS::SObject_out obj)
215 {
216   SALOMEDS::Locker lock;
217   SALOMEDSImpl_SObject aSubObj;
218   if(!_impl->FindSubObject(atag, aSubObj)) return false;
219
220   obj = SALOMEDS_SObject_i::New (aSubObj, _orb);
221   return true;
222
223 }
224
225 //============================================================================
226 /*! Function : Name
227  *  Purpose  : gets a name
228  */
229 //============================================================================
230 char* SALOMEDS_SObject_i::Name()
231 {
232   SALOMEDS::Locker lock;
233   return CORBA::string_dup(_impl->Name().c_str());
234 }
235
236 //============================================================================
237 /*! Function : Name
238  *  Purpose  : sets a name
239  */
240 //============================================================================
241 void  SALOMEDS_SObject_i::Name(const char* name)
242 {
243   SALOMEDS::Locker lock;
244   string aName((char*)name);
245   _impl->Name(aName);
246 }
247
248 //============================================================================
249 /*! Function : Tag
250  *  Purpose  :
251  */
252 //============================================================================
253 CORBA::Short SALOMEDS_SObject_i::Tag()
254 {
255   SALOMEDS::Locker lock;
256   return _impl->Tag();
257 }
258
259 //============================================================================
260 /*! Function : Depth
261  *  Purpose  :
262  */
263 //============================================================================
264 CORBA::Short SALOMEDS_SObject_i::Depth()
265 {
266   SALOMEDS::Locker lock;
267   return _impl->Depth();
268 }
269
270 //============================================================================
271 /*! Function : GetObject
272  *  Purpose  :
273  */
274 //============================================================================
275 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
276 {
277   SALOMEDS::Locker lock;
278   CORBA::Object_ptr obj = CORBA::Object::_nil();
279   try {
280     string IOR = _impl->GetIOR();
281     char* c_ior = CORBA::string_dup(IOR.c_str());
282     obj = _orb->string_to_object(c_ior);
283     CORBA::string_free(c_ior);
284   } catch(...) {}
285   return obj;
286 }
287
288 //============================================================================
289 /*! Function : GetName
290  *  Purpose  :
291  */
292 //============================================================================
293 char* SALOMEDS_SObject_i::GetName()
294 {
295   SALOMEDS::Locker lock;
296   CORBA::String_var aStr = CORBA::string_dup(_impl->GetName().c_str());
297   return aStr._retn();
298 }
299
300 //============================================================================
301 /*! Function : GetComment
302  *  Purpose  :
303  */
304 //============================================================================
305 char* SALOMEDS_SObject_i::GetComment()
306 {
307   SALOMEDS::Locker lock;
308   CORBA::String_var aStr = CORBA::string_dup(_impl->GetComment().c_str());
309   return aStr._retn();
310 }
311
312 //============================================================================
313 /*! Function : GetIOR
314  *  Purpose  :
315  */
316 //============================================================================
317 char* SALOMEDS_SObject_i::GetIOR()
318 {
319   SALOMEDS::Locker lock;
320   CORBA::String_var aStr = CORBA::string_dup(_impl->GetIOR().c_str());
321   return aStr._retn();
322 }
323
324 //===========================================================================
325 //   PRIVATE FUNCTIONS
326 //===========================================================================
327 CORBA::LongLong SALOMEDS_SObject_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
328 {
329 #ifdef WIN32
330   long pid = (long)_getpid();
331 #else
332   long pid = (long)getpid();
333 #endif
334   isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
335   return ((CORBA::LongLong)(void*)_impl);
336 }