1 // Copyright (C) 2012-2015 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #include "YACSEvalPort.hxx"
22 #include "YACSEvalSeqAny.hxx"
23 #include "OutputPort.hxx"
24 #include "InputPort.hxx"
25 #include "TypeCode.hxx"
27 #include "CORBAPorts.hxx"
28 #include "PythonPorts.hxx"
29 #include "AnyInputPort.hxx"
35 const char YACSEvalAnyDouble::TYPE_REPR[]="double";
37 const char YACSEvalAnyInt::TYPE_REPR[]="int";
39 int YACSEvalAnyDouble::toInt() const
41 throw YACS::Exception("YACSEvalAnyDouble::toInt : invalid type requested !");
44 double YACSEvalAnyDouble::toDouble() const
49 YACSEvalAnyDouble *YACSEvalAnyDouble::deepCpy() const
51 return new YACSEvalAnyDouble(*this);
54 int YACSEvalAnyInt::toInt() const
59 double YACSEvalAnyInt::toDouble() const
61 throw YACS::Exception("YACSEvalAnyInt::toDouble : invalid type requested !");
64 YACSEvalAnyInt *YACSEvalAnyInt::deepCpy() const
66 return new YACSEvalAnyInt(*this);
69 std::string YACSEvalPort::GetTypeOfData(const YACS::ENGINE::DataPort *port)
71 YACS::ENGINE::TypeCode *tc(port->edGetType());
73 throw YACS::Exception("YACSEvalPort::GetTypeOfData : null type code !");
74 if(tc->kind()==YACS::ENGINE::Double)
75 return std::string(YACSEvalAnyDouble::TYPE_REPR);
76 if(tc->kind()==YACS::ENGINE::Int)
77 return std::string(YACSEvalAnyInt::TYPE_REPR);
78 throw YACS::Exception("YACSEvalPort::GetTypeOfData : Unrecognized type of data ! Must be int or double for the moment !");
81 YACSEvalInputPort::YACSEvalInputPort(YACS::ENGINE::InputPort *ptr):_ptr(ptr),_isLocked(false),_mySeq(0)
86 std::string YACSEvalInputPort::getName() const
88 return _ptr->getName();
91 std::string YACSEvalInputPort::getTypeOfData() const
93 return GetTypeOfData(_ptr);
96 bool YACSEvalInputPort::isOKForLock() const
100 return hasDefaultValueDefined();
103 bool YACSEvalInputPort::hasDefaultValueDefined() const
105 return _ptr->edIsManuallyInitialized();
109 * The caller has the ownership of the returned instance.
111 YACSEvalAny *YACSEvalInputPort::getDefaultValueDefined() const
113 if(!hasDefaultValueDefined())
114 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : no default value defined !");
115 _ptr->exRestoreInit();
116 YACS::ENGINE::InputPyPort *i0(dynamic_cast<YACS::ENGINE::InputPyPort *>(_ptr));
119 PyObject *data(i0->getPyObj());
121 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error !");
122 if(PyFloat_Check(data))
123 return new YACSEvalAnyDouble(PyFloat_AsDouble(data));
124 if(PyInt_Check(data))
125 return new YACSEvalAnyInt((int)PyInt_AsLong(data));
126 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : unmanaged types different from int and double for py !");
128 YACS::ENGINE::AnyInputPort *i1(dynamic_cast<YACS::ENGINE::AnyInputPort *>(_ptr));
131 YACS::ENGINE::Any *data(i1->getValue());
132 return convertFromInternalAnyToExternal(data);
134 YACS::ENGINE::InputCorbaPort *i2(dynamic_cast<YACS::ENGINE::InputCorbaPort *>(_ptr));
137 CORBA::Any *data(i2->getAny());
139 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 3 !");
140 std::string td(getTypeOfData());
141 if(td==YACSEvalAnyDouble::TYPE_REPR)
145 return new YACSEvalAnyDouble(v);
147 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 31 !");
149 if(td==YACSEvalAnyInt::TYPE_REPR)
153 return new YACSEvalAnyInt(v);
155 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 32 !");
158 std::ostringstream oss;
159 oss << "YACSEvalInputPort::getDefaultValueDefined : Please contact anthony.geay@edf.fr with -> \"" << typeid(*_ptr).name() << "\" !";
160 throw YACS::Exception(oss.str());
164 * This method does not steal the ownership of \a parameter (a deep copy is performed).
166 void YACSEvalInputPort::setDefaultValue(const YACSEvalAny *parameter)
168 checkForNonConstMethod();
171 if(parameter->getTypeOfData()!=getTypeOfData())
173 std::ostringstream oss; oss << "YACSEvalInputPort::setParameter : Type of data mismatch ! Expecting \"" << getTypeOfData() << "\" having \"" << parameter->getTypeOfData() << "\" !";
174 throw YACS::Exception(oss.str());
178 YACS::ENGINE::InputPyPort *i0(dynamic_cast<YACS::ENGINE::InputPyPort *>(_ptr));
179 YACS::ENGINE::AnyInputPort *i1(dynamic_cast<YACS::ENGINE::AnyInputPort *>(_ptr));
180 YACS::ENGINE::InputCorbaPort *i2(dynamic_cast<YACS::ENGINE::InputCorbaPort *>(_ptr));
183 const YACSEvalAnyDouble *par0(dynamic_cast<const YACSEvalAnyDouble *>(parameter));
184 const YACSEvalAnyInt *par1(dynamic_cast<const YACSEvalAnyInt *>(parameter));
189 PyObject *obj(PyFloat_FromDouble(par0->toDouble()));
195 PyObject *obj(PyInt_FromLong(par1->toInt()));
200 throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for py !");
206 YACS::ENGINE::AtomAny *v(YACS::ENGINE::AtomAny::New(par0->toDouble()));
212 YACS::ENGINE::AtomAny *v(YACS::ENGINE::AtomAny::New(par1->toInt()));
217 throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for yacsport !");
223 v<<=par0->toDouble();
227 throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for corbaport !");
232 std::ostringstream oss;
233 oss << "YACSEvalInputPort::getDefaultValueDefined : Please contact anthony.geay@edf.fr with -> \"" << typeid(*_ptr).name() << "\" !";
234 throw YACS::Exception(oss.str());
239 _ptr->edRemoveManInit();
242 void YACSEvalInputPort::setSequenceOfValuesToEval(const YACSEvalSeqAny *vals)
244 checkForNonConstMethod();
246 throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : input is NULL !");
254 bool YACSEvalInputPort::hasSequenceOfValuesToEval(std::size_t& sz) const
256 sz=std::numeric_limits<std::size_t>::max();
263 void YACSEvalInputPort::initializeUndergroundWithSeq(YACS::ENGINE::InputPyPort *p) const
266 throw YACS::Exception("YACSEvalInputPort::initializeUndergroundWithSeq : p is null !");
268 throw YACS::Exception("YACSEvalInputPort::initializeUndergroundWithSeq : this is not set as a sequence !");
269 _mySeq->initialize(p);
272 YACSEvalInputPort::~YACSEvalInputPort()
277 YACSEvalAny *YACSEvalInputPort::convertFromInternalAnyToExternal(YACS::ENGINE::Any *data) const
280 throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : internal error # 2 !");
281 YACS::ENGINE::AtomAny *data2(dynamic_cast<YACS::ENGINE::AtomAny *>(data));
283 throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : internal error # 21 !");
284 std::string td(getTypeOfData());
285 if(td==YACSEvalAnyDouble::TYPE_REPR)
286 return new YACSEvalAnyDouble(data2->getDoubleValue());
287 if(td==YACSEvalAnyInt::TYPE_REPR)
288 return new YACSEvalAnyInt(data2->getIntValue());
289 throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : unmanaged types different from int and double for any !");
292 void YACSEvalInputPort::checkForNonConstMethod() const
295 throw YACS::Exception("YACSEvalInputPort::checkForNonConstMethod : trying to modify a locked input ! To release it call unlockAll on YACSEvalYFX instance owning this !");
298 YACSEvalOutputPort::YACSEvalOutputPort(YACS::ENGINE::OutputPort *ptr):_ptr(ptr)
303 std::string YACSEvalOutputPort::getName() const
305 return _ptr->getName();
308 std::string YACSEvalOutputPort::getTypeOfData() const
310 return GetTypeOfData(_ptr);