Salome HOME
4206e551d2a08b4789a9fc88d786ece5da30df1e
[modules/yacs.git] / src / evalyfx / YACSEvalSeqAny.cxx
1 // Copyright (C) 2012-2016  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "YACSEvalSeqAny.hxx"
22 #include "YACSEvalPort.hxx"
23 #include "Exception.hxx"
24
25 #include "PythonPorts.hxx"
26
27 YACSEvalSeqAny *YACSEvalSeqAny::BuildEmptyFromType(const std::string& dataType)
28 {
29   if(dataType==YACSEvalAnyDouble::TYPE_REPR)
30     return new YACSEvalSeqAnyDouble;
31   else if(dataType==YACSEvalAnyInt::TYPE_REPR)
32     return new YACSEvalSeqAnyInt;
33   else
34     throw YACS::Exception("YACSEvalSeqAny::BuildEmptyFromType : Only int and double are actualy managed !");
35 }
36
37 template<class T>
38 std::size_t YACSEvalSeqAnyInternal<T>::size() const
39 {
40   if(!_arr)
41     throw YACS::Exception("YACSEvalSeqAnyDouble<T>::size : empty array !");
42   return _arr->size();
43 }
44
45 YACSEvalSeqAnyDouble::YACSEvalSeqAnyDouble(const std::vector<double>& arr):_arr(0)
46 {
47   std::vector<double> *zeArr(new std::vector<double>(arr));
48   _arr=YACSEvalSeqAnyInternal<double>::New(zeArr);
49 }
50
51 YACSEvalSeqAnyDouble::YACSEvalSeqAnyDouble(const YACSEvalSeqAnyDouble& other):_arr(other._arr)
52 {
53   if(_arr)
54     _arr->incrRef();
55 }
56
57 YACSEvalSeqAnyDouble::YACSEvalSeqAnyDouble():_arr(YACSEvalSeqAnyInternal<double>::New(new std::vector<double>(0)))
58 {
59 }
60
61 std::string YACSEvalSeqAnyDouble::getTypeOfConstitutingElements() const
62 {
63   return std::string(YACSEvalAnyDouble::TYPE_REPR);
64 }
65
66 YACSEvalSeqAnyDouble *YACSEvalSeqAnyDouble::copy() const
67 {
68   return new YACSEvalSeqAnyDouble(*this);
69 }
70
71 void YACSEvalSeqAnyDouble::initialize(YACS::ENGINE::InputPyPort *p) const
72 {
73   std::size_t sz(size());
74   PyObject *ob(PyList_New(sz));
75   for(std::size_t i=0;i<sz;i++)
76     {
77       PyList_SetItem(ob,i,PyFloat_FromDouble((*_arr)[i]));
78     }
79   p->put(ob);
80   p->exSaveInit();
81   Py_XDECREF(ob);
82 }
83
84 std::vector<double> *YACSEvalSeqAnyDouble::getInternal() const
85 {
86   if(!_arr)
87     throw YACS::Exception("YACSEvalSeqAnyDouble::getInternal : null internal ref !");
88   return _arr->getInternal();
89 }
90
91 YACSEvalSeqAnyInt::YACSEvalSeqAnyInt(const std::vector<int>& arr):_arr(0)
92 {
93   std::vector<int> *zeArr(new std::vector<int>(arr));
94   _arr=YACSEvalSeqAnyInternal<int>::New(zeArr);
95 }
96
97 YACSEvalSeqAnyInt::YACSEvalSeqAnyInt(const YACSEvalSeqAnyInt& other):_arr(other._arr)
98 {
99   if(_arr)
100     _arr->incrRef();
101 }
102
103 YACSEvalSeqAnyInt::YACSEvalSeqAnyInt():_arr(YACSEvalSeqAnyInternal<int>::New(new std::vector<int>(0)))
104 {
105 }
106
107 std::string YACSEvalSeqAnyInt::getTypeOfConstitutingElements() const
108 {
109   return std::string(YACSEvalAnyInt::TYPE_REPR);
110 }
111
112 YACSEvalSeqAnyInt *YACSEvalSeqAnyInt::copy() const
113 {
114   return new YACSEvalSeqAnyInt(*this);
115 }
116
117 void YACSEvalSeqAnyInt::initialize(YACS::ENGINE::InputPyPort *p) const
118 {
119   std::size_t sz(size());
120   PyObject *ob(PyList_New(sz));
121   for(std::size_t i=0;i<sz;i++)
122     {
123       PyList_SetItem(ob,i,PyInt_FromLong((*_arr)[i]));
124     }
125   p->put(ob);
126   p->exSaveInit();
127   Py_XDECREF(ob);
128 }
129
130 std::vector<int> *YACSEvalSeqAnyInt::getInternal() const
131 {
132   if(!_arr)
133     throw YACS::Exception("YACSEvalSeqAnyInt::getInternal : null internal ref !");
134   return _arr->getInternal();
135 }