Salome HOME
PR: merge from BR_DATACONV_PR tag "mergeto_trunk_25oct06"
[modules/yacs.git] / src / runtime / CORBAPorts.cxx
1
2 #include "CORBAPorts.hxx"
3 #include "RuntimeSALOME.hxx"
4 #include "TypeConversions.hxx"
5
6 #include <iostream>
7 #include <sstream>
8
9
10 using namespace YACS::ENGINE;
11 using namespace std;
12
13
14 InputCorbaPort::InputCorbaPort(const string& name,
15                                Node *node,
16                                TypeCode * type)
17   : InputPort(name, node, type), Port(node)
18 {
19   _impl="CORBA";
20   _orb = getSALOMERuntime()->getOrb();
21 }
22
23 void InputCorbaPort::put(const void *data) throw (ConversionException)
24 {
25   put((CORBA::Any *)data);
26   _empty = false;
27 }
28
29 void InputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
30 {
31   cerr << "InputCorbaPort::put" << endl;
32   // cerr << "addr data: " << data << endl;
33   // cerr << "addr data.value(): " << data->value() << endl;
34   switch(type()->kind())
35     {
36     case Double:
37       CORBA::Double d;
38       *data >>= d;
39       cerr << "Double: " << d << endl;
40       break;
41     case Int:
42       CORBA::Long l;
43       *data >>= l;
44       cerr << "Int: " << l << endl;
45       break;
46     case Sequence:
47       break;
48     default:
49       break;
50     }
51   // on fait une copie du any (protection contre la destruction du any source)
52   // la gestion des destructions est correctement faite par omniorb
53   _data=*data;
54   // cerr << "addr _data: " << &_data << endl;
55   // cerr << "addr _data.value(): " << _data.value() << endl;
56 }
57
58 CORBA::Any * InputCorbaPort::getAny()
59 {
60   // cerr << "_data: " << &_data << endl;
61   // cerr << "_data.value(): " << _data.value() << endl;
62   // cerr << "_data.NP_pd(): " << _data.NP_pd() << endl;
63   // --- on retourne un pointeur sur le any interne
64   return &_data;
65 }
66
67
68 OutputCorbaPort::OutputCorbaPort(const string& name,
69                                  Node *node,
70                                  TypeCode * type)
71   : OutputPort(name, node, type), Port(node)
72 {
73   _impl="CORBA";
74   _orb = getSALOMERuntime()->getOrb();
75 }
76
77 void OutputCorbaPort::put(const void *data) throw (ConversionException)
78 {
79   put((CORBA::Any *)data);
80 }
81
82 void OutputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
83 {
84   cerr << "OutputCorbaPort::put" << endl;
85   // cerr << "addr data: " << data << endl;
86   InputPort *p;
87   // on fait une copie du any source
88   // (protection contre la destruction de la source)
89   _data=*data;
90   set<InputPort *>::iterator iter;
91   for(iter=_setOfInputPort.begin(); iter!=_setOfInputPort.end(); iter++)
92     {
93       p=*iter;
94       // on pousse le pointeur mais put fait normalement une copie
95       p->put(&_data);
96     }
97 }
98
99 CORBA::Any * OutputCorbaPort::getAny()
100 {
101   // cerr << "_data: " << &_data << endl;
102   // cerr << "_data.value(): " << _data.value() << endl;
103   // cerr << "_data.NP_pd(): " << _data.NP_pd() << endl;
104   // on retourne un pointeur sur le any interne
105   return &_data;
106 }
107
108 CORBA::Any * OutputCorbaPort::getAnyOut() 
109 {
110   CORBA::Any* a=&_data;
111   DynType kind=type()->kind();
112
113   if(kind == Int)
114     {
115       a->replace(CORBA::_tc_long, (void*) 0);
116     }
117     else if(kind == String)
118       {
119         a->replace(CORBA::_tc_string, (void*) 0);
120       }
121     else if(kind == Double)
122       {
123         a->replace(CORBA::_tc_double, (void*) 0);
124       }
125     else if(kind == Objref)
126       {
127         a->replace(CORBA::_tc_Object, (void*) 0);
128       }
129     else if(kind == Sequence)
130       {
131         CORBA::TypeCode_var t;
132         t = getCorbaTC(type());
133         a->replace(t, (void*) 0);
134        }
135     else if(kind == Bool)
136       {
137         stringstream msg;
138         msg << "Cannot set Any Out for Bool" << __FILE__ << ":" << __LINE__;
139         throw Exception(msg.str());
140       }
141     else if(kind == None)
142       {
143         stringstream msg;
144         msg << "Cannot set Any Out for None" << __FILE__ << ":" << __LINE__;
145         throw Exception(msg.str());
146       }
147     else
148       {
149         stringstream msg;
150         msg << "Cannot set Any Out for unknown type" << __FILE__
151             << ":" << __LINE__;
152         throw Exception(msg.str());
153       }
154     
155   // on retourne un pointeur sur le any interne reinitialisé
156   // cerr << "getAnyOut::_data: " << a << endl;
157   return a;
158 }
159
160 ostream& YACS::ENGINE::operator<<(ostream& os, const OutputCorbaPort& p)
161 {
162   CORBA::Double l;
163   p._data>>=l;
164   os << p._name << " : " << l ;
165   return os;
166 }
167