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