Salome HOME
e73799b1d7ed6a26edd3c0c82616b8ffaa6bfae7
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject_i.cxx
1 // Copyright (C) 2007-2023  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_Study_i.hxx"
30 #include "SALOMEDS_SComponent_i.hxx"
31 #include "SALOMEDS_GenericAttribute_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   GenericObj_i(SALOMEDS_Study_i::GetThePOA())
63 {
64   _impl = 0;
65   if(!impl.IsNull()) {
66      if(impl.IsComponent()) {
67          SALOMEDSImpl_SComponent sco = impl;
68          _impl = sco.GetPersistentCopy();       
69      }
70      else {
71          _impl = impl.GetPersistentCopy();
72      }
73   }
74   _orb = CORBA::ORB::_duplicate(orb);
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 Get default POA for the servant object.
91
92   This function is implicitly called from "_this()" function.
93   Default POA can be set via the constructor.
94
95   \return reference to the default POA for the servant
96 */
97 //============================================================================
98 PortableServer::POA_ptr SALOMEDS_SObject_i::_default_POA()
99 {
100   myPOA = PortableServer::POA::_duplicate(SALOMEDS_Study_i::GetThePOA());
101   //MESSAGE("SALOMEDS_SObject_i::_default_POA: " << myPOA);
102   return PortableServer::POA::_duplicate(myPOA);
103 }
104
105 //================================================================================
106 /*!
107  * \brief Returns true if the %SObject does not belong to any %Study
108  */
109 //================================================================================
110
111 CORBA::Boolean SALOMEDS_SObject_i::IsNull()
112 {
113   SALOMEDS::Locker lock;
114   return !_impl || _impl->IsNull();
115 }
116
117 //============================================================================
118 /*! Function :GetID
119  *  Purpose  :
120  */
121 //============================================================================
122 char* SALOMEDS_SObject_i::GetID()
123 {
124   SALOMEDS::Locker lock;
125   return CORBA::string_dup(_impl->GetID().c_str());
126 }
127
128 //============================================================================
129 /*! Function : GetFatherComponent
130  *  Purpose  :
131  */
132 //============================================================================
133 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
134 {
135   SALOMEDS::Locker lock;
136   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (_impl->GetFatherComponent(), _orb);
137   return sco._retn();
138 }
139
140 //============================================================================
141 /*! Function : GetFather
142  *  Purpose  :
143  */
144 //============================================================================
145 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
146 {
147   SALOMEDS::Locker lock;
148   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (_impl->GetFather(), _orb);
149   return so._retn();
150 }
151
152 //============================================================================
153 /*! Function : FindAttribute
154  *  Purpose  : Find attribute of given type on this SObject
155  */
156 //============================================================================
157 CORBA::Boolean SALOMEDS_SObject_i::FindAttribute (SALOMEDS::GenericAttribute_out anAttribute,
158                                                   const char* aTypeOfAttribute)
159 {
160   SALOMEDS::Locker lock;
161   DF_Attribute* anAttr = NULL;
162   if(_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute)) {
163     anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
164     return true;
165   }
166
167   return false;
168 }
169
170 //============================================================================
171 /*! Function : GetAllAttributes
172  *  Purpose  : Returns list of all attributes for this sobject
173  */
174 //============================================================================
175
176 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
177 {
178   SALOMEDS::Locker lock;
179   std::vector<DF_Attribute*> aSeq = _impl->GetAllAttributes();
180   SALOMEDS::ListOfAttributes_var SeqOfAttr = new SALOMEDS::ListOfAttributes;
181   int length = (int)aSeq.size(); //!< TODO: conversion from size_t to int
182
183   SeqOfAttr->length(length);
184
185   if (length != 0) {
186     for(int i = 0; i < length; i++) {
187       SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]);
188       SALOMEDS::GenericAttribute_var anAttribute;
189       anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
190       if (!CORBA::is_nil(anAttribute)) {
191         SeqOfAttr[i] = anAttribute;
192       }
193     }
194   }
195   return SeqOfAttr._retn();
196 }
197
198
199 //============================================================================
200 /*! Function : ReferencedObject
201  *  Purpose  :
202  */
203 //============================================================================
204 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out obj)
205 {
206   SALOMEDS::Locker lock;
207   SALOMEDSImpl_SObject aRefObj;
208   if(!_impl->ReferencedObject(aRefObj)) return false;
209
210   obj = SALOMEDS_SObject_i::New (aRefObj, _orb);
211   return true;
212 }
213
214 //============================================================================
215 /*! Function : FindSubObject
216  *  Purpose  :
217  */
218 //============================================================================
219 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(CORBA::Long atag, SALOMEDS::SObject_out obj)
220 {
221   SALOMEDS::Locker lock;
222   SALOMEDSImpl_SObject aSubObj;
223   if(!_impl->FindSubObject(atag, aSubObj)) return false;
224
225   obj = SALOMEDS_SObject_i::New (aSubObj, _orb);
226   return true;
227
228 }
229
230 //============================================================================
231 /*! Function : Name
232  *  Purpose  : gets a name
233  */
234 //============================================================================
235 char* SALOMEDS_SObject_i::Name()
236 {
237   SALOMEDS::Locker lock;
238   return CORBA::string_dup(_impl->Name().c_str());
239 }
240
241 //============================================================================
242 /*! Function : Name
243  *  Purpose  : sets a name
244  */
245 //============================================================================
246 void  SALOMEDS_SObject_i::Name(const char* name)
247 {
248   SALOMEDS::Locker lock;
249   std::string aName((char*)name);
250   _impl->Name(aName);
251 }
252
253 //============================================================================
254 /*! Function : Tag
255  *  Purpose  :
256  */
257 //============================================================================
258 CORBA::Short SALOMEDS_SObject_i::Tag()
259 {
260   SALOMEDS::Locker lock;
261   return (CORBA::Short)_impl->Tag(); //!< TODO: conversion from int to CORBA::Short
262 }
263
264 //============================================================================
265 /*! Function : GetLastChildTag
266  *  Purpose  :
267  */
268 //============================================================================
269 CORBA::Short SALOMEDS_SObject_i::GetLastChildTag()
270 {
271   SALOMEDS::Locker lock;
272   return (CORBA::Short) _impl->GetLastChildTag();
273 }
274
275 //============================================================================
276 /*! Function : Depth
277  *  Purpose  :
278  */
279 //============================================================================
280 CORBA::Short SALOMEDS_SObject_i::Depth()
281 {
282   SALOMEDS::Locker lock;
283   return (CORBA::Short)_impl->Depth(); //!< TODO: conversion from int to CORBA::Short
284 }
285
286 //============================================================================
287 /*! Function : GetObject
288  *  Purpose  :
289  */
290 //============================================================================
291 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
292 {
293   SALOMEDS::Locker lock;
294   CORBA::Object_ptr obj = CORBA::Object::_nil();
295   try {
296     std::string IOR = _impl->GetIOR();
297     char* c_ior = CORBA::string_dup(IOR.c_str());
298     obj = _orb->string_to_object(c_ior);
299     CORBA::string_free(c_ior);
300   } catch(...) {}
301   return obj;
302 }
303
304 //============================================================================
305 /*! Function : GetName
306  *  Purpose  :
307  */
308 //============================================================================
309 char* SALOMEDS_SObject_i::GetName()
310 {
311   SALOMEDS::Locker lock;
312   CORBA::String_var aStr = CORBA::string_dup(_impl->GetName().c_str());
313   return aStr._retn();
314 }
315
316 //============================================================================
317 /*! Function : GetComment
318  *  Purpose  :
319  */
320 //============================================================================
321 char* SALOMEDS_SObject_i::GetComment()
322 {
323   SALOMEDS::Locker lock;
324   CORBA::String_var aStr = CORBA::string_dup(_impl->GetComment().c_str());
325   return aStr._retn();
326 }
327
328 //============================================================================
329 /*! Function : GetIOR
330  *  Purpose  :
331  */
332 //============================================================================
333 char* SALOMEDS_SObject_i::GetIOR()
334 {
335   SALOMEDS::Locker lock;
336   CORBA::String_var aStr = CORBA::string_dup(_impl->GetIOR().c_str());
337   return aStr._retn();
338 }
339
340 //============================================================================
341 /*! Function : SetAttrString
342  *  Purpose  :
343  */
344 //============================================================================
345 void SALOMEDS_SObject_i::SetAttrString(const char* name, const char* value)
346 {
347   SALOMEDS::Locker lock;
348   _impl->SetAttrString(name,value);
349 }
350
351 //===========================================================================
352 //   PRIVATE FUNCTIONS
353 //===========================================================================
354 CORBA::LongLong SALOMEDS_SObject_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
355 {
356 #ifdef WIN32
357   long pid = (long)_getpid();
358 #else
359   long pid = (long)getpid();
360 #endif
361   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
362   return reinterpret_cast<CORBA::LongLong>(_impl);
363 }