Salome HOME
Synchronize adm files
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject_i.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDS_SObject_i.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28 #include "SALOMEDS_SObject_i.hxx"
29 #include "SALOMEDS_SComponent_i.hxx"
30 #include "SALOMEDS_GenericAttribute_i.hxx"
31 #include "SALOMEDS_StudyManager_i.hxx"
32 #include "SALOMEDS.hxx"
33 #include "SALOMEDSImpl_GenericAttribute.hxx"
34 #include "SALOMEDSImpl_SComponent.hxx"
35 #include "SALOMEDSImpl_Study.hxx"
36 #include "SALOMEDSImpl_AttributeIOR.hxx"
37 #include "Basics_Utils.hxx"
38
39 #include <map>
40
41 #ifdef WIN32
42 #include <process.h>
43 #else
44 #include <sys/types.h>
45 #include <unistd.h>
46 #endif
47
48 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::New(const SALOMEDSImpl_SObject& theImpl, CORBA::ORB_ptr theORB)
49 {
50   SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i(theImpl, theORB);
51
52   return so_servant->_this();
53 }
54
55
56 //============================================================================
57 /*! Function : constructor
58  *  Purpose  :
59  */
60 //============================================================================
61 SALOMEDS_SObject_i::SALOMEDS_SObject_i(const SALOMEDSImpl_SObject& impl, CORBA::ORB_ptr orb)
62 {
63   _impl = 0;
64   if(!impl.IsNull()) {
65      if(impl.IsComponent()) {
66          SALOMEDSImpl_SComponent sco = impl;
67          _impl = sco.GetPersistentCopy();       
68      }
69      else {
70          _impl = impl.GetPersistentCopy();
71      }
72   }
73   _orb = CORBA::ORB::_duplicate(orb);
74    //SALOME::GenericObj_i::myPOA = SALOMEDS_StudyManager_i::GetPOA(GetStudy());
75 }
76
77
78 //============================================================================
79 /*! Function : destructor
80  *  Purpose  :
81  */
82 //============================================================================
83 SALOMEDS_SObject_i::~SALOMEDS_SObject_i()
84 {
85    if(_impl) delete _impl;    
86 }
87
88 //================================================================================
89 /*!
90  * \brief Returns true if the %SObject does not belong to any %Study
91  */
92 //================================================================================
93
94 CORBA::Boolean SALOMEDS_SObject_i::IsNull()
95 {
96   SALOMEDS::Locker lock;
97   return !_impl || _impl->IsNull();
98 }
99
100 //============================================================================
101 /*! Function :GetID
102  *  Purpose  :
103  */
104 //============================================================================
105 char* SALOMEDS_SObject_i::GetID()
106 {
107   SALOMEDS::Locker lock;
108   return CORBA::string_dup(_impl->GetID().c_str());
109 }
110
111 //============================================================================
112 /*! Function : GetFatherComponent
113  *  Purpose  :
114  */
115 //============================================================================
116 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
117 {
118   SALOMEDS::Locker lock;
119   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (_impl->GetFatherComponent(), _orb);
120   return sco._retn();
121 }
122
123 //============================================================================
124 /*! Function : GetFather
125  *  Purpose  :
126  */
127 //============================================================================
128 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
129 {
130   SALOMEDS::Locker lock;
131   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (_impl->GetFather(), _orb);
132   return so._retn();
133 }
134
135 //============================================================================
136 /*! Function :
137  *  Purpose  :
138  */
139 //============================================================================
140 SALOMEDS::Study_ptr SALOMEDS_SObject_i::GetStudy()
141 {
142   SALOMEDS::Locker lock;
143   SALOMEDSImpl_Study* aStudy = _impl->GetStudy();
144   if(!aStudy) {
145     MESSAGE("Problem GetStudy");
146     return SALOMEDS::Study::_nil();
147   }
148
149   std::string IOR = aStudy->GetTransientReference();
150   CORBA::Object_var obj = _orb->string_to_object(IOR.c_str());
151   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(obj) ;
152   ASSERT(!CORBA::is_nil(Study));
153   return SALOMEDS::Study::_duplicate(Study);
154 }
155
156 //============================================================================
157 /*! Function : FindAttribute
158  *  Purpose  : Find attribute of given type on this SObject
159  */
160 //============================================================================
161 CORBA::Boolean SALOMEDS_SObject_i::FindAttribute (SALOMEDS::GenericAttribute_out anAttribute,
162                                                   const char* aTypeOfAttribute)
163 {
164   SALOMEDS::Locker lock;
165   DF_Attribute* anAttr = NULL;
166   if(_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute)) {
167     anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
168     return true;
169   }
170
171   return false;
172 }
173
174 //============================================================================
175 /*! Function : GetAllAttributes
176  *  Purpose  : Returns list of all attributes for this sobject
177  */
178 //============================================================================
179
180 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
181 {
182   SALOMEDS::Locker lock;
183   std::vector<DF_Attribute*> aSeq = _impl->GetAllAttributes();
184   SALOMEDS::ListOfAttributes_var SeqOfAttr = new SALOMEDS::ListOfAttributes;
185   int length = aSeq.size();
186
187   SeqOfAttr->length(length);
188
189   if (length != 0) {
190     for(int i = 0; i < length; i++) {
191       SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]);
192       SALOMEDS::GenericAttribute_var anAttribute;
193       anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
194       if (!CORBA::is_nil(anAttribute)) {
195         SeqOfAttr[i] = anAttribute;
196       }
197     }
198   }
199   return SeqOfAttr._retn();
200 }
201
202
203 //============================================================================
204 /*! Function : ReferencedObject
205  *  Purpose  :
206  */
207 //============================================================================
208 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out obj)
209 {
210   SALOMEDS::Locker lock;
211   SALOMEDSImpl_SObject aRefObj;
212   if(!_impl->ReferencedObject(aRefObj)) return false;
213
214   obj = SALOMEDS_SObject_i::New (aRefObj, _orb);
215   return true;
216 }
217
218 //============================================================================
219 /*! Function : FindSubObject
220  *  Purpose  :
221  */
222 //============================================================================
223 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(CORBA::Long atag, SALOMEDS::SObject_out obj)
224 {
225   SALOMEDS::Locker lock;
226   SALOMEDSImpl_SObject aSubObj;
227   if(!_impl->FindSubObject(atag, aSubObj)) return false;
228
229   obj = SALOMEDS_SObject_i::New (aSubObj, _orb);
230   return true;
231
232 }
233
234 //============================================================================
235 /*! Function : Name
236  *  Purpose  : gets a name
237  */
238 //============================================================================
239 char* SALOMEDS_SObject_i::Name()
240 {
241   SALOMEDS::Locker lock;
242   return CORBA::string_dup(_impl->Name().c_str());
243 }
244
245 //============================================================================
246 /*! Function : Name
247  *  Purpose  : sets a name
248  */
249 //============================================================================
250 void  SALOMEDS_SObject_i::Name(const char* name)
251 {
252   SALOMEDS::Locker lock;
253   std::string aName((char*)name);
254   _impl->Name(aName);
255 }
256
257 //============================================================================
258 /*! Function : Tag
259  *  Purpose  :
260  */
261 //============================================================================
262 CORBA::Short SALOMEDS_SObject_i::Tag()
263 {
264   SALOMEDS::Locker lock;
265   return _impl->Tag();
266 }
267
268 //============================================================================
269 /*! Function : Depth
270  *  Purpose  :
271  */
272 //============================================================================
273 CORBA::Short SALOMEDS_SObject_i::Depth()
274 {
275   SALOMEDS::Locker lock;
276   return _impl->Depth();
277 }
278
279 //============================================================================
280 /*! Function : GetObject
281  *  Purpose  :
282  */
283 //============================================================================
284 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
285 {
286   SALOMEDS::Locker lock;
287   CORBA::Object_ptr obj = CORBA::Object::_nil();
288   try {
289     std::string IOR = _impl->GetIOR();
290     char* c_ior = CORBA::string_dup(IOR.c_str());
291     obj = _orb->string_to_object(c_ior);
292     CORBA::string_free(c_ior);
293   } catch(...) {}
294   return obj;
295 }
296
297 //============================================================================
298 /*! Function : GetName
299  *  Purpose  :
300  */
301 //============================================================================
302 char* SALOMEDS_SObject_i::GetName()
303 {
304   SALOMEDS::Locker lock;
305   CORBA::String_var aStr = CORBA::string_dup(_impl->GetName().c_str());
306   return aStr._retn();
307 }
308
309 //============================================================================
310 /*! Function : GetComment
311  *  Purpose  :
312  */
313 //============================================================================
314 char* SALOMEDS_SObject_i::GetComment()
315 {
316   SALOMEDS::Locker lock;
317   CORBA::String_var aStr = CORBA::string_dup(_impl->GetComment().c_str());
318   return aStr._retn();
319 }
320
321 //============================================================================
322 /*! Function : GetIOR
323  *  Purpose  :
324  */
325 //============================================================================
326 char* SALOMEDS_SObject_i::GetIOR()
327 {
328   SALOMEDS::Locker lock;
329   CORBA::String_var aStr = CORBA::string_dup(_impl->GetIOR().c_str());
330   return aStr._retn();
331 }
332
333 //============================================================================
334 /*! Function : SetAttrString
335  *  Purpose  :
336  */
337 //============================================================================
338 void SALOMEDS_SObject_i::SetAttrString(const char* name, const char* value)
339 {
340   SALOMEDS::Locker lock;
341   _impl->SetAttrString(name,value);
342 }
343
344 //===========================================================================
345 //   PRIVATE FUNCTIONS
346 //===========================================================================
347 CORBA::LongLong SALOMEDS_SObject_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
348 {
349 #ifdef WIN32
350   long pid = (long)_getpid();
351 #else
352   long pid = (long)getpid();
353 #endif
354   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
355   return reinterpret_cast<CORBA::LongLong>(_impl);
356 }