Salome HOME
eaf664e03cbd987f459c06f659255a7838005b5f
[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/
19 //
20 //  File   : SALOMEDS_SObject.hxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24
25
26 #include <string>
27 #include <TCollection_AsciiString.hxx> 
28 #include <TColStd_HSequenceOfTransient.hxx>
29
30 #include "SALOMEDS_SObject.hxx"
31 #include "SALOMEDS_SComponent.hxx"
32 #include "SALOMEDSImpl_SComponent.hxx"
33 #include "SALOMEDS_GenericAttribute.hxx"
34 #include "SALOMEDSImpl_GenericAttribute.hxx"
35 #include "SALOMEDS_Study.hxx"
36 #include "SALOMEDSImpl_Study.hxx"
37
38 #include "Utils_ORB_INIT.hxx" 
39 #include "Utils_SINGLETON.hxx" 
40
41 #ifdef WIN32
42 #include <process.h>
43 #else
44 #include <sys/types.h>
45 #include <unistd.h>
46 #endif
47
48 #include "OpUtil.hxx"
49 #include "utilities.h"
50
51 using namespace std;  
52
53 SALOMEDS_SObject::SALOMEDS_SObject(SALOMEDS::SObject_ptr theSObject)
54 {
55 #ifdef WIN32
56   long pid =  (long)_getpid();
57 #else
58   long pid =  (long)getpid();
59 #endif  
60
61   long addr = theSObject->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
62   if(_isLocal) {
63     _local_impl = ((SALOMEDSImpl_SObject*)(addr));
64     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
65   }
66   else {
67     _local_impl = NULL;
68     _corba_impl = SALOMEDS::SObject::_duplicate(theSObject);
69   }
70
71   init_orb();
72 }
73
74 SALOMEDS_SObject::SALOMEDS_SObject(const Handle(SALOMEDSImpl_SObject)& theSObject)
75 :_isLocal(true)
76 {
77   _corba_impl = SALOMEDS::SObject::_nil();
78   _local_impl = theSObject;
79
80   init_orb();
81 }
82
83 SALOMEDS_SObject::~SALOMEDS_SObject()
84 {
85   if (!_isLocal) {
86     _corba_impl->Destroy();
87   }
88 }
89
90 std::string SALOMEDS_SObject::GetID()
91 {
92   std::string aValue;
93   if(_isLocal) aValue = _local_impl->GetID().ToCString();
94   else aValue = _corba_impl->GetID();  
95   return aValue;
96 }
97
98 _PTR(SComponent) SALOMEDS_SObject::GetFatherComponent()
99 {
100   if(_isLocal) {
101     Handle(SALOMEDSImpl_SComponent) aSCO = Handle(SALOMEDSImpl_SComponent)::DownCast(_local_impl->GetFatherComponent());
102     return _PTR(SComponent)(new SALOMEDS_SComponent(aSCO));
103   }
104   return _PTR(SComponent)(new SALOMEDS_SComponent(_corba_impl->GetFatherComponent()));
105 }
106
107 _PTR(SObject) SALOMEDS_SObject::GetFather()
108 {
109   if(_isLocal) return _PTR(SObject)(new SALOMEDS_SObject(_local_impl->GetFather()));
110   return _PTR(SObject)(new SALOMEDS_SObject(_corba_impl->GetFather()));
111 }
112
113 bool SALOMEDS_SObject::FindAttribute(_PTR(GenericAttribute)& anAttribute, const std::string& aTypeOfAttribute)
114 {
115   bool ret = false;
116   if(_isLocal) {
117     Handle(SALOMEDSImpl_GenericAttribute) anAttr;
118     ret = _local_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute.c_str());
119     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
120   }
121   else {
122     SALOMEDS::GenericAttribute_var anAttr;
123     ret = _corba_impl->FindAttribute(anAttr.out(), aTypeOfAttribute.c_str());
124     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(anAttr));
125   }
126
127   return ret;
128 }
129
130 bool SALOMEDS_SObject::ReferencedObject(_PTR(SObject)& theObject)
131 {
132   bool ret = false;
133   if(_isLocal) {
134     Handle(SALOMEDSImpl_SObject) aSO;
135     ret = _local_impl->ReferencedObject(aSO);
136     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
137   }
138   else {
139     SALOMEDS::SObject_var aSO;
140     ret = _corba_impl->ReferencedObject(aSO.out());
141     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
142   }
143
144   return ret; 
145 }
146
147
148 bool SALOMEDS_SObject::FindSubObject(int theTag, _PTR(SObject)& theObject)
149 {
150   bool ret = false;
151   if(_isLocal) {
152     Handle(SALOMEDSImpl_SObject) aSO;
153     ret = _local_impl->FindSubObject(theTag, aSO);
154     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
155   }
156   else {
157     SALOMEDS::SObject_var aSO;
158     ret = _corba_impl->FindSubObject(theTag, aSO.out());
159     if(ret) theObject = _PTR(SObject)(new SALOMEDS_SObject(aSO));
160   }
161
162   return ret;   
163 }
164
165 _PTR(Study) SALOMEDS_SObject::GetStudy()
166 {
167   if(_isLocal) return _PTR(Study)(new SALOMEDS_Study(_local_impl->GetStudy()));
168   return _PTR(Study)(new SALOMEDS_Study(_corba_impl->GetStudy()));
169 }
170
171 std::string SALOMEDS_SObject::Name()
172 {
173   std::string aName;
174   if(_isLocal) aName = _local_impl->Name().ToCString();
175   else aName = _corba_impl->Name();
176
177   return aName;
178 }
179
180 void  SALOMEDS_SObject::Name(const std::string& theName)
181 {
182   if(_isLocal) _local_impl->Name((char*)theName.c_str());
183   else _corba_impl->Name(theName.c_str());
184 }
185
186 vector<_PTR(GenericAttribute)> SALOMEDS_SObject::GetAllAttributes()
187 {
188   vector<_PTR(GenericAttribute)> aVector;
189   int aLength = 0;
190   SALOMEDSClient_GenericAttribute* anAttr;
191
192   if(_isLocal) {
193     Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetAllAttributes();
194     aLength = aSeq->Length();
195     for(int i = 1; i <= aLength; i++) {
196       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aSeq->Value(i)));
197       aVector.push_back(_PTR(GenericAttribute)(anAttr));
198     }
199   }
200   else {
201     SALOMEDS::ListOfAttributes_var aSeq = _corba_impl->GetAllAttributes();
202     aLength = aSeq->length();
203     for(int i = 0; i < aLength; i++) {
204       anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aSeq[i]);
205       aVector.push_back(_PTR(GenericAttribute)(anAttr));
206     }
207   }
208
209   return aVector;
210 }
211
212 std::string SALOMEDS_SObject::GetName()
213 {
214   std::string aName;
215   if(_isLocal) aName = _local_impl->GetName().ToCString();
216   else aName = _corba_impl->GetName();
217
218   return aName;
219 }
220
221 std::string SALOMEDS_SObject::GetComment()
222 {
223   std::string aComment;
224   if(_isLocal) aComment = _local_impl->GetComment().ToCString();
225   else aComment = _corba_impl->GetComment();
226
227   return aComment;
228 }
229
230 std::string SALOMEDS_SObject::GetIOR()
231 {
232   std::string anIOR;
233   if(_isLocal) anIOR = _local_impl->GetIOR().ToCString();
234   else anIOR = _corba_impl->GetIOR();
235
236   return anIOR;
237 }
238
239 int SALOMEDS_SObject::Tag()
240 {
241   if(_isLocal) return _local_impl->Tag();
242   return _corba_impl->Tag(); 
243 }
244
245 int SALOMEDS_SObject::Depth()
246 {
247   if(_isLocal) return _local_impl->Depth();
248   return _corba_impl->Depth();  
249 }
250
251 CORBA::Object_ptr SALOMEDS_SObject::GetObject()
252 {
253   CORBA::Object_var obj;
254   if(_isLocal) {
255     std::string anIOR = GetIOR();
256     if (!anIOR.empty())
257       obj = _orb->string_to_object(anIOR.c_str());
258     return obj._retn();
259   }
260   else {
261     obj = _corba_impl->GetObject();
262     return obj._retn();
263   }
264
265   return CORBA::Object::_nil();
266 }
267
268 SALOMEDS::SObject_ptr SALOMEDS_SObject::GetSObject()
269 {
270   if(_isLocal) {
271     if(!CORBA::is_nil(_corba_impl)) return _corba_impl;
272     SALOMEDS::SObject_var aSO = SALOMEDS_SObject_i::New(_local_impl, _orb);
273     return aSO._retn();
274   }
275   else {
276     return _corba_impl;
277   }
278   return SALOMEDS::SObject::_nil();
279 }
280
281
282 void SALOMEDS_SObject::init_orb()
283 {
284   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
285   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
286   _orb = init(0 , 0 ) ;     
287 }