]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphBase/DataFlowBase_DataPort.cxx
Salome HOME
NRI : Merge from V1_2.
[modules/superv.git] / src / GraphBase / DataFlowBase_DataPort.cxx
1 //  SUPERV GraphBase : contains fondamental classes for Services, Input Ports, Output Ports Links and Nodes.
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   : DataFlowBase_DataPort.cxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 using namespace std;
30 #include <stdio.h>
31 #include "DataFlowBase_DataPort.hxx"
32
33 GraphBase::DataPort::DataPort(
34          const char *const * NodeName  ,
35          const SALOME_ModuleCatalog::ServicesParameter aserviceParameter ) :
36               Port( NodeName , aserviceParameter ) {
37   pthread_mutex_init( &_MutexWait , NULL ) ;
38   InitialValues( CORBA::Any() ) ;
39 }
40
41 GraphBase::DataPort::~DataPort() {
42 }
43
44 void GraphBase::DataPort::InitialValues(CORBA::Any aValue ) {
45   _theValue = new CORBA::Any( aValue ) ;
46   string _Type = CORBA::string_dup(GetServicesParameter().Parametertype) ;
47   const char * Type = _Type.c_str();
48   CORBA::Any InitialValue ;
49   cdebug << "InitialValues " << NodeName() << " " << PortName() << " " << PortType()
50          << " : " ;
51   if ( !strcmp( Type , "string" ) ) {
52     cdebug << "string" << endl ;
53     InitialValue <<= (char *) NULL ;
54   }
55   else if ( !strcmp( Type , "double" ) ) {
56     cdebug << "double" << endl ;
57     InitialValue <<= 0. ;
58   }
59   else if ( !strcmp( Type , "long" ) ) {
60     cdebug << "long" << endl ;
61     InitialValue <<= (long ) 0 ;
62   }
63   else if ( !strcmp( Type , "objref" ) ) {
64     cdebug << "objref" << endl ;
65 //    InitialValue.replace(CORBA::_tc_Object, NULL);
66     InitialValue <<= CORBA::Object::_nil() ;
67   }
68   else {
69     cdebug << "InitialValues ERROR (other) " << Type << endl ;
70     InitialValue <<= (long ) 0 ;
71   }
72   _InitialValue = new CORBA::Any( InitialValue ) ;
73   _Value = &_InitialValue ;
74 }
75
76 void GraphBase::DataPort::Value( const CORBA::Any & aDataValue ) {
77   Value( new CORBA::Any( aDataValue ) ) ;
78 }
79
80 void GraphBase::DataPort::Value( const CORBA::Any * aDataValue ) {
81 //  cdebug << pthread_self() << " Value(aDataValue) --> pthread_mutex_lock " << &_MutexWait
82 //         << endl ;
83   if ( pthread_mutex_lock( &_MutexWait ) ) {
84     perror( "lock DataValue" ) ;
85     exit( 0 ) ;
86   }
87 //  cdebug << pthread_self() << " Value(aDataValue) pthread_mutex_locked " << &_MutexWait
88 //         << endl ;
89
90   delete _theValue ;
91   _theValue = aDataValue ;
92   *_Value = aDataValue ;
93 //  Done( true ) ;
94
95 #if 0
96   cdebug << "NewValue " << NodeName() << " " << PortName() << " " << PortType()
97          << " : " << _Value << " *_Value " << *_Value  << " " ;
98   switch (_theValue->type()->kind()) {
99   case CORBA::tk_string: {
100     char * t;
101     *_theValue >>= t;
102     cdebug << "Value( " << t << ") (string)";
103     break;
104   }
105   case CORBA::tk_double: {
106     double d;
107     *_theValue >>= d;
108     cdebug << "Value( " << d << ") (double)";
109     break;
110   }
111   case CORBA::tk_long: {
112     long l;
113     *_theValue >>= l;
114     cdebug << "Value( " << l << ") (long)";
115     break;
116   }
117   case CORBA::tk_objref: {
118     cdebug << "Value( " << ") (object reference)";
119     break;
120   }
121   default: {
122     cdebug << "Value" << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR";
123     break;
124   }
125   }
126   cdebug << endl ;
127 #endif
128   if ( pthread_mutex_unlock( &_MutexWait ) ) {
129     perror( "unlock DataValue" ) ;
130     exit( 0 ) ;
131   }
132 //  cdebug << pthread_self() << " Value(aDataValue) --> pthread_mutex_unlocked " << &_MutexWait
133 //         << endl ;
134
135 }
136
137 void GraphBase::DataPort::Value( const CORBA::Any ** aDataSharedValue ) {
138   delete _theValue ;
139   _theValue = *aDataSharedValue ;
140   _Value = aDataSharedValue ;
141 //  Done( true ) ;
142
143 #if 0
144   MESSAGE( NodePortName() << " : " << hex << _Value << " " );
145   switch (_theValue->type()->kind()) {
146   case CORBA::tk_string:
147     char * t;
148     *_theValue >>= t;
149     MESSAGE( "Value( " << t << ") (string)");
150     break;
151   case CORBA::tk_double:
152     double d;
153     *_theValue >>= d;
154     MESSAGE( "Value( " << d << ") (double)");
155     break;
156   case CORBA::tk_long:
157     long l;
158     *_theValue >>= l;
159     MESSAGE( "Value( " << l << ") (long)");
160     break;
161   case CORBA::tk_objref:
162     MESSAGE( "Value( " << ") (object reference)");
163     break;
164   default:
165     MESSAGE( "Value" << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR");
166     break;
167   }
168   MESSAGE();
169 #endif
170 }
171
172 CORBA::Any const * GraphBase::DataPort::Value() const {
173   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
174 //  cdebug << pthread_self() << " Value() --> pthread_mutex_lock " << aMutexWait << endl ;
175   if ( pthread_mutex_lock( aMutexWait ) ) {
176     perror( "lock DataValue" ) ;
177     exit( 0 ) ;
178   }
179 //  cdebug << pthread_self() << " Value() --> pthread_mutex_locked " << aMutexWait << endl ;
180   const CORBA::Any *const Value = *_Value ;
181   CORBA::Any * theValue ;
182 //  cdebug  << pthread_self() << " Value " << NodeName() << " " << PortName() << " "
183 //          << PortType() << " _Value " << _Value << " *_Value " << *_Value  << " kind "
184 //          << Value->type()->kind() << " :" << endl ;
185   if ( Done() ) {
186     theValue = new CORBA::Any( *Value ) ;
187     switch ( theValue->type()->kind() ) {
188     case CORBA::tk_string: {
189       char * t;
190       *theValue >>= t;
191 //      cdebug << "GraphBase::DataPort::Value() : " << t << " (string) " << endl ;
192       break;
193     }
194     case CORBA::tk_double: {
195       double d;
196       *theValue >>= d;
197 //      cdebug << "GraphBase::DataPort::Value() : " << d << " (double) " << endl ;
198       break;
199     }
200     case CORBA::tk_long: {
201       long l;
202       *theValue >>= l;
203 //      cdebug << "GraphBase::DataPort::Value() : " << l << " (long) " << endl ;
204       break;
205     }
206     case CORBA::tk_objref: {
207       CORBA::Object_ptr obj ;
208       char * retstr ;
209       try {
210         *theValue >>= obj ;
211         retstr = ObjectToString( obj );
212 //        cdebug << "GraphBase::DataPort::Value() : " << retstr
213 //               << "(object reference) " << endl;
214       }
215       catch( ... ) {
216         cdebug << "ToString( object ) Catched ERROR" << endl ;
217       }
218       break;
219     }
220     default: {
221       cdebug << "GraphBase::DataPort::Value() : "
222              << "(other(tk_string,tk_double,tk_long,tk_objref)) ERROR" << endl ;
223       break;
224     }
225     }
226   }
227   else {
228 //    cdebug << "GraphBase::DataPort::InitialValue() " << endl ;
229     theValue = new CORBA::Any( *Value ) ;
230   }
231   if ( pthread_mutex_unlock( aMutexWait ) ) {
232     perror( "unlock DataValue" ) ;
233     exit( 0 ) ;
234   }
235 //  cdebug << pthread_self() << " Value() --> pthread_mutex_unlocked " << aMutexWait << endl ;
236     
237   return theValue ;
238 }
239
240 bool GraphBase::DataPort::BoolValue() const {
241   bool RetVal = false ;
242   long val ;
243   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
244 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_lock " << aMutexWait << endl ;
245   if ( pthread_mutex_lock( aMutexWait ) ) {
246     perror( "lock DataValue" ) ;
247     exit( 0 ) ;
248   }
249 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_locked " << aMutexWait
250 //         << endl ;
251   if ( (*_Value)->type()->kind() == CORBA::tk_long ) {
252     **_Value >>= val ;
253     if ( val ) {
254       RetVal = true ;
255     }
256   }
257   if ( pthread_mutex_unlock( aMutexWait ) ) {
258     perror( "unlock DataValue" ) ;
259     exit( 0 ) ;
260   }
261 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_unlocked " << aMutexWait
262 //         << endl ;
263   return RetVal ;
264 }
265
266 const CORBA::Any ** GraphBase::DataPort::ValuePtr() const {
267   return _Value ;
268 }
269
270 void GraphBase::DataPort::StringValue(ostream & f ) const {
271   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
272 //  cdebug << pthread_self() << " StringValue() --> pthread_mutex_lock " << aMutexWait
273 //         << endl ;
274   if ( pthread_mutex_lock( aMutexWait ) ) {
275     perror( "lock DataValue" ) ;
276     exit( 0 ) ;
277   }
278 //  cdebug << pthread_self() << " StringValue() --> pthread_mutex_locked " << aMutexWait
279 //         << endl ;
280   if ( Done() ) {
281 //    cdebug << "StringValue " << NodeName() << " " << PortName() << " " << PortType()
282 //           << " _Value "  << _Value << " *_Value "
283 //           << *_Value  << " " << endl ;
284     const CORBA::Any * theValue = *_Value ;
285     switch (theValue->type()->kind()) {
286     case CORBA::tk_string:
287       char * t;
288       *theValue >>= t;
289       f << t << " (string)" ;
290       break;
291     case CORBA::tk_double:
292       double d;
293       *theValue >>= d;
294       f << d << " (double)" ;
295       break;
296     case CORBA::tk_long:
297       long l;
298       *theValue >>= l;
299       f << l << " (long)" ;
300       break;
301     case CORBA::tk_objref:
302       CORBA::Object_ptr ObjRef ;
303       try {
304         *theValue >>= ObjRef ;
305         f << "(object reference) " << ObjectToString( ObjRef ) ;
306       }
307       catch(...) {
308         f << "(object reference) catched error" ;
309       }
310       break;
311     default:
312       f << "(other ERROR)" ;
313       break;
314     }
315   }
316   else {
317     f << " Default (undefined) NOT DONE"  ;
318   }
319   if ( pthread_mutex_unlock( aMutexWait ) ) {
320     perror( "unlock DataValue" ) ;
321     exit( 0 ) ;
322   }
323 //  cdebug << pthread_self() << " StringValue() --> pthread_mutex_unlocked " << aMutexWait
324 //         << endl ;
325     
326 }
327
328