Salome HOME
PR: merge from BR_DATACONV_PR tag "mergeto_trunk_25oct06"
[modules/yacs.git] / src / engine / OutputPort.cxx
1 #include "OutputPort.hxx"
2 #include "InputPort.hxx"
3 #include "Runtime.hxx"
4
5 #include <sstream>
6 #include <iostream>
7
8 using namespace YACS::ENGINE;
9 using namespace std;
10
11 const char OutputPort::NAME[]="OutputPort";
12
13 OutputPort::OutputPort(const string& name, Node *node, TypeCode* type):DataFlowPort(name,node,type),OutPort(node),Port(node)
14 {
15 }
16
17 string OutputPort::getNameOfTypeOfCurrentInstance() const
18 {
19   return NAME;
20 }
21
22 void OutputPort::exInit()
23 {
24 //   _data.exInit();
25 }
26
27
28 void OutputPort::put(const void *data) throw(ConversionException)
29 {
30 //   _data = (void *)data;
31   cerr << _name << endl;
32   cerr << _impl << endl;
33   stringstream msg;
34   msg << "Not implemented (" << __FILE__ << ":" << __LINE__ << ")";
35   throw Exception(msg.str());
36  }
37
38
39 /**
40  * check if output type is an input type and if a data converter exists before link
41  */
42
43 bool OutputPort::edAddInputPort(InputPort *inputPort) throw(ConversionException)
44 {
45   InputPort *pwrap = getRuntime()->adapt(inputPort->getImpl(),
46                                              inputPort,
47                                              this->getImpl(),
48                                              this->type());
49
50   if(!isAlreadyInSet(pwrap))
51     {
52       _setOfInputPort.insert(pwrap);
53       inputPort->edNotifyReferenced();
54       return true;
55     }
56   else
57     {
58       if ( dynamic_cast<ProxyPort*> (pwrap) )
59         {
60           cerr << "ProxyPort destruction, while creating the same link twice..." << endl;
61           delete pwrap;
62         }
63       return false;
64     }
65 }
66
67 set<InputPort *> OutputPort::edSetInputPort()
68 {
69   return _setOfInputPort;
70 }
71
72 void OutputPort::edRemoveInputPort(InputPort *inputPort) throw(Exception)
73 {
74   if(isAlreadyInSet(inputPort))
75     _setOfInputPort.erase(inputPort);
76   else
77     throw Exception("OutputPort::edRemoveInputPort : link does not exist, unable to remove it");
78 }
79
80 //Idem OutputPort::edRemoveInputPort but without any check.
81 void OutputPort::edRemoveInputPortOneWay(InputPort *inputPort)
82 {
83   _setOfInputPort.erase(inputPort);
84 }
85
86 OutputPort::~OutputPort()
87 {
88 }
89
90 bool OutputPort::isAlreadyInSet(InputPort *inputPort) const
91 {
92   bool ret=false;
93   for(set<InputPort *>::const_iterator iter=_setOfInputPort.begin();iter!=_setOfInputPort.end() && !ret;iter++)
94     if((*iter)==inputPort)
95       ret=true;
96   return ret;
97 }
98
99 /**
100  * check compatibility of port class ( an inputPort) before trying to create the link
101  */
102
103 bool OutputPort::addInPort(InPort *inPort) throw(Exception)
104 {
105   if(inPort->getNameOfTypeOfCurrentInstance()!=InputPort::NAME)
106     {
107       string what="not compatible type of port requested during building of link FROM ";
108       what+=NAME; what+=" TO "; what+=inPort->getNameOfTypeOfCurrentInstance();
109       throw Exception(what);
110     }
111   return edAddInputPort(static_cast<InputPort*>(inPort));
112 }
113
114 void OutputPort::removeInPort(InPort *inPort) throw(Exception)
115 {
116   if(inPort->getNameOfTypeOfCurrentInstance()!=InputPort::NAME)
117     {
118       string what="not compatible type of port requested during destruction of for link FROM ";
119       what+=NAME; what+=" TO "; what+=inPort->getNameOfTypeOfCurrentInstance();
120       throw Exception(what);
121     }
122   edRemoveInputPort(static_cast<InputPort*>(inPort));
123 }
124
125 bool OutputPort::isLinked()
126 {
127   return _setOfInputPort.empty();
128 }