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