]> SALOME platform Git repositories - modules/superv.git/blob - src/Supervision/Value_Impl.cxx
Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/superv.git] / src / Supervision / Value_Impl.cxx
1 //  SUPERV Supervision : contains the implementation of interfaces of SuperVision described in SUPERV.idl
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Value_Impl.cxx
25 //  Author : Jean Rahuel
26 //  Module : SUPERV
27 //  $Header: 
28
29 using namespace std;
30 #include <stdio.h>
31 #include <fstream>
32 //#include <strstream>
33 //#include <sstream>
34 #include <string>
35
36 //#include "utilities.h"
37
38 #include "Value_Impl.hxx"
39
40 #include "DataFlowExecutor_DataFlow.hxx"
41
42 Value_Impl::Value_Impl( CORBA::ORB_ptr orb ,
43                         PortableServer::POA_ptr poa ,
44                         PortableServer::ObjectId * contId , 
45                         const char *instanceName ,
46                         const char *interfaceName ,
47                         GraphEditor::DataFlow * DataFlowEditor ,
48                         GraphEditor::InNode * DataFlowNode ,
49                         const char *ParameterName ,
50                         const CORBA::Any * anAny ,
51                         const bool activate ) :
52   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
53   if ( activate ) {
54 //    MESSAGE("Value_Impl::Value_Impl activate object instanceName("
55 //            << instanceName << ") interfaceName(" << interfaceName << ") --> "
56 //            << hex << (void *) this << dec )
57     _thisObj = this ;
58     _id = _poa->activate_object(_thisObj);
59   }
60   _Orb = CORBA::ORB::_duplicate(orb);
61   _Poa = poa ;
62   _ContId = contId ;
63   _DataFlowEditor = DataFlowEditor ;
64   _DataFlowNode = DataFlowNode ;
65   if ( ParameterName != NULL ) {
66     _ParameterName = new char[ strlen( ParameterName ) + 1 ] ;
67     strcpy( _ParameterName , ParameterName ) ;
68     if ( DataFlowNode ) {
69       _NodeName = new char[ strlen( DataFlowNode->Name() ) + 1 ] ;
70       strcpy( _NodeName , DataFlowNode->Name() ) ;
71     }
72     else {
73     _NodeName = NULL ;
74     }
75   }
76   else {
77     _ParameterName = NULL ;
78     _NodeName = NULL ;
79   }
80   _InputValue = false ;
81   _Any = new CORBA::Any( *anAny ) ;
82 }
83
84 Value_Impl::Value_Impl( CORBA::ORB_ptr orb ,
85                         PortableServer::POA_ptr poa ,
86                         PortableServer::ObjectId * contId , 
87                         const char *instanceName ,
88                         const char *interfaceName ,
89                         GraphEditor::DataFlow * DataFlowEditor ,
90                         GraphEditor::InNode * DataFlowNode ,
91                         const char *ParameterName ,
92                         const bool activate ) :
93   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
94   if ( activate ) {
95 //    MESSAGE("Value_Impl::Value_Impl activate object instanceName("
96 //            << instanceName << ") interfaceName(" << interfaceName << ") --> "
97 //            << hex << (void *) this << dec )
98     _thisObj = this ;
99     _id = _poa->activate_object(_thisObj);
100   }
101   _Orb = CORBA::ORB::_duplicate(orb);
102   _Poa = poa ;
103   _ContId = contId ;
104   _DataFlowEditor = DataFlowEditor ;
105   _DataFlowNode = DataFlowNode ;
106   if ( ParameterName != NULL ) {
107     _ParameterName = new char[ strlen( ParameterName ) + 1 ] ;
108     strcpy( _ParameterName , ParameterName ) ;
109     if ( DataFlowNode ) {
110       _NodeName = new char[ strlen( DataFlowNode->Name() ) + 1 ] ;
111       strcpy( _NodeName , DataFlowNode->Name() ) ;
112     }
113     else {
114     _NodeName = NULL ;
115     }
116   }
117   else {
118     _ParameterName = NULL ;
119     _NodeName = NULL ;
120   }
121   _InputValue = false ;
122   _Any = new CORBA::Any() ;
123 }
124
125 Value_Impl::Value_Impl() {
126 }
127
128 Value_Impl::~Value_Impl() {
129   beginService( "Value_Impl::~Value_Impl" );
130   endService( "Value_Impl::~Value_Impl" );
131 }
132
133 void Value_Impl::destroy() {
134   beginService( "Value_Impl::destroy" );
135   _poa->deactivate_object(*_id) ;
136   CORBA::release(_poa) ;
137   delete(_id) ;
138   _thisObj->_remove_ref();
139   endService( "Value_Impl::destroy" );
140 }
141
142 void Value_Impl::InPort( bool anInput ) {
143   _InputPort = anInput ;
144 }
145
146 bool Value_Impl::Input( const CORBA::Any * anAny ) {
147 //  _DataFlowEditor->AddInputData( _DataFlowNode->Name() ,
148 //                                 _ParameterName ,
149 //                                 *anAny ) ;
150   delete _Any ;
151   _Any = new CORBA::Any( *anAny ) ;
152   _InputValue = true ;
153   return true ;
154 }
155
156 CORBA::Any * Value_Impl::ToAny() {
157   CORBA::Any * anAny ;
158   if ( _DataFlowEditor ) {
159     char * name ;
160     if ( _DataFlowNode ) {
161       name = _DataFlowNode->Name() ;
162     }
163     else {
164       name = _DataFlowEditor->Graph()->Name() ;
165     }
166     if ( _InputPort ) {
167       if ( _DataFlowNode ) {
168         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
169       }
170       else {
171         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
172       }
173       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
174         anAny = new CORBA::Any( * _DataFlowEditor->GetInData( name ,
175                                                               _ParameterName ) ) ;
176       }
177       else {
178         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
179         if ( _DataFlowExecutor ) {
180           anAny = new CORBA::Any( * _DataFlowExecutor->GetInData( name ,
181                                                                   _ParameterName ) ) ;
182         }
183         else {
184           anAny = new CORBA::Any( *_Any ) ;
185         }
186       }
187     }
188     else {
189       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
190         anAny = new CORBA::Any( * _DataFlowEditor->GetOutData( name ,
191                                                               _ParameterName ) ) ;
192       }
193       else {
194         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
195         if ( _DataFlowExecutor ) {
196           anAny = new CORBA::Any( * _DataFlowExecutor->GetOutData( name ,
197                                                                   _ParameterName ) ) ;
198         }
199         else {
200           anAny = new CORBA::Any( *_Any ) ;
201         }
202       }
203     }
204   }
205   else {
206     anAny = new CORBA::Any( *_Any ) ;
207   }
208   return anAny ;
209 }
210
211 char * Value_Impl::ToString() {
212 //  beginService( "Value_Impl::ToString" );
213   CORBA::Any anAny = *ToAny() ;
214 //  cout << "Value_Impl::ToString " << _DataFlowNode->Name() << " "
215 //       << _ParameterName << " " ;
216   if ( _DataFlowEditor ) {
217     char * name ;
218     if ( _DataFlowNode ) {
219       name = _DataFlowNode->Name() ;
220     }
221     else {
222       name = _DataFlowEditor->Graph()->Name() ;
223     }
224     if ( _InputPort ) {
225       if ( _DataFlowNode ) {
226         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
227       }
228       else {
229         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
230       }
231       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
232         anAny = * _DataFlowEditor->GetInData( name ,
233                                               _ParameterName ) ;
234       }
235       else {
236         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
237         if ( _DataFlowExecutor ) {
238           anAny = * _DataFlowExecutor->GetInData( name ,
239                                                   _ParameterName ) ;
240         }
241       }
242     }
243     else {
244       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
245         anAny = * _DataFlowEditor->GetOutData( name ,
246                                                _ParameterName ) ;
247       }
248       else {
249         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
250         if ( _DataFlowExecutor ) {
251           anAny = * _DataFlowExecutor->GetOutData( name ,
252                                                    _ParameterName ) ;
253         }
254       }
255     }
256   }
257   ostringstream astr ;
258   const char * retstr ;
259   int startstr = 0 ;
260   switch (anAny.type()->kind()) {
261     case CORBA::tk_string: {
262       anAny >>= retstr;
263 //      MESSAGE( "ToString( string ) '" << retstr << "'" );
264       break ;
265     }
266     case CORBA::tk_long: {
267       long l;
268       anAny >>= l;
269       astr << l << ends ;
270       retstr = astr.str().c_str() ;
271 //      MESSAGE( "ToString( long ) '" << l << " " << retstr << "'" );
272       break ;
273     }
274     case CORBA::tk_double: {
275       double d;
276       anAny >>= d;
277       astr << setw(25) << setprecision(18) << d << ends ;
278       retstr = astr.str().c_str() ;
279       int i = 0 ;
280       while ( i < (int ) strlen( retstr ) && retstr[ i++ ] == ' ' ) {
281         startstr = i ;
282       }
283 //      MESSAGE( "ToString( double ) '" << d << "' '" << retstr << "' '" << &retstr[ startstr ] << "'");
284       break ;
285     }
286     case CORBA::tk_objref: {
287       CORBA::Object_ptr obj ;
288       try {
289         anAny >>= obj ;
290         retstr = _Orb->object_to_string( obj );
291 //        MESSAGE( "ToString( object ) '" << retstr << "'" );
292       }
293       catch ( ... ) {
294         retstr = "object_to_string catched " ;
295       }
296       break ;
297     }
298     default: {
299       retstr = "Unknown CORBA::Any Type" ;
300 //      MESSAGE( retstr );
301       break ;
302     }
303   }
304 //  endService( "Value_Impl::ToString" );
305   return CORBA::string_dup( &retstr[ startstr ] ) ;
306 }
307
308 bool Value_Impl::IsIOR() {
309   CORBA::Any anAny = *ToAny() ;
310   if ( _DataFlowEditor ) {
311     char * name ;
312     if ( _DataFlowNode ) {
313       name = _DataFlowNode->Name() ;
314     }
315     else {
316       name = _DataFlowEditor->Graph()->Name() ;
317     }
318     if ( _InputPort ) {
319       if ( _DataFlowNode ) {
320         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
321       }
322       else {
323         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
324       }
325       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
326         anAny = * _DataFlowEditor->GetInData( name ,
327                                               _ParameterName ) ;
328       }
329       else {
330         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
331         if ( _DataFlowExecutor ) {
332           anAny = * _DataFlowExecutor->GetInData( name ,
333                                                   _ParameterName ) ;
334         }
335       }
336     }
337     else {
338       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
339         anAny = * _DataFlowEditor->GetOutData( name ,
340                                                _ParameterName ) ;
341       }
342       else {
343         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
344         if ( _DataFlowExecutor ) {
345           anAny = * _DataFlowExecutor->GetOutData( name ,
346                                                    _ParameterName ) ;
347         }
348       }
349     }
350   }
351   return(anAny.type()->kind() == CORBA::tk_objref);
352 }
353
354 char* Value_Impl::ComponentDataType() {
355   GraphExecutor::DataFlow* _DataFlowExecutor = _DataFlowEditor->Executor();
356   const GraphBase::ComputingNode* node = _DataFlowExecutor->Graph()->GetGraphNode(_DataFlowNode->Name());
357   if ( node->IsFactoryNode() ) {
358     Engines::Component_var compo = ((GraphBase::FactoryNode * ) node )->Component();
359     if ( CORBA::is_nil( compo ) ) {
360       return ( strdup( "UnknownComponent" ) ) ;
361     }
362     else {
363       return ( strdup( compo->instanceName() ) ) ;
364     }
365   }
366   return ( strdup ( "UnknownComponent" ) ) ;
367 }