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